-
Notifications
You must be signed in to change notification settings - Fork 0
/
035_SearchInsertPosition35.java
114 lines (98 loc) · 2.05 KB
/
035_SearchInsertPosition35.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
class Solution {
public int searchInsert(int[] nums, int target) {
int i = 0 ;
if(target==1 ||nums[i]==target){
return i;
}
if(target==0){
return 0;
}
if((nums.length-1)==0){
return i+1 ;
}
while(target>=nums[i]){
if(target==nums[i]){
return i ;
}
if(i==nums.length-1){
return i+1 ;
}
i++;
}
return i;
}
}
****************************************************************
class Solution {
public int searchInsert(int[] nums, int target) {
int i = 0, index = 0;
int n = nums.length;
for(i = 0; i < n; i++)
{
if(nums[i] == target)
{
index = i;
}
else
{
if(nums[i] < target)
{
index = i+1;
}
}
}
return index;
}
}
********************************************************************
class Solution {
public int searchInsert(int[] nums, int target) {
int i,a=-1;
for(i=0;i<nums.length;i++){
if(nums[i]==target)
a=i;
}
if(a==-1){
for(i=0;i<nums.length;i++){
if(nums[i]>target || i==nums.length-1){
if(i==(nums.length-1) && target>nums[i]){
a=i+1;
break;
}
else
a=i;
a=i;
break;
}
}
}
return a;
}
}
******************************************************************************
class Solution {
public int searchInsert(int[] nums, int target) {
for(int i=0;i<nums.length;i++)
if(nums[i]>=target)
return i;
return nums.length;
}
}
**********************************************************************
class Solution {
public int searchInsert(int[] nums, int target) {
int i=0;
for(i=0;i<nums.length;i++)
{
if(nums[i]==target)
{
return i;
}
if(nums[i]>target)
{
return i;
}
}
return i;
}
}