LeetCode 118th Weekly Contest 总结

这次周赛是在做🐎呢??
状态不好,思路不清晰,反思反思

  • 薄弱项:math类题;tree类题不熟练(最近做的少);array类题有时候比较灵活的时候想不到

970. Powerful Integers

题意

Given two non-negative integers x and y, an integer is powerful if it is equal to x^i + y^j for some integers i >= 0 and j >= 0.
Return a list of all powerful integers that have value less than or equal to bound.
You may return the answer in any order. In your answer, each value should occur at most once.

思路

周赛时被这道题坑了好久哦;开始一直在纠结开n次方,结果是因为corner case !注意x = 1y = 1的情况!

代码

class Solution {
    public List<Integer> powerfulIntegers(int x, int y, int bound) {
        Set<Integer> res = new HashSet<>();
        if (x == 1 && y == 1) {
            for (int i = 1; i<= bound; i++) {
                if (x + y == i) res.add(i);
            }
            return new ArrayList<>(res);
        }
        if (x == 1) {
            int i = 0;
            for (; (int)Math.pow(y, i) <= bound - 1; i++) {
                res.add((int)Math.pow(y, i) + 1);
            }
            return new ArrayList<>(res);
        }
        if (y == 1) {
            int i = 0;
            for (; (int)Math.pow(x, i) <= bound - 1; i++) {
                res.add((int)Math.pow(x, i) + 1);
            }
            return new ArrayList<>(res);
        }
        int i = 0, j = 0;
        for (; (int)Math.pow(x, i) + (int)Math.pow(y, j) <= bound; i++, j = 0) {
            for (; (int)Math.pow(x, i) + (int)Math.pow(y, j) <= bound; j++) {
                res.add((int)Math.pow(x, i) + (int)Math.pow(y, j));
            }
        }
        return new ArrayList<>(res);
    }
}

969. Pancake Sorting

题意

Given an array A, we can perform a pancake flip: We choose some positive integer k <= A.length, then reverse the order of the first k elements of A. We want to perform zero or more pancake flips (doing them one after another in succession) to sort the array A.

Return the k-values corresponding to a sequence of pancake flips that sort A. Any valid answer that sorts the array within 10 * A.length flips will be judged as correct.

煎饼排序,要求每次输出交换顺序的前k个

思路

  • 类似与选择排序:每次都保证最大的数在它应该在的位置
    需要进行反转两次:维护一个curMaxIndex, curEndIndex
    • 1) 找到[0, curEndIndex]最大的数,反转开始到最大的数,使得最大的数位于位置0
    • 2) 反转[0, curEndIndex]使得最大的数位于curEndIndex,则最大的数就位于它应该在的位置了

代码

class Solution {
    public List<Integer> pancakeSort(int[] A) {
        List<Integer> result = new ArrayList<>();
        int len = A.length;
        for (int i = len; i > 0; i--) {
            int k = getMax(A, i - 1);
            if (k == i - 1) continue;
            if (k != 0) flip(A, k, result);
            flip(A, i - 1, result);
        }
        return result;
    }
    
    private void flip(int[] A, int k, List<Integer> result) {
        int mid = k / 2;
        for (int i = 0; i <= mid; i++) {
            swap(A, i, k - i);
        }
        result.add(k + 1);
    }

    private void swap(int[] A, int i, int j) {
        int t = A[i];
        A[i] = A[j];
        A[j] = t;
    }
    
    private int getMax(int[] A, int len) {
        int max = A[0], maxIndex = 0;
        for (int i = 0; i <= len; i++) {
            if (A[i] > max) {
                max = A[i];
                maxIndex = i;
            }
        }
        return maxIndex;
    }
}


971. Flip Binary Tree To Match Preorder Traversal

题意

Given a binary tree with N nodes, each node has a different value from {1, ..., N}.
A node in this binary tree can be flipped by swapping the left child and the right child of that node.
Consider the sequence of N values reported by a preorder traversal starting from the root. Call such a sequence of N values the voyage of the tree.

(Recall that a preorder traversal of a node means we report the current node's value, then preorder-traverse the left child, then preorder-traverse the right child.)

Our goal is to flip the least number of nodes in the tree so that the voyage of the tree matches the voyage we are given.>If we can do so, then return a list of the values of all nodes flipped. You may return the answer in any order.
If we cannot do so, then return the list [-1].

思路

  • 反转二叉树某节点的左右孩子,以使得匹配前序遍历序列
    只有当当前节点的左孩子存在,且不等于当前的前序位置的值,则要交换这个节点

代码

class Solution {
    List<Integer> res = new ArrayList<>();
    int index = 0;
    public List<Integer> flipMatchVoyage(TreeNode root, int[] voyage) {
        return helper(root, voyage) ? res : Arrays.asList(-1);
    }
    
    private boolean helper(TreeNode node, int[] A) {
        if (node == null) return true;
        if (node.val != A[index++]) return false;
        if (node.left != null && node.left.val != A[index]) {
            res.add(node.val);
            return helper(node.right, A) & helper(node.left, A);
        }
        return helper(node.left, A) & helper(node.right, A);
    }
}

总结:

这次周赛环境不是很稳定,自己思路也不清晰,成绩不好

posted @ 2019-01-07 11:51  shawshawwan  阅读(178)  评论(0编辑  收藏  举报