Skip to content

Commit d8ddb8a

Browse files
committed
add leetcode: offer-21-57-58
1 parent 7d2bfd6 commit d8ddb8a

4 files changed

+93
-17
lines changed

.gitignore

+19-17
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,22 @@ build/
3636

3737

3838
# Mobile Tools for Java (J2ME)
39-
.mtj.tmp/
40-
.logs/*
41-
**/*.iml
42-
# Package Files #
43-
*.jar
44-
**/*.jar
45-
*.war
46-
*.ear
47-
target
48-
.idea
49-
.DS_Store
50-
exports
51-
# virtual machine crash logs, see
52-
http://www.java.com/en/download/help/error_hotspot.xml
53-
hs_err_pid*
54-
.tags
55-
.tags_sorted_by_file
39+
.mtj.tmp/
40+
.logs/*
41+
**/*.iml
42+
# Package Files #
43+
*.jar
44+
**/*.jar
45+
*.war
46+
*.ear
47+
target
48+
.idea
49+
.DS_Store
50+
exports
51+
# virtual machine crash logs, see
52+
http://www.java.com/en/download/help/error_hotspot.xml
53+
hs_err_pid*
54+
.tags
55+
.tags_sorted_by_file
56+
57+
Algorithm/src/leetCode/subject/number1801_1850/test.java
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package offer;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* @author : CodeWater
7+
* @create :2022-07-05-16:23
8+
* @Function Description :21.调整数组顺序使奇数位于偶数前面
9+
*/
10+
public class _21ReorderTheArraySoThatTheOddNumberPrecedesTheEvenNumber {
11+
12+
@Test
13+
public void exchange() {
14+
int[] nums = {1,2,3,4};
15+
int temp;
16+
for (int i = 0, j = nums.length - 1; i < j; ) {
17+
// 不加i<j,ij会越过去,导致可能刚交换的又变回原样。这里加了i<j之后,ij就是在相等的时候会退出
18+
while (i < j && (nums[i] % 2 == 1)) i++;
19+
while (i < j && (nums[j] % 2 == 0)) j--;
20+
21+
temp = nums[i];
22+
nums[i] = nums[j];
23+
nums[j] = temp;
24+
}
25+
26+
// return nums;
27+
}
28+
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-05-16:38
6+
* @Function Description :57. 和为s的两个数字
7+
*/
8+
public class _57AndTwoNumbersOfS {
9+
class Solution {
10+
public int[] twoSum(int[] nums, int target) {
11+
for( int i = 0 , j = nums.length - 1 ; i < j ; ){
12+
if( nums[i] + nums[j] > target ) j--;
13+
else if( nums[i] + nums[j] < target ) i++;
14+
else if( nums[i] + nums[j] == target ){
15+
return new int[]{ nums[i] , nums[j] };
16+
17+
}
18+
19+
}
20+
return new int[0];
21+
}
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-05-16:56
6+
* @Function Description :58.翻转单词顺序
7+
*/
8+
public class _58_1FlipWordOrder {
9+
class Solution {
10+
public String reverseWords(String s) {
11+
// trim取出首尾的空格
12+
String[] str = s.trim().split(" ");
13+
StringBuilder res = new StringBuilder();
14+
for( int i = str.length - 1 ; i >= 0 ; i-- ){
15+
if(str[i].equals("")) continue;
16+
res.append(str[i] + " " );
17+
18+
}
19+
return res.toString().trim();
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)