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

Sign up
Appearance settings

Commit 2986d67

Browse files
added leetcode questions based on Binary Search
1 parent f5ed84c commit 2986d67

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

‎Leetcode/Leetcode_278.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package Leetcode;
2+
3+
/* The isBadVersion API is defined in the parent class VersionControl.
4+
boolean isBadVersion(int version); */
5+
6+
//public class Solution extends VersionControl {
7+
// public int firstBadVersion(int n) {
8+
// int lb = 1, ub = n;
9+
// int p = 1;
10+
// while (lb <= ub) {
11+
// int mid = lb + (ub - lb) / 2;
12+
// boolean r = isBadVersion(mid);
13+
// if (r == true) {
14+
// p = mid;
15+
// ub = mid - 1;
16+
// } else {
17+
// lb = mid + 1;
18+
// }
19+
// }
20+
// return p;
21+
// }
22+
//}

‎Leetcode/Leetcode_35.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Leetcode;
2+
3+
public class Leetcode_35 {
4+
public int searchInsert(int[] nums, int target) {
5+
int lb = 0, ub = nums.length - 1;
6+
while (lb <= ub) {
7+
int mid = (lb + ub) / 2;
8+
if (nums[mid] < target) {
9+
lb = mid + 1;
10+
} else if (nums[mid] > target) {
11+
// p = mid;
12+
ub = mid -1;
13+
} else {
14+
return mid;
15+
}
16+
}
17+
return lb;
18+
}
19+
}

‎Leetcode/Leetcode_367.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package Leetcode;
2+
3+
public class Leetcode_367 {
4+
public static boolean isPerfectSquare(int num) {
5+
long l = 1, r = num;
6+
if (num == 1)
7+
return true;
8+
while (l <= r) {
9+
long mid = l + (r - l) / 2;
10+
if (mid * mid == num) {
11+
return true;
12+
} else if (mid * mid > num) {
13+
r = mid - 1;
14+
} else {
15+
l = mid + 1;
16+
}
17+
}
18+
return false;
19+
}
20+
21+
public static void main(String[] args) {
22+
System.out.println(isPerfectSquare(14));
23+
}
24+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /