Stay button --- 2020.3.11

1013. The array is divided into three equal portions and

class Solution {
    public boolean canThreePartsEqualSum(int[] A) {
        int sum = 0,res = 0,cnt = 0;
        for(int a : A){
            sum += a;
        }
        if(sum % 3!=0) return false;
        sum = sum/3;
        for(int a : A){
            res += a;
            if(res == sum){
                cnt++;
                res=0;
            }
        }
        return cnt==3 || (cnt > 3 && sum == 0);
    }
}
class Solution {
    public boolean canThreePartsEqualSum(int[] A) {
        int sum = 0;
        for(int i : A){
            sum += i;
        }
        if(sum%3 != 0){
            // 总和不是3的倍数,直接返回false
            return false;
        }

        // 使用双指针,从数组两头开始一起找,节约时间
        int left = 0;
        int leftSum = A[left];
        int right = A.length - 1;
        int rightSum = A[right];

        // 使用left + 1 < right 的原因,防止只能将数组分成两个部分
        // 例如:[1,-1,1,-1],使用left < right作为判断条件就会出错
        while(left + 1 < right){
            if(leftSum == sum/3 && rightSum == sum/3){
                // 左右两边都等于 sum/3 ,中间也一定等于
                return true;
            }
            if(leftSum != sum/3){
                // left = 0赋予了初值,应该先left++,在leftSum += A[left];
                leftSum += A[++left];
            }
            if(rightSum != sum/3){
                // right = A.length - 1 赋予了初值,应该先right--,在rightSum += A[right];
                rightSum += A[--right];
            }
        }
        return false;  
    }
}

1304. N unique zero and integer

class Solution {
    public int[] sumZero(int n) {
        int sum = 0;
        int[] res = new int[n];
        for(int i=0;i<n-1;i++){
            res[i] = i;
            sum += i;
        }
        res[n-1] = -sum;
        return res;
    }
}
class Solution {
    public int[] sumZero(int n) {
        int[] ans = new int[n];
        int index = 0;
        for (int i = 1; i <= n / 2; i++) {
            ans[index++] = -i;
            ans[index++] = i;
        }
        return ans;
    }
}

1305. All elements of two binary search tree

//遍历+优先队列
//PriorityQueue使用跟普通队列一样,唯一区别是PriorityQueue会根据排序规则决定谁在队头,谁在队尾。
class Solution {
   private PriorityQueue<Integer> priorityQueue;

    private void dfs(TreeNode root) {
        if (root == null) {
            return;
        }

        priorityQueue.offer(root.val);
        dfs(root.left);
        dfs(root.right);
    }

    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        priorityQueue = new PriorityQueue<>();
        dfs(root1);
        dfs(root2);
        List<Integer> ansList = new ArrayList<>();
        while (!priorityQueue.isEmpty()) {
            ansList.add(priorityQueue.poll());
        }
        return ansList;
    }
}
class Solution {
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
        List<Integer> list = new ArrayList<>();
        getAllElements(root1, list);
        getAllElements(root2, list);
        Collections.sort(list);
        return list;
    }

    public void getAllElements(TreeNode node, List<Integer> list) {
        if (node == null) {
            return;
        }
        getAllElements(node.left, list);
        list.add(node.val);
        getAllElements(node.right, list);
    }
}

The more you know, the more you do not know.
Proper way without surgery, patients can still seek, there is no way to surgery, ending surgery.
If you have other questions, welcome message, we can discuss, learn together and progress together

He published 193 original articles · won praise 116 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_40722827/article/details/104805472
Recommended