力扣---2020.9.28

carroll18發表於2020-12-17

117. 填充每個節點的下一個右側節點指標 II

class Solution {
    public Node connect(Node root) {
        if(root == null){
            return null;
        }
        if(root.left != null && root.right != null){
            root.left.next = root.right;
        }
        if(root.left != null && root.right == null){
            root.left.next = getnext(root.next);
        }
        if(root.right != null){
            root.right.next = getnext(root.next);
        }
        connect(root.right);
        connect(root.left);
        return root;
    }

    public Node getnext(Node root){
        if(root==null) return null;
        if(root.left != null){
            return root.left;
        }
        if(root.right != null){
            return root.right;
        }
        if(root.next != null){
            return getnext(root.next);
        }
        return null;
    }
}
class Solution {
    public Node connect(Node root) {
        if (root == null) {
            return null;
        }
        Queue<Node> queue = new LinkedList<Node>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            int n = queue.size();
            Node last = null;
            for (int i = 1; i <= n; ++i) {
                Node f = queue.poll();
                if (f.left != null) {
                    queue.offer(f.left);
                }
                if (f.right != null) {
                    queue.offer(f.right);
                }
                if (i != 1) {
                    last.next = f;
                }
                last = f;
            }
        }
        return root;
    }
}

581. 最短無序連續子陣列

public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int[] snums = nums.clone();
        Arrays.sort(snums);
        int start = snums.length, end = 0;
        for (int i = 0; i < snums.length; i++) {
            if (snums[i] != nums[i]) {
                start = Math.min(start, i);
                end = Math.max(end, i);
            }
        }
        return (end - start >= 0 ? end - start + 1 : 0);
    }
}
class Solution {
    public int findUnsortedSubarray(int[] nums) {
        Stack<Integer> stack = new Stack<Integer>();
        int min = nums.length;
        int max = 0;
        for(int i = 0;i < nums.length;i++){
            while(!stack.isEmpty() && nums[stack.peek()] > nums[i]){
                min = Math.min(min,stack.pop());
            }
            stack.push(i);
        }

        for(int i = nums.length - 1;i >= 0;i--){
            while(!stack.isEmpty() && nums[stack.peek()] < nums[i]){
                max = Math.max(max,stack.pop());
            }
            stack.push(i);
        }
        return max>=min ? (max-min+1):0;
    }
}
//大佬的程式碼
public class Solution {
    public int findUnsortedSubarray(int[] nums) {
        int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE;
        boolean flag = false;
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] < nums[i - 1])
                flag = true;
            if (flag)
                min = Math.min(min, nums[i]);
        }
        flag = false;
        for (int i = nums.length - 2; i >= 0; i--) {
            if (nums[i] > nums[i + 1])
                flag = true;
            if (flag)
                max = Math.max(max, nums[i]);
        }
        int l, r;
        for (l = 0; l < nums.length; l++) {
            if (min < nums[l])
                break;
        }
        for (r = nums.length - 1; r >= 0; r--) {
            if (max > nums[r])
                break;
        }
        return r - l < 0 ? 0 : r - l + 1;
    }
}

208. 實現 Trie (字首樹)

class Trie {

    class TireNode {
        private boolean isEnd;
        TireNode[] next;

        public TireNode() {
            isEnd = false;
            next = new TireNode[26];
        }
    }

    private TireNode root;

    public Trie() {
        root = new TireNode();
    }

    public void insert(String word) {
        TireNode node = root;
        for (char c : word.toCharArray()) {
            if (node.next[c - 'a'] == null) {
                node.next[c - 'a'] = new TireNode();
            }
            node = node.next[c - 'a'];
        }
        node.isEnd = true;
    }

    public boolean search(String word) {
        TireNode node = root;
        for (char c : word.toCharArray()) {
            node = node.next[c - 'a'];
            if (node == null) {
                return false;
            }
        }
        return node.isEnd;
    }

    public boolean startsWith(String prefix) {
        TireNode node = root;
        for (char c : prefix.toCharArray()) {
            node = node.next[c - 'a'];
            if (node == null) {
                return false;
            }
        }
        return true;
    }
}

你知道的越多,你不知道的越多。

相關文章