Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit cfbce0e

Browse files
新增题解
1 parent 9419280 commit cfbce0e

File tree

34 files changed

+311
-518
lines changed

34 files changed

+311
-518
lines changed

‎README.md‎

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

‎codes/java/leetcodes/src/main/java/com/hit/basinfo/sort_algorithm/QuickSort.java‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ public class QuickSort {
1414
*/
1515
public void quickSort(int[] nums) {
1616
// check parameters
17-
if (nums == null || nums.length < 2) {
18-
return;
19-
}
17+
if (nums == null || nums.length < 2) return;
2018
// initial value
2119
int left = 0, right = nums.length - 1;
2220
// call overload method
@@ -32,13 +30,9 @@ public void quickSort(int[] nums) {
3230
*/
3331
private void quickSort(int[] nums, int left, int right) {
3432
// check parameters
35-
if (nums == null || nums.length < 2) {
36-
return;
37-
}
38-
33+
if (nums == null || nums.length < 2) return;
3934
// obtain the division datum point, which is the left value after the division
4035
int index = partition(nums, left, right);
41-
4236
// sort left half
4337
if (left < index - 1) {
4438
quickSort(nums, left, index - 1);
@@ -60,7 +54,6 @@ private void quickSort(int[] nums, int left, int right) {
6054
private int partition(int[] nums, int left, int right) {
6155
// the position in the middle of the default array is the reference point
6256
int pivot = nums[(left + right) / 2];
63-
6457
/**
6558
* when left < = right, the cycle condition is satisfied
6659
* when left > right, it means that the current trip is completed
@@ -74,7 +67,6 @@ private int partition(int[] nums, int left, int right) {
7467
while (nums[right] > pivot) {
7568
right--;
7669
}
77-
7870
// find the elements that meet the conditions and exchange them
7971
if (left <= right) {
8072
// exchange element

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@
5151
*/
5252
public class _48 {
5353
public void rotate(int[][] matrix) {
54-
int s = 0, e = matrix.length - 1;
55-
while (s < e) {
56-
int[] temp = matrix[s];
57-
matrix[s] = matrix[e];
58-
matrix[e] = temp;
59-
s++;
60-
e--;
54+
int start = 0, end = matrix.length - 1;
55+
while (start < end) {
56+
int[] temp = matrix[start];
57+
matrix[start] = matrix[end];
58+
matrix[end] = temp;
59+
start++;
60+
end--;
6161
}
62-
6362
for (int i = 0; i < matrix.length; i++) {
6463
for (int j = i + 1; j < matrix[i].length; j++) {
6564
int temp = matrix[i][j];

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,7 @@
2525
* Explanation: In this case, no transaction is done, i.e. max profit = 0.
2626
*/
2727
public class _121 {
28-
2928
public int maxProfit(int[] prices) {
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) {
4229
int minPrice = Integer.MAX_VALUE;
4330
int maxProfit = 0;
4431
for (int price : prices) {

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,12 @@
2525
*/
2626
public class _198 {
2727
public int rob(int[] nums) {
28-
int[][] dp = new int[nums.length + 1][2];
29-
for (int i = 1; i <= nums.length; i++) {
30-
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]);
31-
dp[i][1] = nums[i - 1] + dp[i - 1][0];
28+
int prevMax = 0, currMax = 0;
29+
for (int n : nums) {
30+
int temp = currMax;
31+
currMax = Math.max(prevMax + n, currMax);
32+
prevMax = temp;
3233
}
33-
return Math.max(dp[nums.length][0], dp[nums.length][1]);
34+
return currMax;
3435
}
3536
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
*/
5353
public class _13 {
5454
public int romanToInt(String s) {
55-
intnums[] = new int[s.length()];
55+
int[] nums = new int[s.length()];
5656
for (int i = 0; i < s.length(); i++) {
5757
switch (s.charAt(i)) {
5858
case 'M':
@@ -80,10 +80,11 @@ public int romanToInt(String s) {
8080
}
8181
int sum = 0;
8282
for (int i = 0; i < nums.length - 1; i++) {
83-
if (nums[i] < nums[i + 1])
83+
if (nums[i] < nums[i + 1]) {
8484
sum -= nums[i];
85-
else
85+
} else {
8686
sum += nums[i];
87+
}
8788
}
8889
return sum + nums[nums.length - 1];
8990
}

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

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,9 @@ public class _412 {
3939
public List<String> fizzBuzz(int n) {
4040
// ans list
4141
List<String> ans = new ArrayList<String>();
42-
4342
for (int num = 1; num <= n; num++) {
44-
4543
boolean divisibleBy3 = (num % 3 == 0);
4644
boolean divisibleBy5 = (num % 5 == 0);
47-
4845
if (divisibleBy3 && divisibleBy5) {
4946
// Divides by both 3 and 5, add FizzBuzz
5047
ans.add("FizzBuzz");
@@ -59,78 +56,63 @@ public List<String> fizzBuzz(int n) {
5956
ans.add(Integer.toString(num));
6057
}
6158
}
62-
6359
return ans;
6460
}
6561

6662
public List<String> fizzBuzz2(int n) {
6763
// ans list
6864
List<String> ans = new ArrayList<String>();
69-
7065
for (int num = 1; num <= n; num++) {
71-
7266
boolean divisibleBy3 = (num % 3 == 0);
7367
boolean divisibleBy5 = (num % 5 == 0);
74-
7568
String numAnsStr = "";
76-
7769
if (divisibleBy3) {
7870
// Divides by 3, add Fizz
7971
numAnsStr += "Fizz";
8072
}
81-
8273
if (divisibleBy5) {
8374
// Divides by 5, add Buzz
8475
numAnsStr += "Buzz";
8576
}
86-
8777
if (numAnsStr.equals("")) {
8878
// Not divisible by 3 or 5, add the number
8979
numAnsStr += Integer.toString(num);
9080
}
91-
9281
// Append the current answer str to the ans list
9382
ans.add(numAnsStr);
9483
}
95-
9684
return ans;
9785
}
9886

9987
public List<String> fizzBuzz3(int n) {
10088
// ans list
10189
List<String> ans = new ArrayList<String>();
102-
10390
// Hash map to store all fizzbuzz mappings.
10491
HashMap<Integer, String> fizzBizzDict =
10592
new HashMap<Integer, String>() {
10693
private static final long serialVersionUID = 7966206953841139705L;
94+
10795
{
10896
put(3, "Fizz");
10997
put(5, "Buzz");
11098
}
11199
};
112-
113100
for (int num = 1; num <= n; num++) {
114101
StringBuilder numAnsStr = new StringBuilder();
115-
116102
for (Integer key : fizzBizzDict.keySet()) {
117-
118103
// If the num is divisible by key,
119104
// then add the corresponding string mapping to current numAnsStr
120105
if (num % key == 0) {
121106
numAnsStr.append(fizzBizzDict.get(key));
122107
}
123108
}
124-
125109
if (numAnsStr.toString().equals("")) {
126110
// Not divisible by 3 or 5, add the number
127111
numAnsStr.append(Integer.toString(num));
128112
}
129-
130113
// Append the current answer str to the ans list
131114
ans.add(numAnsStr.toString());
132115
}
133-
134116
return ans;
135117
}
136118
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,10 @@
2626
*/
2727
public class _461 {
2828
public int hammingDistance(int x, int y) {
29-
int xor = x ^ y, count = 0;
30-
for (int i = 0; i < 32; i++) {
31-
count += (xor >> i) & 1;
29+
int curr = x ^ y, count = 0;
30+
while (curr != 0) {
31+
count++;
32+
curr = curr & (curr - 1);
3233
}
3334
return count;
3435
}

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/math/_149.java‎

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,9 @@
4242
*/
4343
public class _149 {
4444
public int maxPoints(int[][] points) {
45-
if(points.length<=2) return points.length;
46-
int max=0;
47-
int localMax=0;
48-
45+
if (points.length <= 2) return points.length;
46+
int max = 0;
47+
int localMax;
4948
for (int[] point : points) {
5049
HashMap<String, Integer> map = new HashMap<>();
5150
int overlap = 0;
@@ -67,13 +66,11 @@ public int maxPoints(int[][] points) {
6766
localMax += overlap;
6867
max = Math.max(localMax, max);
6968
}
70-
7169
return max;
7270
}
7371

74-
private int gcd(int x, int y){
75-
if(y==0) return x;
76-
77-
return gcd(y,x%y);
72+
private int gcd(int x, int y) {
73+
if (y == 0) return x;
74+
return gcd(y, x % y);
7875
}
7976
}

‎codes/java/leetcodes/src/main/java/com/hit/basmath/interview/top_interview_questions/hard_collection/math/_179.java‎

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,12 @@
2222
*/
2323
public class _179 {
2424
public String largestNumber(int[] nums) {
25-
if (nums == null || nums.length == 0)
26-
return "";
27-
25+
if (nums == null || nums.length == 0) return "";
2826
// Convert int array to String array, so we can sort later on
29-
String[] s_num = new String[nums.length];
30-
for (int i = 0; i < nums.length; i++)
31-
s_num[i] = String.valueOf(nums[i]);
32-
27+
String[] strs = new String[nums.length];
28+
for (int i = 0; i < nums.length; i++) {
29+
strs[i] = String.valueOf(nums[i]);
30+
}
3331
// Comparator to decide which string should come first in concatenation
3432
Comparator<String> comp = new Comparator<String>() {
3533
@Override
@@ -39,15 +37,11 @@ public int compare(String str1, String str2) {
3937
return s2.compareTo(s1); // reverse order here, so we can do append() later
4038
}
4139
};
42-
43-
Arrays.sort(s_num, comp);
40+
Arrays.sort(strs, comp);
4441
// An extreme edge case by lc, say you have only a bunch of 0 in your int array
45-
if (s_num[0].charAt(0) == '0')
46-
return "0";
47-
42+
if (strs[0].charAt(0) == '0') return "0";
4843
StringBuilder sb = new StringBuilder();
49-
for (String s : s_num)
50-
sb.append(s);
44+
for (String s : strs) sb.append(s);
5145

5246
return sb.toString();
5347
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /