Skip to content

Commit fe6be61

Browse files
committed
feat: update solutions to leetcode problem: No.0035
1 parent 3e92cbd commit fe6be61

File tree

8 files changed

+219
-181
lines changed

8 files changed

+219
-181
lines changed

solution/0000-0099/0035.Search Insert Position/README.md

+84-1
Original file line numberDiff line numberDiff line change
@@ -37,22 +37,105 @@
3737

3838
<!-- 这里可写通用的实现逻辑 -->
3939

40+
二分查找。
41+
4042
<!-- tabs:start -->
4143

4244
### **Python3**
4345

4446
<!-- 这里可写当前语言的特殊实现逻辑 -->
4547

4648
```python
47-
49+
class Solution:
50+
def searchInsert(self, nums: List[int], target: int) -> int:
51+
l, h = 0, len(nums) - 1
52+
while l <= h:
53+
m = l + ((h - l) >> 1)
54+
if nums[m] == target:
55+
return m
56+
if nums[m] < target:
57+
l = m + 1
58+
else:
59+
h = m - 1
60+
return l
4861
```
4962

5063
### **Java**
5164

5265
<!-- 这里可写当前语言的特殊实现逻辑 -->
5366

5467
```java
68+
class Solution {
69+
public int searchInsert(int[] nums, int target) {
70+
int l = 0, h = nums.length - 1;
71+
while (l <= h) {
72+
int m = l + ((h - l) >> 1);
73+
if (nums[m] == target) return m;
74+
if (nums[m] < target) l = m + 1;
75+
else h = m - 1;
76+
}
77+
return l;
78+
}
79+
}
80+
```
81+
82+
### **Go**
83+
84+
```go
85+
func searchInsert(nums []int, target int) int {
86+
l, h := 0, len(nums) - 1
87+
for l <= h {
88+
m := l + ((h - l) >> 1)
89+
if nums[m] == target {
90+
return m
91+
}
92+
if nums[m] < target {
93+
l = m + 1
94+
} else {
95+
h = m - 1
96+
}
97+
}
98+
return l
99+
}
100+
```
101+
102+
### **C++**
103+
104+
```cpp
105+
class Solution {
106+
public:
107+
int searchInsert(vector<int>& nums, int target) {
108+
int l = 0, h = nums.size() - 1;
109+
while (l <= h) {
110+
int m = l + ((h - l) >> 1);
111+
if (nums[m] == target) return m;
112+
if (nums[m] < target) l = m + 1;
113+
else h = m - 1;
114+
}
115+
return l;
116+
}
117+
};
118+
```
55119
120+
### **JavaScript**
121+
122+
```js
123+
/**
124+
* @param {number[]} nums
125+
* @param {number} target
126+
* @return {number}
127+
*/
128+
var searchInsert = function (nums, target) {
129+
let l = 0,
130+
h = nums.length;
131+
while (l <= h) {
132+
const m = l + ((h - l) >> 1);
133+
if (nums[m] == target) return m;
134+
if (nums[m] < target) l = m + 1;
135+
else h = m - 1;
136+
}
137+
return l;
138+
};
56139
```
57140

58141
### **...**

solution/0000-0099/0035.Search Insert Position/README_EN.md

+82-1
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,94 @@
5555
### **Python3**
5656

5757
```python
58-
58+
class Solution:
59+
def searchInsert(self, nums: List[int], target: int) -> int:
60+
l, h = 0, len(nums) - 1
61+
while l <= h:
62+
m = l + ((h - l) >> 1)
63+
if nums[m] == target:
64+
return m
65+
if nums[m] < target:
66+
l = m + 1
67+
else:
68+
h = m - 1
69+
return l
5970
```
6071

6172
### **Java**
6273

6374
```java
75+
class Solution {
76+
public int searchInsert(int[] nums, int target) {
77+
int l = 0, h = nums.length - 1;
78+
while (l <= h) {
79+
int m = l + ((h - l) >> 1);
80+
if (nums[m] == target) return m;
81+
if (nums[m] < target) l = m + 1;
82+
else h = m - 1;
83+
}
84+
return l;
85+
}
86+
}
87+
```
88+
89+
### **Go**
90+
91+
```go
92+
func searchInsert(nums []int, target int) int {
93+
l, h := 0, len(nums) - 1
94+
for l <= h {
95+
m := l + ((h - l) >> 1)
96+
if nums[m] == target {
97+
return m
98+
}
99+
if nums[m] < target {
100+
l = m + 1
101+
} else {
102+
h = m - 1
103+
}
104+
}
105+
return l
106+
}
107+
```
108+
109+
### **C++**
110+
111+
```cpp
112+
class Solution {
113+
public:
114+
int searchInsert(vector<int>& nums, int target) {
115+
int l = 0, h = nums.size() - 1;
116+
while (l <= h) {
117+
int m = l + ((h - l) >> 1);
118+
if (nums[m] == target) return m;
119+
if (nums[m] < target) l = m + 1;
120+
else h = m - 1;
121+
}
122+
return l;
123+
}
124+
};
125+
```
64126
127+
### **JavaScript**
128+
129+
```js
130+
/**
131+
* @param {number[]} nums
132+
* @param {number} target
133+
* @return {number}
134+
*/
135+
var searchInsert = function (nums, target) {
136+
let l = 0,
137+
h = nums.length;
138+
while (l <= h) {
139+
const m = l + ((h - l) >> 1);
140+
if (nums[m] == target) return m;
141+
if (nums[m] < target) l = m + 1;
142+
else h = m - 1;
143+
}
144+
return l;
145+
};
65146
```
66147

67148
### **...**
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,13 @@
11
class Solution {
22
public:
33
int searchInsert(vector<int>& nums, int target) {
4-
int len = nums.size();
5-
if(len == 0){
6-
nums.push_back(target);
7-
return 0;
4+
int l = 0, h = nums.size() - 1;
5+
while (l <= h) {
6+
int m = l + ((h - l) >> 1);
7+
if (nums[m] == target) return m;
8+
if (nums[m] < target) l = m + 1;
9+
else h = m - 1;
810
}
9-
10-
auto iter = find(nums.begin(),nums.end(),target);
11-
12-
if(iter != nums.end()){
13-
return binarySearch(nums,0,len - 1,target);
14-
}
15-
else{
16-
int slow = 0;
17-
int fast = 1;
18-
19-
if(nums[0] >= target)return 0;
20-
if(nums[len-1] <= target)return len;
21-
22-
while(fast < len){
23-
if(nums[slow] <= target && nums[fast] > target){
24-
nums.insert(nums.begin() + fast,target);
25-
return fast;
26-
}
27-
else{
28-
slow++;
29-
fast++;
30-
}
31-
}
32-
return fast;
33-
}
34-
}
35-
36-
37-
int binarySearch(vector<int> &nums,int left,int right,int target){
38-
if(nums[left] == target)return left;
39-
if(nums[right] == target)return right;
40-
41-
int mid = (left + right) / 2;
42-
43-
if(nums[mid] > target)return binarySearch(nums,left+1,mid,target);
44-
else if(nums[mid] < target)return binarySearch(nums,mid,right-1,target);
45-
else return mid;
46-
}
47-
48-
};
49-
50-
-------------------------
51-
class Solution {
52-
public:
53-
int searchInsert(vector<int>& nums, int target) {
54-
if (nums.size() == 0) {
55-
nums.push_back(target);
56-
return 0;
57-
}
58-
unsigned int i = 0;
59-
60-
while(i<nums.size()&&nums[i]<target)i++;
61-
62-
nums.insert(nums.begin() + i, target);
63-
return i;
11+
return l;
6412
}
6513
};
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
func searchInsert(nums []int, target int) int {
2-
left, right := 0, len(nums)
3-
for left < right {
4-
mid := (left + right) >> 1
5-
if nums[mid] >= target {
6-
right = mid
7-
} else {
8-
left = mid + 1
9-
}
10-
}
11-
return left
12-
}
2+
l, h := 0, len(nums) - 1
3+
for l <= h {
4+
m := l + ((h - l) >> 1)
5+
if nums[m] == target {
6+
return m
7+
}
8+
if nums[m] < target {
9+
l = m + 1
10+
} else {
11+
h = m - 1
12+
}
13+
}
14+
return l
15+
}
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,12 @@
11
class Solution {
22
public int searchInsert(int[] nums, int target) {
3-
if(nums.length == 0){
4-
return 0;
5-
}
6-
if(nums.length == 1){
7-
if(nums[0] < target){
8-
return 1;
9-
} else {
10-
return 0;
11-
}
12-
}
13-
for(int i = 0;i < nums.length;i++){
14-
if(nums[i] == target){
15-
return i;
16-
} else {
17-
int s = Math.min(nums[i],target);
18-
if(s == target){
19-
return i;
20-
}
21-
}
22-
}
23-
return nums.length;
24-
}
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;
3+
int l = 0, h = nums.length - 1;
4+
while (l <= h) {
5+
int m = l + ((h - l) >> 1);
6+
if (nums[m] == target) return m;
7+
if (nums[m] < target) l = m + 1;
8+
else h = m - 1;
9+
}
10+
return l;
4911
}
50-
}
51-
*/
12+
}

0 commit comments

Comments
 (0)