Skip to content

Commit bb1efac

Browse files
authored
Added task 35.
1 parent 25aee2f commit bb1efac

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package s0035_search_insert_position;
2+
3+
public class Solution {
4+
public int searchInsert(int[] nums, int target) {
5+
int lo = 0;
6+
int hi = nums.length - 1;
7+
while (lo <= hi) {
8+
int mid = lo + (hi - lo) / 2;
9+
if (target == nums[mid]) {
10+
return mid;
11+
} else if (target < nums[mid]) {
12+
hi = mid - 1;
13+
} else if (target > nums[mid]) {
14+
lo = mid + 1;
15+
}
16+
}
17+
return lo;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package s0035_search_insert_position;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.Test;
7+
8+
public class SolutionTest {
9+
@Test
10+
public void searchInsert() {
11+
assertThat(new Solution().searchInsert(new int[] {1, 3, 5, 6}, 5), equalTo(2));
12+
}
13+
}

0 commit comments

Comments
 (0)