forked from yehongzhi/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
35.搜索插入位置.java
59 lines (58 loc) · 1.46 KB
/
35.搜索插入位置.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/*
* @lc app=leetcode.cn id=35 lang=java
*
* [35] 搜索插入位置
*/
// @lc code=start
class Solution {
/**
* 执行结果:
*
* 62/62 cases passed (1 ms)
* Your runtime beats 7.54 % of java submissions
* Your memory usage beats 31.81 % of java submissions (38.9 MB)
*/
public int searchInsert(int[] nums, int target) {
if (nums == null || nums.length == 0) {
return 0;
}
// 如果数组长度等于1,特殊处理
if (nums.length == 1) {
int num = nums[0];
if (target < num || target == num) {
return 0;
} else {
return 1;
}
}
int num;
int next;
for (int i = 0; i < nums.length; i++) {
if (i == nums.length - 1) {
if (nums[i] == target) {
return i;
} else {
return i + 1;
}
}
num = nums[i];
if (num == target) {
return i;
}
next = nums[i + 1];
if (target < next) {
if (i == 0) {
if (target < num) {
return 0;
} else {
return i + 1;
}
} else {
return i + 1;
}
}
}
return -1;
}
}
// @lc code=end