Skip to content

Commit 9fb745d

Browse files
committed
汉化
1 parent 46fded8 commit 9fb745d

File tree

75 files changed

+2145
-1631
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

75 files changed

+2145
-1631
lines changed

README.md

Lines changed: 1090 additions & 961 deletions
Large diffs are not rendered by default.

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/array/_122.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,53 @@
3535
*/
3636
public class _122 {
3737
public int maxProfit(int[] prices) {
38-
int profit = 0, i = 0;
39-
while (i < prices.length) {
40-
// find next local minimum
41-
while (i < prices.length - 1 && prices[i + 1] <= prices[i]) i++;
42-
// need increment to avoid infinite loop for "[1]"
43-
int min = prices[i++];
44-
// find next local maximum
45-
while (i < prices.length - 1 && prices[i + 1] >= prices[i]) i++;
46-
profit += i < prices.length ? prices[i++] - min : 0;
38+
return calculate(prices, 0);
39+
}
40+
41+
private int calculate(int prices[], int s) {
42+
if (s >= prices.length)
43+
return 0;
44+
int max = 0;
45+
for (int start = s; start < prices.length; start++) {
46+
int maxprofit = 0;
47+
for (int i = start + 1; i < prices.length; i++) {
48+
if (prices[start] < prices[i]) {
49+
int profit = calculate(prices, i + 1) + prices[i] - prices[start];
50+
if (profit > maxprofit)
51+
maxprofit = profit;
52+
}
53+
}
54+
if (maxprofit > max)
55+
max = maxprofit;
56+
}
57+
return max;
58+
}
59+
60+
public int maxProfit2(int[] prices) {
61+
if (prices == null || prices.length == 0) return 0;
62+
int i = 0;
63+
int valley = prices[0];
64+
int peak = prices[0];
65+
int maxprofit = 0;
66+
while (i < prices.length - 1) {
67+
while (i < prices.length - 1 && prices[i] >= prices[i + 1])
68+
i++;
69+
valley = prices[i];
70+
while (i < prices.length - 1 && prices[i] <= prices[i + 1])
71+
i++;
72+
peak = prices[i];
73+
maxprofit += peak - valley;
74+
}
75+
return maxprofit;
76+
}
77+
78+
public int maxProfit3(int[] prices) {
79+
int maxProfit = 0;
80+
for (int i = 1; i < prices.length; i++) {
81+
if (prices[i] > prices[i - 1]) {
82+
maxProfit += prices[i] - prices[i - 1];
83+
}
4784
}
48-
return profit;
85+
return maxProfit;
4986
}
5087
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/dynamic_programming/_121.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,29 @@
2525
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
2626
*/
2727
public class _121 {
28+
2829
public int maxProfit(int[] prices) {
29-
int maxCur = 0, maxSoFar = 0;
30-
for (int i = 1; i < prices.length; i++) {
31-
maxCur = Math.max(0, maxCur += prices[i] - prices[i - 1]);
32-
maxSoFar = Math.max(maxCur, maxSoFar);
30+
int maxProfit = 0;
31+
for (int i = 0; i < prices.length - 1; i++) {
32+
for (int j = i + 1; j < prices.length; j++) {
33+
int profit = prices[j] - prices[i];
34+
if (profit > maxProfit)
35+
maxProfit = profit;
36+
}
37+
}
38+
return maxProfit;
39+
}
40+
41+
public int maxProfit2(int[] prices) {
42+
int minPrice = Integer.MAX_VALUE;
43+
int maxProfit = 0;
44+
for (int price : prices) {
45+
if (price < minPrice) {
46+
minPrice = price;
47+
} else if (price - minPrice > maxProfit) {
48+
maxProfit = price - minPrice;
49+
}
3350
}
34-
return maxSoFar;
51+
return maxProfit;
3552
}
3653
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/dynamic_programming/_53.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,12 @@ public int maxSubArray(int[] nums) {
2828
dp[i] = nums[i] + (dp[i - 1] > 0 ? dp[i - 1] : 0);
2929
max = Math.max(max, dp[i]);
3030
}
31-
3231
return max;
3332
}
3433

3534
public int maxSubArray2(int[] nums) {
36-
int n = nums.length;
3735
int currSum = nums[0], maxSum = nums[0];
38-
39-
for (int i = 1; i < n; ++i) {
36+
for (int i = 1; i < nums.length; ++i) {
4037
currSum = Math.max(nums[i], currSum + nums[i]);
4138
maxSum = Math.max(maxSum, currSum);
4239
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/math/_204.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ public int countPrimes(int n) {
2323
}
2424
}
2525
}
26-
2726
return count;
2827
}
2928
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/others/_190.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,15 @@
2929
public class _190 {
3030
// you need treat n as an unsigned value
3131
public int reverseBits(int n) {
32-
int result = 0;
32+
int ans = 0;
3333
for (int i = 0; i < 32; i++) {
34-
result += n & 1;
35-
n >>>= 1; // CATCH: must do unsigned shift
36-
if (i < 31) // CATCH: for last digit, don't shift!
37-
result <<= 1;
34+
// handle the lowest bit of binary every time
35+
int curr = n & 1;
36+
// directly arrange the lowest bit of binary to the final position
37+
ans += (curr << (31 - i));
38+
// update n
39+
n = n >> 1;
3840
}
39-
return result;
41+
return ans;
4042
}
4143
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/others/_191.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.hit.basmath.interview.top_interview_questions.easy_collection.others;
1+
package com.hit.basmath.interview.top_interview_questions.easy_collection.others;
22

33
/**
44
* 191. Number of 1 Bits
@@ -37,11 +37,15 @@
3737
public class _191 {
3838
// you need to treat n as an unsigned value
3939
public int hammingWeight(int n) {
40-
int ones = 0;
40+
int sum = 0;
4141
while (n != 0) {
42-
ones = ones + (n & 1);
43-
n = n >>> 1;
42+
sum++;
43+
/**
44+
* n & (n - 1) always change the lowest 1 in n to 0
45+
* and keep the other bits unchanged
46+
*/
47+
n &= (n - 1);
4448
}
45-
return ones;
49+
return sum;
4650
}
4751
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/others/_268.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,10 @@
2121
*/
2222
public class _268 {
2323
public int missingNumber(int[] nums) {
24-
int xor = 0, i = 0;
25-
for (i = 0; i < nums.length; i++) {
26-
xor = xor ^ i ^ nums[i];
24+
int missing = nums.length;
25+
for (int i = 0; i < nums.length; i++) {
26+
missing ^= i ^ nums[i];
2727
}
28-
29-
return xor ^ i;
28+
return missing;
3029
}
3130
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/strings/_242.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.hit.basmath.interview.top_interview_questions.easy_collection.strings;
22

3+
import java.util.Arrays;
4+
35
/**
46
* 242. Valid Anagram
57
* <p>
@@ -25,10 +27,30 @@
2527
*/
2628
public class _242 {
2729
public boolean isAnagram(String s, String t) {
28-
int[] alphabet = new int[26];
29-
for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
30-
for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
31-
for (int i : alphabet) if (i != 0) return false;
30+
if (s.length() != t.length()) {
31+
return false;
32+
}
33+
char[] str1 = s.toCharArray();
34+
char[] str2 = t.toCharArray();
35+
Arrays.sort(str1);
36+
Arrays.sort(str2);
37+
return Arrays.equals(str1, str2);
38+
}
39+
40+
public boolean isAnagram2(String s, String t) {
41+
if (s.length() != t.length()) {
42+
return false;
43+
}
44+
int[] counter = new int[26];
45+
for (int i = 0; i < s.length(); i++) {
46+
counter[s.charAt(i) - 'a']++;
47+
counter[t.charAt(i) - 'a']--;
48+
}
49+
for (int count : counter) {
50+
if (count != 0) {
51+
return false;
52+
}
53+
}
3254
return true;
3355
}
3456
}

codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/easy_collection/strings/_7.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,21 @@ public int reverse(int x) {
4444
}
4545

4646
public int reverse2(int x) {
47-
int rev = 0;
48-
47+
int ans = 0;
4948
while (x != 0) {
5049
int pop = x % 10;
5150
x /= 10;
5251

53-
if (rev > Integer.MAX_VALUE / 10 ||
54-
(rev == Integer.MAX_VALUE / 10 && pop > 7)) {
52+
if (ans > Integer.MAX_VALUE / 10 ||
53+
(ans == Integer.MAX_VALUE / 10 && pop > 7)) {
5554
return 0;
5655
}
57-
58-
if (rev < Integer.MIN_VALUE / 10 ||
59-
(rev == Integer.MIN_VALUE / 10 && pop < -8)) {
56+
if (ans < Integer.MIN_VALUE / 10 ||
57+
(ans == Integer.MIN_VALUE / 10 && pop < -8)) {
6058
return 0;
6159
}
62-
rev = rev * 10 + pop;
60+
ans = ans * 10 + pop;
6361
}
64-
return rev;
62+
return ans;
6563
}
6664
}

0 commit comments

Comments
 (0)