Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(leetcode), #8 study binary search problems #9

Merged
merged 2 commits into from
May 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions leetcode/src/main/java/com/github/lucaseo90/easy/No0035.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.lucaseo90.easy;

public class No0035 {
public int searchInsert(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
int mid;

while (low <= high) {
mid = low + (high - low) / 2;

if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return low;
}
}
24 changes: 24 additions & 0 deletions leetcode/src/main/java/com/github/lucaseo90/easy/No0704.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.lucaseo90.easy;

public class No0704 {

public int search(int[] nums, int target) {
int low = 0;
int high = nums.length - 1;
int mid;

while (low <= high) {
mid = low + (high - low) / 2;

if (nums[mid] == target) {
return mid;
} else if (nums[mid] > target) {
high = mid - 1;
} else {
low = mid + 1;
}
}

return -1;
}
}
53 changes: 53 additions & 0 deletions leetcode/src/test/java/com/github/lucaseo90/easy/No0035Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.github.lucaseo90.easy;

import org.junit.Assert;
import org.junit.Test;

public class No0035Test {

No0035 no0035 = new No0035();

@Test
public void testExample01() {
Assert.assertEquals(2, no0035.searchInsert(new int[]{1, 3, 5, 6}, 5));
}

@Test
public void testExample02() {
Assert.assertEquals(1, no0035.searchInsert(new int[]{1, 3, 5, 6}, 2));
}

@Test
public void testExample03() {
Assert.assertEquals(4, no0035.searchInsert(new int[]{1, 3, 5, 6}, 7));
}

@Test
public void testUserDefinedExample01() {
Assert.assertEquals(4, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 9));
}

@Test
public void testUserDefinedExample02() {
Assert.assertEquals(2, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 2));
}

@Test
public void testUserDefinedExample03() {
Assert.assertEquals(0, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, -2));
Assert.assertEquals(0, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, -1));
Assert.assertEquals(2, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 1));
Assert.assertEquals(2, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 2));
Assert.assertEquals(2, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 3));
Assert.assertEquals(3, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 4));
Assert.assertEquals(3, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 5));
Assert.assertEquals(4, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 6));
Assert.assertEquals(4, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 7));
Assert.assertEquals(4, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 8));
Assert.assertEquals(4, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 9));
Assert.assertEquals(5, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 10));
Assert.assertEquals(5, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 11));
Assert.assertEquals(5, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 12));
Assert.assertEquals(6, no0035.searchInsert(new int[]{-1, 0, 3, 5, 9, 12}, 13));
}
}
31 changes: 31 additions & 0 deletions leetcode/src/test/java/com/github/lucaseo90/easy/No0704Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.github.lucaseo90.easy;

import org.junit.Assert;
import org.junit.Test;

public class No0704Test {

No0704 no0704 = new No0704();

@Test
public void testExample01() {
Assert.assertEquals(4, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 9));
}

@Test
public void testExample02() {
Assert.assertEquals(-1, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 2));
}

@Test
public void testUserDefinedExample01() {
Assert.assertEquals(-1, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 2));
Assert.assertEquals(0, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, -1));
Assert.assertEquals(1, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 0));
Assert.assertEquals(2, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 3));
Assert.assertEquals(3, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 5));
Assert.assertEquals(4, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 9));
Assert.assertEquals(5, no0704.search(new int[]{-1, 0, 3, 5, 9, 12}, 12));
}

}