Skip to content

Commit c3901ac

Browse files
committed
Update solution 035 [Java]
1 parent de5f485 commit c3901ac

File tree

3 files changed

+77
-18
lines changed

3 files changed

+77
-18
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Complete [solutions](https://github.com/doocs/leetcode/tree/master/solution) to
2727
| 020 | [Valid Parentheses](https://github.com/doocs/leetcode/tree/master/solution/020.Valid%20Parentheses) | `String`, `Stack` |
2828
| 021 | [Merge Two Sorted Lists](https://github.com/doocs/leetcode/tree/master/solution/021.Merge%20Two%20Sorted%20Lists) | `Linked List` |
2929
| 028 | [Implement strStr()](https://github.com/doocs/leetcode/tree/master/solution/028.Implement%20strStr()) | `Two Pointers`, `String` |
30+
| 035 | [Search Insert Position](https://github.com/doocs/leetcode/tree/master/solution/035.Search%20Insert%20Position) | `Array`, `Binary Search` |
3031
| 053 | [Maximum Subarray](https://github.com/doocs/leetcode/tree/master/solution/053.Maximum%20Subarray) | `Array`, `Divide and Conquer`, `Dynamic Programming` |
3132
| 070 | [Climbing Stairs](https://github.com/doocs/leetcode/tree/master/solution/070.Climbing%20Stairs) | `Dynamic Programming` |
3233
| 083 | [Remove Duplicates from Sorted List](https://github.com/doocs/leetcode/tree/master/solution/083.Remove%20Duplicates%20from%20Sorted%20List) | `Linked List` |
@@ -100,7 +101,7 @@ I'm looking for long-term contributors/partners to this repo! Send me [PRs](http
100101
" If you want to go fast, go alone. If you want to go far, go together. And that's the spirit of [teamwork](https://github.com/doocs/leetcode/graphs/contributors) ".
101102

102103
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
103-
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhkmxx9302013 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991)<br />[💻](https://github.com/doocs/leetcode/commits?author=MarkKuang1991 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian)<br />[💻](https://github.com/doocs/leetcode/commits?author=fonxian "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhanary "Code") </center> |
104-
|---|---|---|---|---|---|
104+
| <center> [<img src="https://avatars3.githubusercontent.com/u/21008209?v=4" width="80px;"/>](https://github.com/yanglbme)<br />[💻](https://github.com/doocs/leetcode/commits?author=yanglbme "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/23625436?v=4" width="80px;"/>](https://github.com/chakyam)<br />[💻](https://github.com/doocs/leetcode/commits?author=chakyam "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/10081554?v=4" width="80px;"/>](https://github.com/zhkmxx9302013)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhkmxx9302013 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/40383345?v=4" width="80px;"/>](https://github.com/MarkKuang1991)<br />[💻](https://github.com/doocs/leetcode/commits?author=MarkKuang1991 "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/12371194?v=4" width="80px;"/>](https://github.com/fonxian)<br />[💻](https://github.com/doocs/leetcode/commits?author=fonxian "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/25222367?v=4" width="80px;"/>](https://github.com/zhanary)<br />[💻](https://github.com/doocs/leetcode/commits?author=zhanary "Code") </center> | <center> [<img src="https://avatars3.githubusercontent.com/u/42396616?v=4" width="80px;"/>](https://github.com/ZhouTingZhaobiu)<br />[💻](https://github.com/doocs/leetcode/commits?author=ZhouTingZhaobiu "Code") </center> |
105+
|---|---|---|---|---|---|---|
105106

106107
<!-- ALL-CONTRIBUTORS-LIST:END -->

solution/035.Search Insertion Location/README.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,55 +6,87 @@
66

77
你可以假设数组中无重复元素。
88

9-
示例 1:
10-
9+
**示例 1:**
10+
```
1111
输入: [1,3,5,6], 5
1212
输出: 2
13-
示例 2:
13+
```
1414

15+
**示例 2:**
16+
```
1517
输入: [1,3,5,6], 2
1618
输出: 1
17-
示例 3:
19+
```
1820

21+
**示例 3:**
22+
```
1923
输入: [1,3,5,6], 7
2024
输出: 4
21-
示例 4:
25+
```
2226

27+
**示例 4:**
28+
```
2329
输入: [1,3,5,6], 0
2430
输出: 0
31+
```
2532

2633
### 解法
27-
首先判断传入的数组为0,1这样的长度
34+
首先判断传入的数组为 0,1 这样的长度。
2835

2936
因为是一个给定的排序数组,在循环时就可以判断是否存在的同时判断大小,有相同的则直接返回索引,
30-
不存在则判断大小,只要相较于当前索引的元素较小,则可以认为该目标数在数组中无对应元素,直接返回索引即可
37+
不存在则判断大小,只要相较于当前索引的元素较小,则可以认为该目标数在数组中无对应元素,直接返回索引即可
3138

32-
除此之外还可用二分法做解
39+
除此之外还可用二分法做解
3340

3441
```java
3542
class Solution {
3643
public int searchInsert(int[] nums, int target) {
37-
if(nums.length == 0){
44+
if(nums.length == 0) {
3845
return 0;
3946
}
40-
if(nums.length == 1){
41-
if(nums[0] < target){
47+
if(nums.length == 1) {
48+
if(nums[0] < target) {
4249
return 1;
4350
} else {
4451
return 0;
4552
}
4653
}
47-
for(int i = 0;i < nums.length;i++){
48-
if(nums[i] == target){
54+
for(int i = 0;i < nums.length;i++) {
55+
if(nums[i] == target) {
4956
return i;
5057
} else {
5158
int s = Math.min(nums[i],target);
52-
if(s == target){
59+
if(s == target) {
5360
return i;
5461
}
5562
}
5663
}
5764
return nums.length;
5865
}
5966
}
60-
```
67+
```
68+
69+
- 二分法
70+
```java
71+
class Solution {
72+
public int searchInsert(int[] nums, int target) {
73+
if (nums == null || nums.length == 0) {
74+
return 0;
75+
}
76+
int low = 0;
77+
int high = nums.length - 1;
78+
while (low <= high) {
79+
int mid = low + ((high - low) >> 1);
80+
if (nums[mid] == target) {
81+
return mid;
82+
}
83+
if (nums[mid] < target) {
84+
low = mid + 1;
85+
} else {
86+
high = mid - 1;
87+
}
88+
}
89+
return low;
90+
}
91+
}
92+
```

solution/035.Search Insertion Location/Solution.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,30 @@ public int searchInsert(int[] nums, int target) {
2222
}
2323
return nums.length;
2424
}
25-
}
25+
}
26+
27+
/*
28+
29+
// 二分法
30+
class Solution {
31+
public int searchInsert(int[] nums, int target) {
32+
if (nums == null || nums.length == 0) {
33+
return 0;
34+
}
35+
int low = 0;
36+
int high = nums.length - 1;
37+
while (low <= high) {
38+
int mid = low + ((high - low) >> 1);
39+
if (nums[mid] == target) {
40+
return mid;
41+
}
42+
if (nums[mid] < target) {
43+
low = mid + 1;
44+
} else {
45+
high = mid - 1;
46+
}
47+
}
48+
return low;
49+
}
50+
}
51+
*/

0 commit comments

Comments
 (0)