高频top

205 阅读1分钟

反转链表

反转链表

/*
public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}*/
public class Solution {
    public ListNode ReverseList(ListNode head) {
        ListNode curr = head;
        ListNode pre = null;
        ListNode next = null;
        while(curr != null){
            next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
        return pre;
    }
}

反转链表2

反转链表2

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public void reverse(ListNode head){
        ListNode curr = head;
        ListNode pre = null;
        ListNode next = null;
        while(curr != null){
            next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
    }
    public ListNode reverseBetween(ListNode head, int left, int right) {
        ListNode dummyNod = new ListNode(-1);
        dummyNod.next = head;
        ListNode pre = dummyNod;
        for(int i=0; i<left-1; i++){
            pre = pre.next;
        }
        ListNode leftNode = pre.next;
        ListNode rightNode = pre;
        for(int i=0; i<right-left+1; i++){
            rightNode = rightNode.next;
        }
        ListNode next = rightNode.next;

        pre.next = null;
        rightNode.next = null;
        reverse(leftNode);

        pre.next = rightNode;
        leftNode.next = next;
        return dummyNod.next;
    }
}

K个一组反转链表

K个一组反转链表

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public void reverse(ListNode head){
        ListNode curr = head;
        ListNode pre = null;
        ListNode next = null;
        while(curr != null){
            next = curr.next;
            curr.next = pre;
            pre = curr;
            curr = next;
        }
    }

    public void reversePart(ListNode pre, ListNode leftNode, ListNode rightNode, ListNode next){
        pre.next = null;
        rightNode.next = null;
        reverse(leftNode);
        pre.next = rightNode;
        leftNode.next = next;
    }

    public ListNode reverseKGroup(ListNode head, int k) {
        ListNode dummyNode = new ListNode(-1);
        dummyNode.next = head;
        ListNode pre = dummyNode;
        ListNode leftNode = null;
        ListNode rightNode = head;
        ListNode next = null;
        int count = 1;
        while(rightNode != null){
            next = rightNode.next;
            if(count == k){
                leftNode = pre.next;
                reversePart(pre, leftNode, rightNode, next);
                pre = leftNode;
                count = 0;
            }
            count++;
            rightNode = next;
        }
        return dummyNode.next;
    }
}

合并两个有序数组

合并两个有序数组

class Solution {
    public void merge(int[] nums1, int m, int[] nums2, int n) {
        int p1 = 0;
        int p2 = 0;
        int[] sorted = new int[m+n];
        int index = 0;
        while(p1<m && p2<n){
            sorted[index++] = nums1[p1]<= nums2[p2] ? nums1[p1++] : nums2[p2++];
        }
        while(p1<m){
            sorted[index++] = nums1[p1++];
        }
        while(p2<n){
            sorted[index++] = nums2[p2++];
        }
        for(int i=0; i<m+n; i++){
            nums1[i] = sorted[i];
        }
    }
}

LRU缓存

import java.util.*;

class Node{
    public int key;
    public int value;
    public Node pre;
    public Node next;
    public Node(int _key, int _value){
        key = _key;
        value = _value;
    }
}

class DoubleLinkedList{
    public Node head;
    public Node tail;
    public DoubleLinkedList(){
        head = null;
        tail = null;
    }
    public void addNode(Node node){ // 添加元素
        if(head == null){
            head = node;
            tail = node;
        }else{
            tail.next = node;
            node.pre = tail;
            tail = node;
        }
    }
    public void moveNodeToTail(Node node){    // 查询元素
        if(node == tail){
            return;
        }
        if(node == head){
            head = head.next;
            head.pre = null;
        }else{
            node.next.pre = node.pre;
            node.pre.next = node.next;
        }
        node.pre = tail;
        node.next = null;
        tail.next = node;
        tail = node;
    }
    public Node removeHead(){   // 清理缓存
        if(head == null){
            return head;
        }
        Node ans = head;
        if(head == tail){
            head = null;
            tail = null;
        }else{
            head = head.next;
            head.pre = null;
            ans.next = null;
        }
        return ans;
    }
}

class LRUcache{
    public DoubleLinkedList list;
    public HashMap<Integer, Node> hashMap;
    public int capacity;
    
    public LRUcache(int _capacity){
        list = new DoubleLinkedList();
        hashMap = new HashMap<>();
        capacity = _capacity;
    }
    public int get(int key){
        if(hashMap.containsKey(key)){
            Node node = hashMap.get(key);
            list.moveNodeToTail(node);
            return node.value;
        }
        return -1;
    }
    public void put(int key, int value){
        if(hashMap.containsKey(key)){
            Node node = hashMap.get(key);
            node.value = value;
            list.moveNodeToTail(node);
        }else{
            Node node = new Node(key, value);
            hashMap.put(key, node);
            list.addNode(node);
            if(hashMap.size() == capacity+1){
                // 删除缓存
                removeLRUcache();
            }
        }
    }
    public void removeLRUcache(){
        Node node = list.removeHead();
        hashMap.remove(node.key);
    }
}

public class Solution {
    /**
     * lru design
     * @param operators int整型二维数组 the ops
     * @param k int整型 the k
     * @return int整型一维数组
     */
    public int[] LRU (int[][] operators, int k) {
        // write code here
        ArrayList<Integer> arrayList = new ArrayList<>();
        LRUcache cache = new LRUcache(k);
        int n = operators.length;
        for(int i=0; i<n; i++){
            if(operators[i][0] == 1){
                int key = operators[i][1];
                int value = operators[i][2];
                cache.put(key, value);
            }else{
                int key = operators[i][1];
                int ans = cache.get(key);
                arrayList.add(ans);
            }
        }
        int[] ans = new int[arrayList.size()];
        for(int i=0; i<arrayList.size(); i++){
            ans[i] = arrayList.get(i);
        }
        return ans;
    }
}

排序

归并排序
// 归并排序
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 将给定数组排序
     * @param arr int整型一维数组 待排序的数组
     * @return int整型一维数组
     */
    public void process(int[] arr, int L, int R){
        if(L >= R){
            return;
        }
        int mid = L + ((R-L)>>1);
        process(arr, L, mid);
        process(arr, mid+1, R);
        merge(arr, L, mid, R);
    }
    public void merge(int[] arr, int L, int mid, int R){
        int[] help = new int[R-L+1];
        int p1 = L;
        int p2 = mid + 1;
        int index = 0;
        while(p1<=mid && p2<=R){
            help[index++] = arr[p1]<=arr[p2] ? arr[p1++] : arr[p2++];
        }
        while(p1<=mid){
            help[index++] = arr[p1++];
        }
        while(p2<=R){
            help[index++] = arr[p2++];
        }
        for(int i=0; i<R-L+1; i++){
            arr[L+i] = help[i];
        }
    }
    public int[] MySort (int[] arr) {
        // write code here
        int n = arr.length;
        process(arr, 0, n-1);
        return arr;
    }
}
堆排序
// 堆排序
import java.util.*;

public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 将给定数组排序
     * @param arr int整型一维数组 待排序的数组
     * @return int整型一维数组
     */
    public void heapInsert(int[] arr, int index){
        while(arr[index] > arr[(index-1)/2]){
            swap(arr, index, (index-1)/2);
            index = (index-1)/2;
        }
    }
    public void heapify(int[] arr, int index, int heapSize){
        int left = index*2+1;
        while(left < heapSize){
            int large = left+1 < heapSize && arr[left+1] > arr[left]
                ? left + 1
                : left;
            large = arr[large] > arr[index] ? large : index;
            if(large == index){
                break;
            }
            swap(arr, index, large);
            index = large;
            left = index*2+1;
        }
    }
    public void swap(int[] arr, int m, int n){
        int tmp = arr[m];
        arr[m] = arr[n];
        arr[n] = tmp;
    }
    public int[] MySort (int[] arr) {
        // write code here
        int n = arr.length;
        for(int i=0; i<n; i++){
            heapInsert(arr, i);
        }
        swap(arr, 0, --n);
        while(n>0){
            heapify(arr, 0, n);
            swap(arr, 0, --n);
        }
        return arr;
    }
}

二叉树先序中序后序遍历

递归实现
// 递归
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    ArrayList<Integer> pre = new ArrayList<>();
    ArrayList<Integer> in = new ArrayList<>();
    ArrayList<Integer> pos = new ArrayList<>();
    
    public void preOrder(TreeNode node){
        if(node == null){
            return;
        }
        
        pre.add(node.val);
        preOrder(node.left);
        preOrder(node.right);
    }
    public void inOrder(TreeNode node){
        if(node == null){
            return;
        }
        
        inOrder(node.left);
        in.add(node.val);
        inOrder(node.right);
    }
    public void posOrder(TreeNode node){
        if(node == null){
            return;
        }
        
        posOrder(node.left);
        posOrder(node.right);
        pos.add(node.val);
    }
    public int[][] threeOrders (TreeNode root) {
        // write code here
        preOrder(root);
        inOrder(root);
        posOrder(root);
        int n = pre.size();
        int[][] ans = new int[3][n];
        for(int i=0; i<n; i++){
            ans[0][i] = pre.get(i);
        }
        for(int i=0; i<n; i++){
            ans[1][i] = in.get(i);
        }
        for(int i=0; i<n; i++){
            ans[2][i] = pos.get(i);
        }
        return ans;
    }
}
非递归实现
// 非递归
import java.util.*;

/*
 * public class TreeNode {
 *   int val = 0;
 *   TreeNode left = null;
 *   TreeNode right = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param root TreeNode类 the root of binary tree
     * @return int整型二维数组
     */
    ArrayList<Integer> pre = new ArrayList<>();
    ArrayList<Integer> in = new ArrayList<>();
    ArrayList<Integer> pos = new ArrayList<>();
    
    public void preOrder(TreeNode node){
        Stack<TreeNode> stack = new Stack<>();
        stack.push(node);
        while(!stack.isEmpty()){
            node = stack.pop();
            pre.add(node.val);
            if(node.right != null){
                stack.push(node.right);
            }
            if(node.left != null){
                stack.push(node.left);
            }
        }
    }
    public void inOrder(TreeNode node){
        Stack<TreeNode> stack = new Stack<>();
        while(!stack.isEmpty() || node!=null){
            if(node != null){
                stack.push(node);
                node = node.left;
            }else{
                node = stack.pop();
                in.add(node.val);
                node = node.right;
            }
        }
    }
    public void posOrder(TreeNode node){
        Stack<TreeNode> stack1 = new Stack<>();
        Stack<TreeNode> stack2 = new Stack<>();
        stack1.push(node);
        while(!stack1.isEmpty()){
            node = stack1.pop();
            stack2.push(node);
            if(node.left != null){
                stack1.push(node.left);
            }
            if(node.right != null){
                stack1.push(node.right);
            }
        }
        while(!stack2.isEmpty()){
            node = stack2.pop();
            pos.add(node.val);
        }
    }
    public int[][] threeOrders (TreeNode root) {
        // write code here
        preOrder(root);
        inOrder(root);
        posOrder(root);
        int n = pre.size();
        int[][] ans = new int[3][n];
        for(int i=0; i<n; i++){
            ans[0][i] = pre.get(i);
        }
        for(int i=0; i<n; i++){
            ans[1][i] = in.get(i);
        }
        for(int i=0; i<n; i++){
            ans[2][i] = pos.get(i);
        }
        return ans;
    }
}

最小的k个数

排序实现
import java.util.ArrayList;
import java.util.*;

public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        Arrays.sort(input);
        ArrayList<Integer> ans = new ArrayList<>();
        for(int i=0; i<k; i++){
            ans.add(input[i]);
        }
        return ans;
    }
}
大根堆(优先级队列)
import java.util.ArrayList;
import java.util.*;

public class Solution {
    public ArrayList<Integer> GetLeastNumbers_Solution(int [] input, int k) {
        ArrayList<Integer> array = new ArrayList<>();
        if(k == 0){
            return array;
        }
        PriorityQueue<Integer> queue = new PriorityQueue<Integer>(new Comparator<Integer>(){
            public int compare(Integer o1, Integer o2){
                return o2-o1;
            }
        });
        for(int i=0; i<k; i++){
            queue.offer(input[i]);
        }
        for(int i=k; i<input.length; i++){
            if(queue.peek() > input[i]){
                queue.poll();
                queue.offer(input[i]);
            }
        }
        for(int i=0; i<k; i++){
            array.add(queue.poll());
        }
        return array;
    }
}