LeetCode题解(十)0900-0999,flutter自定义弹窗

Any ping with time in [t - 3000, t] will count, including the current ping.

It is guaranteed that every call to ping uses a strictly larger value of t than before.

Example 1:

Input: inputs = [“RecentCounter”,“ping”,“ping”,“ping”,“ping”], inputs = [[],[1],[100],[3001],[3002]]

Output: [null,1,2,3,3]

Note:

Each test case will have at most 10000 calls to ping.

Each test case will call ping with strictly increasing values of t.

Each call to ping will have 1 <= t <= 10^9.

[Answer]

Runtime: 71 ms, faster than 87.90% of Java online submissions for Number of Recent Calls.

Memory Usage: 66.5 MB, less than 31.28% of Java online submissions for Number of Recent Calls.

class RecentCounter {

Queue q;

public RecentCounter() {

q = new LinkedList();

}

public int ping(int t) {

q.add(t);

while (q.peek() < t - 3000)

q.poll();

return q.size();

}

}

/**

  • Your RecentCounter object will be instantiated and called as such:

  • RecentCounter obj = new RecentCounter();

  • int param_1 = obj.ping(t);

*/

【Queue 队列】

Queue: 基本上,一个队列就是一个先入先出(FIFO)的数据结构

Queue接口与List、Set同一级别,都是继承了Collection接口。LinkedList实现了Deque接 口。

阻塞队列的操作:

add 增加一个元索 如果队列已满,则抛出一个IIIegaISlabEepeplian异常

remove 移除并返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常

element 返回队列头部的元素 如果队列为空,则抛出一个NoSuchElementException异常

offer 添加一个元素并返回true 如果队列已满,则返回false

poll 移除并返问队列头部的元素 如果队列为空,则返回null

peek 返回队列头部的元素 如果队列为空,则返回null

put 添加一个元素 如果队列满,则阻塞

take 移除并返回队列头部的元素 如果队列为空,则阻塞

938. Range Sum of BST

[Description]

Given the root node of a binary search tree, return the sum of values of all nodes with value between L and R (inclusive).

The binary search tree is guaranteed to have unique values.

[Example]

Example 1:

Input: root = [10,5,15,3,7,null,18], L = 7, R = 15

10

/

5 15

/ \

3 7 18

Output: 32

Example 2:

Input: root = [10,5,15,3,7,13,18,1,null,6], L = 6, R = 10

Output: 23

Note:

The number of nodes in the tree is at most 10000.

The final answer is guaranteed to be less than 2^31.

[Answer]

Runtime: 1 ms, faster than 61.11% of Java online submissions for Range Sum of BST.

Memory Usage: 43.5 MB, less than 99.80% of Java online submissions for Range Sum of BST.

/**

  • Definition for a binary tree node.

  • public class TreeNode {

  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) { val = x; }
    
《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

浏览器打开:qq.cn.hn/FTe 开源分享
  • }

*/

class Solution {

public int rangeSumBST(TreeNode root, int L, int R) {

int sum = 0;

if(root.val >= L && root.val <= R)

sum += root.val;

if (root.left != null)

sum += rangeSumBST(root.left, L, R);

if (root.right != null)

sum += rangeSumBST(root.right, L, R);

return sum;

}

}

Input:

[15,9,21,7,13,19,23,5,null,11,null,17]

19

21

Expect:40

/**

  • Definition for a binary tree node.

  • public class TreeNode {

  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) { val = x; }
    
  • }

*/

class Solution {

public int rangeSumBST(TreeNode root, int L, int R) {

int sum = 0;

if(root.val >= L && root.val <= R)

sum += root.val;

if (root.left != null) {

if (root.left.val == L)

sum += root.left.val;

else

sum += rangeSumBST(root.left, L, R);

}

if (root.right != null) {

if (root.right.val == R)

sum += root.right.val;

else

sum += rangeSumBST(root.right, L, R);

}

return sum;

}

}

942. DI String Match

[Description]

Given a string S that only contains “I” (increase) or “D” (decrease), let N = S.length.

Return any permutation A of [0, 1, …, N] such that for all i = 0, …, N-1:

If S[i] == “I”, then A[i] < A[i+1]

If S[i] == “D”, then A[i] > A[i+1]

Example 1:

Input: “IDID”

Output: [0,4,1,3,2]

Example 2:

Input: “III”

Output: [0,1,2,3]

Example 3:

Input: “DDI”

Output: [3,2,0,1]

Note:

1 <= S.length <= 10000

S only contains characters “I” or “D”.

[Answer]

Runtime: 70 ms, faster than 5.28% of Java online submissions for DI String Match.

Memory Usage: 39.6 MB, less than 5.03% of Java online submissions for DI String Match.

class Solution {

public int[] diStringMatch(String S) {

int numMin = 0;

int numMax = S.length();

int[] result = new int[S.length() + 1];

for (int i = 0; i < S.toCharArray().length; i++) {

if (S.charAt(i) == ‘I’) {

result[i] = numMin;

numMin++;

} else if (S.charAt(i) == ‘D’) {

result[i] = numMax;

numMax–;

}

}

result[S.length()] = numMax;

return result;

}

}

944. Delete Columns to Make Sorted

[Description]

We are given an array A of N lowercase letter strings, all of the same length.

Now, we may choose any set of deletion indices, and for each string, we delete all the characters in those indices.

For example, if we have an array A = [“abcdef”,“uvwxyz”] and deletion indices {0, 2, 3}, then the final array after deletions is [“bef”, “vyz”], and the remaining columns of A are [“b”,“v”], [“e”,“y”], and [“f”,“z”]. (Formally, the c-th column is [A[0][c], A[1][c], …, A[A.length-1][c]].)

Suppose we chose a set of deletion indices D such that after deletions, each remaining column in A is in non-decreasing sorted order.

Return the minimum possible value of D.length.

Example 1:

Input: [“cba”,“daf”,“ghi”]

Output: 1

Explanation:

After choosing D = {1}, each column [“c”,“d”,“g”] and [“a”,“f”,“i”] are in non-decreasing sorted order.

If we chose D = {}, then a column [“b”,“a”,“h”] would not be in non-decreasing sorted order.

Example 2:

Input: [“a”,“b”]

Output: 0

Explanation: D = {}

Example 3:

Input: [“zyx”,“wvu”,“tsr”]

Output: 3

Explanation: D = {0, 1, 2}

Note:

1 <= A.length <= 100

1 <= A[i].length <= 1000

[Answer]

Runtime: 216 ms, faster than 5.03% of Java online submissions for Delete Columns to Make Sorted.

Memory Usage: 42.5 MB, less than 5.11% of Java online submissions for Delete Columns to Make Sorted.

class Solution {

public int minDeletionSize(String[] A) {

int minDeleteSize = 0;

int itemLength = 0;

if (A == null || A.length == 0)

return 0;

else

itemLength = A[0].trim().length();

for (int i = 0; i < itemLength; i++) {

for (int j = 0; j < A.length - 1; j++) {

if (A[j].toCharArray()[i] > A[j + 1].toCharArray()[i]) {

minDeleteSize++;

break;

}

}

}

return minDeleteSize;

}

}

961. N-Repeated Element in Size 2N Array

[Description]

In a array A of size 2N, there are N+1 unique elements, and exactly one of these elements is repeated N times.

Return the element repeated N times.

Example 1:

Input: [1,2,3,3]

Output: 3

Example 2:

Input: [2,1,2,5,3,2]

Output: 2

Example 3:

Input: [5,1,5,2,5,3,5,4]

Output: 5

Note:

4 <= A.length <= 10000

0 <= A[i] < 10000

A.length is even

[Answer]

Runtime: 1 ms, faster than 82.35% of Java online submissions for N-Repeated Element in Size 2N Array.

Memory Usage: 38.9 MB, less than 99.16% of Java online submissions for N-Repeated Element in Size 2N Array.

class Solution {

public int repeatedNTimes(int[] A) {

Map<Integer, Integer> map = new HashMap<>();

for (int a : A) {

map.put(a, map.getOrDefault(a, 0) + 1);

if (map.get(a) > 1)

return a;

}

return 0;

}

}

Runtime: 1 ms, faster than 82.35% of Java online submissions for N-Repeated Element in Size 2N Array.

Memory Usage: 38.7 MB, less than 99.28% of Java online submissions for N-Repeated Element in Size 2N Array.

class Solution {

public int repeatedNTimes(int[] A) {

int length = A.length;

int[] counter = new int[10000];

for (int i = 0; i < length; i++) {

counter[A[i]]++;

if (counter[A[i]] > 1)

return A[i];

}

for (int i = 0; i < 10000; i++) {

if (counter[i] > 1)

return i;

}

return 0;

}

}

Runtime: 2 ms, faster than 57.36% of Java online submissions for N-Repeated Element in Size 2N Array.

Memory Usage: 39.2 MB, less than 98.92% of Java online submissions for N-Repeated Element in Size 2N Array.

class Solution {

public int repeatedNTimes(int[] A) {

int length = A.length;

int[] counter = new int[10000];

for (int i = 0; i < length; i++) {

counter[A[i]]++;

}

for (int i = 0; i < 10000; i++) {

if (counter[i] > 1)

return i;

}

return 0;

}

}

Runtime: 3 ms, faster than 54.59% of Java online submissions for N-Repeated Element in Size 2N Array.

Memory Usage: 37.8 MB, less than 99.82% of Java online submissions for N-Repeated Element in Size 2N Array.

class Solution {

public int repeatedNTimes(int[] A) {

int length = A.length;

int[] counter = new int[10000];

for (int i = 0; i < length; i++) {

counter[A[i]]++;

}

for (int i = 0; i < 10000; i++) {

if (counter[i] == length / 2 && counter[i] % (length / 2) == 0)

return i;

}

return 0;

}

}

965. Univalued Binary Tree

A binary tree is univalued if every node in the tree has the same value.

Return true if and only if the given tree is univalued.

Example 1:

1

/

1 1

/ \

1 1 1

Input: [1,1,1,1,1,null,1]

Output: true

Example 2:

2

/

2 2

/ \

5 2

Input: [2,2,2,5,2]

Output: false

[Answer]

Runtime: 0 ms, faster than 100.00% of Java online submissions for Univalued Binary Tree.

Memory Usage: 34.2 MB, less than 100.00% of Java online submissions for Univalued Binary Tree.

/**

  • Definition for a binary tree node.

  • public class TreeNode {

  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) { val = x; }
    
  • }

*/

class Solution {

public boolean isUnivalTree(TreeNode root) {

if (root == null || (root.left == null && root.right == null)) return true;

if (root.left == null)

return (root.val == root.right.val) && isUnivalTree(root.right);

else if (root.right == null)

return (root.val == root.left.val) && isUnivalTree(root.left);

else

return isUnivalTree(root.left) && isUnivalTree(root.right)

&& (root.val == root.left.val) && (root.val == root.right.val);

}

}

Input [1,2]

Output true

Expected false

/**

  • Definition for a binary tree node.

  • public class TreeNode {

  • int val;
    
  • TreeNode left;
    
  • TreeNode right;
    
  • TreeNode(int x) { val = x; }
    
  • }

*/

class Solution {

public boolean isUnivalTree(TreeNode root) {

if (root == null) return true;

if (root.left == null || root.right == null)

return isUnivalTree(root.left) && isUnivalTree(root.right);

else

return isUnivalTree(root.left) && isUnivalTree(root.right)

&& (root.val == root.left.val) && (root.val == root.right.val);

}

}

977. Squares of a Sorted Array

[Description]

Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.

Example 1:

Input: [-4,-1,0,3,10]

Output: [0,1,9,16,100]

Example 2:

Input: [-7,-3,2,3,11]

Output: [4,9,9,49,121]

Note:

1 <= A.length <= 10000

-10000 <= A[i] <= 10000

A is sorted in non-decreasing order.

[Answer]

经过调优的快速排序法

Runtime: 2 ms, faster than 80.62% of Java online submissions for Squares of a Sorted Array.

Memory Usage: 40.3 MB, less than 98.75% of Java online submissions for Squares of a Sorted Array.

class Solution {

public int[] sortedSquares(int[] A) {

for (int i = 0; i < A.length; i++) {

A[i] = A[i] * A[i];

}

Arrays.sort(A);

return A;

}

}

冒泡排序

Runtime: 233 ms, faster than 5.01% of Java online submissions for Squares of a Sorted Array.

Memory Usage: 41.7 MB, less than 94.93% of Java online submissions for Squares of a Sorted Array.

class Solution {

public int[] sortedSquares(int[] A) {

for (int i = 0; i < A.length; i++) {

A[i] = A[i] * A[i];

}

for (int i = 0; i < A.length - 1; i++) {

for (int j = 0; j < A.length - 1 - i; j++) {

if (A[j] > A[j + 1]) {

int temp = A[j];

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值