Skip to content

Commit e8066bc

Browse files
committed
feat: add 035
1 parent d5d83fc commit e8066bc

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
|26|[Remove Duplicates from Sorted Array][026]|Array, Two Pointers|
1717
|27|[Remove Element][027]|Array, Two Pointers|
1818
|28|[Implement strStr()][028]|Two Pointers, String|
19-
|38|[Count and Say][028]|String|
19+
|35|[Search Insert Position][035]|String|
20+
|38|[Count and Say][038]|String|
2021
|58|[Length of Last Word][058]|String|
2122

2223

@@ -52,5 +53,6 @@
5253
[026]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/026/README.md
5354
[027]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/027/README.md
5455
[028]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/028/README.md
56+
[035]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/035/README.md
5557
[038]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/038/README.md
5658
[058]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/058/README.md

note/007/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ The input is assumed to be a 32-bit signed integer. Your function should **retur
2929

3030
## 思路
3131

32-
题意是给你一个整型数,求它的逆序整型数,而且有个小坑点,当它的逆序整型数溢出的话,那么就返回0
33-
,用我们代码表示的话可以求得结果保存在long中,最后把结果和整型的两个范围比较即可。
32+
题意是给你一个整型数,求它的逆序整型数,而且有个小坑点,当它的逆序整型数溢出的话,那么就返回0,用我们代码表示的话可以求得结果保存在long中,最后把结果和整型的两个范围比较即可。
3433

3534
``` java
3635
public class Solution {

note/008/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ If no valid conversion could be performed, a zero value is returned. If the corr
2525

2626
## 思路
2727

28-
题目大意就是把一个字符串转为整型,但要注意所给的要求,先去除最前面的空格,然后判断正负数,注意正数可能包含`+`,如果之后存在非数字或全为空则返回`0`,而如果合法的值超过int表示的最大范围,则根据正负号返回`INT_MAX``INT_MIN`
28+
题意是把一个字符串转为整型,但要注意所给的要求,先去除最前面的空格,然后判断正负数,注意正数可能包含`+`,如果之后存在非数字或全为空则返回`0`,而如果合法的值超过int表示的最大范围,则根据正负号返回`INT_MAX``INT_MIN`
2929

3030
``` java
3131
public class Solution {

note/035/README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# [Search Insert Position][title]
2+
3+
## Description
4+
5+
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
6+
7+
You may assume no duplicates in the array.
8+
9+
Here are few examples.
10+
11+
`[1,3,5,6]`, 5 → 2
12+
`[1,3,5,6]`, 2 → 1
13+
`[1,3,5,6]`, 7 → 4
14+
`[1,3,5,6]`, 0 → 0
15+
16+
**Tags:** Array, Binary Search
17+
18+
19+
## 思路
20+
21+
题意是让你从一个没有重复元素的已排序数组中找到插入位置的索引。因为数组已排序,所以我们可以想到二分查找法,因为查找到的条件是找到第一个等于或者大于`target`的元素的位置,所以二分法略作变动即可。
22+
23+
``` java
24+
public class Solution {
25+
public int searchInsert(int[] nums, int target) {
26+
int left = 0, right = nums.length - 1, mid = (right + left) >> 1;
27+
while (left <= right) {
28+
if (target <= nums[mid]) right = mid - 1;
29+
else left = mid + 1;
30+
mid = (right + left) >> 1;
31+
}
32+
return left;
33+
}
34+
}
35+
```
36+
37+
38+
## 结语
39+
40+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
41+
42+
43+
44+
[title]: https://leetcode.com/problems/search-insert-position
45+
[ajl]: https://github.com/Blankj/awesome-java-leetcode
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.blankj.easy._035;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/04/21
8+
* desc :
9+
* </pre>
10+
*/
11+
12+
public class Solution {
13+
public int searchInsert(int[] nums, int target) {
14+
int left = 0, right = nums.length - 1, mid = (right + left) >> 1;
15+
while (left <= right) {
16+
if (target <= nums[mid]) right = mid - 1;
17+
else left = mid + 1;
18+
mid = (right + left) >> 1;
19+
}
20+
return left;
21+
}
22+
23+
public static void main(String[] args) {
24+
Solution solution = new Solution();
25+
int[] nums = new int[]{1, 3, 5, 6};
26+
System.out.println(solution.searchInsert(nums, 5));
27+
System.out.println(solution.searchInsert(nums, 2));
28+
System.out.println(solution.searchInsert(nums, 7));
29+
System.out.println(solution.searchInsert(nums, 0));
30+
}
31+
}

0 commit comments

Comments
 (0)