LeetCode题解(十)0900-0999,深入浅出Android

Write a class RecentCounter to count recent requests.

It has only one method: ping(int t), where t represents some time in milliseconds.

Return the number of pings that have been made from 3000 milliseconds ago until now.

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; }
    
  • }

*/

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:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

移动架构师

系统学习技术大纲

一线互联网Android面试题总结含详解(初级到高级专题)

image

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

img-Kv7hVqqJ-1712783180861)]

[外链图片转存中…(img-usdy9rmI-1712783180861)]

[外链图片转存中…(img-mvlNNido-1712783180862)]

[外链图片转存中…(img-gC0VugFi-1712783180862)]

[外链图片转存中…(img-noRUzMOu-1712783180862)]

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

文末

架构师不是天生的,是在项目中磨练起来的,所以,我们学了技术就需要结合项目进行实战训练,那么在Android里面最常用的架构无外乎 MVC,MVP,MVVM,但是这些思想如果和模块化,层次化,组件化混和在一起,那就不是一件那么简单的事了,我们需要一个真正身经百战的架构师才能讲解透彻其中蕴含的深理。

[外链图片转存中…(img-h9PwUG9V-1712783180862)]

[外链图片转存中…(img-FuZYitjy-1712783180863)]

一线互联网Android面试题总结含详解(初级到高级专题)

[外链图片转存中…(img-gEvSSeEq-1712783180863)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值