其他分享
首页 > 其他分享> > LeetCode题解(十)0900-0999,android实现文件下载

LeetCode题解(十)0900-0999,android实现文件下载

作者:互联网

return A;

}

}

922. Sort Array By Parity II

[Description]

Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even.

Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even.

You may return any answer array that satisfies this condition.

Example 1:

Input: [4,2,5,7]

Output: [4,5,2,7]

Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.

Note:

2 <= A.length <= 20000

A.length % 2 == 0

0 <= A[i] <= 1000

[Answer]

Runtime: 2 ms, faster than 99.72% of Java online submissions for Sort Array By Parity II.

Memory Usage: 41.7 MB, less than 76.76% of Java online submissions for Sort Array By Parity II.

class Solution {

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

int[] result = new int[A.length];

int indexOdd = 1;

int indexEven = 0;

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

if (A[i] % 2 == 0) {

result[indexEven] = A[i];

indexEven += 2;

} else {

result[indexOdd] = A[i];

indexOdd += 2;

}

}

return result;

}

}

929. Unique Email Addresses

[Description]

Every email consists of a local name and a domain name, separated by the @ sign.

For example, in alice@leetcode.com, alice is the local name, and leetcode.com is the domain name.

Besides lowercase letters, these emails may contain ‘.‘s or ‘+‘s.

If you add periods (’.’) between some characters in the local name part of an email address, mail sent there will be forwarded to the same address without dots in the local name. For example, “alice.z@leetcode.com” and “alicez@leetcode.com” forward to the same email address. (Note that this rule does not apply for domain names.)

If you add a plus (’+’) in the local name, everything after the first plus sign will be ignored. This allows certain emails to be filtered, for example m.y+name@email.com will be forwarded to my@email.com. (Again, this rule does not apply for domain names.)

It is possible to use both of these rules at the same time.

Given a list of emails, we send one email to each address in the list. How many different addresses actually receive mails?

Example 1:

Input: [“test.email+alex@leetcode.com”,“test.e.mail+bob.cathy@leetcode.com”,“testemail+david@lee.tcode.com”]

Output: 2

Explanation: “testemail@leetcode.com” and “testemail@lee.tcode.com” actually receive mails

Note:

1 <= emails[i].length <= 100

1 <= emails.length <= 100

Each emails[i] contains exactly one ‘@’ character.

All local and domain names are non-empty.

Local names do not start with a ‘+’ character.

[Answer]

Runtime: 26 ms, faster than 30.73% of Java online submissions for Unique Email Addresses.

Memory Usage: 38.4 MB, less than 97.90% of Java online submissions for Unique Email Addresses.

class Solution {

public int numUniqueEmails(String[] emails) {

HashSet set = new HashSet<>();

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

String[] split = emails[i].split("@");

String local = split[0].replace(".", “”);

if (local.contains("+"))

local = local.split("\+")[0];

emails[i] = local + “@” + split[1];

if (!set.contains(emails[i]))

set.add(emails[i]);

}

return set.size();

}

}

Input:[“test.email+alex@leetcode.com”,“test.email.leet+alex@code.com”]

Output:1

Expected:2

class Solution {

public int numUniqueEmails(String[] emails) {

HashSet set = new HashSet<>();

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

String[] split = emails[i].split("@");

String local = split[0].replace(".", “”);

if (local.contains("+"))

local = local.split("\+")[0];

emails[i] = local + split[1];

if (!set.contains(emails[i]))

set.add(emails[i]);

}

return set.size();

}

}

933. Number of Recent Calls

[Description]

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();

}

}

/**

*/

【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.

/**

*/

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

/**

*/

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

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

开源分享完整内容戳这里

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;

}

}

标签:return,int,题解,length,0900,online,android,root,than
来源: https://blog.csdn.net/m0_64319496/article/details/121399258