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

Appearance settings

Latest commit

 

History

History
History
23 lines (21 loc) · 845 Bytes

File metadata and controls

23 lines (21 loc) · 845 Bytes
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* LC#35. Search Insert Position
* link:https://leetcode-cn.com/problems/search-insert-position/
* 题目非常简单,看到是有序数组,最先想到的就是 O(n) 的解法,CASE也能顺利通过,不过解答方式似乎不是最优,感觉这道题作者想考察的方向应该是给一个有序数组,通过二分查找达到 O(lon N) 的时间解答效率
*/
public class S35 {
public static int searchInsert(int[] nums, int target) {
for (int i = 0; i < nums.length; i++) {
if(nums[i] >= target){
return i;
}
}
return nums.length;
}
public static void main(String[] args) {
int[] nums = {1,3,5,6};
int target = 7;
int index = S35.searchInsert(nums, target);
System.out.println("index = " + index);
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.