国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

7th Feb: 刷題筆記

2019-11-09 19:36:07
字體:
供稿:網(wǎng)友

1/ Maximum Depth of Binary TreeGiven a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

/** * Definition for a binary tree node. * public class TreeNode { *     int val; *     TreeNode left; *     TreeNode right; *     TreeNode(int x) { val = x; } * } */public class Solution {    public int maxDepth(TreeNode root) {        if (root == null) {            return 0;        }                int left = maxDepth(root.left);        int right = maxDepth(root.right);        int ans = 1 + Math.max(left, right);                return ans;    }}

2/ Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.For example,Given [3,2,1,5,6,4] and k = 2, return 5.Note: You may assume k is always valid, 1 ≤ k ≤ array's length.

最簡單的做法:

public class Solution {    public int findKthLargest(int[] nums, int k) {        Arrays.sort(nums);        return nums[nums.length - k];    }}

或者利用PRiority Queue實(shí)現(xiàn)大頂堆,接著去到第k個(gè)。

快速排序的做法(明天補(bǔ)充):

3/ Minstack (見5th Feb的筆記)

4/  Implement strStr()Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

public class Solution {    public int strStr(String haystack, String needle) {        if (haystack == null || needle == null) {            return -1;        }                for (int i = 0; i < haystack.length() - needle.length() + 1; i++) {            int j = 0;            for (j = 0; j < needle.length(); j++) {                if (haystack.charAt(i + j) != needle.charAt(j)) {                    break;                }            }            if (j == needle.length()) {//錯(cuò)誤                    return i;            }        }        return -1;    }}這道題,之前把if寫在for循環(huán)里的話,就錯(cuò)了。

5/ Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane.

Each rectangle is defined by its bottom left corner and top right corner as shown in the figure.Assume that the total area is never beyond the maximum possible value of int.

public class Solution {    public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {                int areaOfSqrA = (C-A) * (D-B);         int areaOfSqrB = (G-E) * (H-F);                int left = Math.max(A, E);        int right = Math.min(G, C);        int bottom = Math.max(F, B);        int top = Math.min(D, H);                //If overlap        int overlap = 0;        if(right > left && top > bottom)             overlap = (right - left) * (top - bottom);                return areaOfSqrA + areaOfSqrB - overlap;    }}6/ Rotate ArrayRotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:

Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.

7/

10/ Given a map of task and its corresponding cooldown period(unordered_map<char, int> tasks), and a sequence of task(such as "ABCDABC"), all tasks are the unit running time and the machine can run a task one time. Return the total time needed to finish all the sequence tasks.

類似的LeetCode題目(Hard 難度,放棄):https://leetcode.com/problems/rearrange-string-k-distance-apart/

8/ 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 关岭| 安岳县| 新龙县| 和硕县| 彭泽县| 班戈县| 裕民县| 揭东县| 锡林浩特市| 长岭县| 广丰县| 龙州县| 江北区| 澎湖县| 靖安县| 锦屏县| 阿瓦提县| 蓬安县| 兴业县| 孟州市| 青岛市| 阜阳市| 泸西县| 阳东县| 屏南县| 海口市| 南华县| 万山特区| 长泰县| 阳朔县| 小金县| 平武县| 莫力| 尼木县| 凤山市| 博罗县| 大同市| 金山区| 葫芦岛市| 新疆| 松滋市|