Skip to content

Commit 5a7facb

Browse files
committed
Added Binary Search Problems in Java
1 parent cbd04db commit 5a7facb

5 files changed

+171
-0
lines changed

Backtracking/Sudoku-Solver-37.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,39 @@
3434
// board[i][j] is a digit or '.'.
3535
// It is guaranteed that the input board has only one solution.
3636

37+
class Solution {
38+
public boolean isValid(char[][] board, int row, int col, char c){
39+
for(int i = 0; i < 9; i++){
40+
if(board[row][i] == c)
41+
return false;
42+
if(board[i][col] == c)
43+
return false;
44+
if(board[3*(row/3) + (i/3)][3*(col/3) + (i%3)] == c)
45+
return false;
46+
}
47+
return true;
48+
}
49+
50+
public boolean solve(char[][] board){
51+
for(int i = 0; i < board.length; i++){
52+
for(int j = 0; j < board[0].length; j++){
53+
if(board[i][j] == '.'){
54+
for(char c = '1'; c <= '9'; c++){
55+
if(isValid(board, i, j, c)){
56+
board[i][j] = c;
57+
if(solve(board)) return true;
58+
else board[i][j] = '.';
59+
}
60+
}
61+
return false;
62+
}
63+
}
64+
}
65+
return true;
66+
}
67+
68+
public void solveSudoku(char[][] board) {
69+
solve(board);
70+
}
71+
}
72+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// There is an integer array nums sorted in ascending order (with distinct values).
2+
// Prior to being passed to your function, nums is rotated at an unknown pivot index
3+
// k (0 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1],
4+
// ..., nums[n-1],
5+
// nums[0], nums[1], ..., nums[k-1]] (0-indexed).
6+
// For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2].
7+
8+
// Given the array nums after the rotation and an integer target, return the index of target
9+
// if it is in nums, or -1 if it is not in nums.
10+
// You must write an algorithm with O(log n) runtime complexity.
11+
12+
// Example 1:
13+
14+
// Input: nums = [4,5,6,7,0,1,2], target = 0
15+
// Output: 4
16+
// Example 2:
17+
18+
// Input: nums = [4,5,6,7,0,1,2], target = 3
19+
// Output: -1
20+
// Example 3:
21+
22+
// Input: nums = [1], target = 0
23+
// Output: -1
24+
25+
// Constraints:
26+
27+
// 1 <= nums.length <= 5000
28+
// -104 <= nums[i] <= 104
29+
// All values of nums are unique.
30+
// nums is guaranteed to be rotated at some pivot.
31+
// -104 <= target <= 104
32+
33+
class Solution {
34+
public int search(int[] nums, int target) {
35+
int start = 0, end = nums.length - 1, mid = 0;
36+
while(start <= end){
37+
mid = start + (end - start) / 2;
38+
if(nums[mid] == target)
39+
return mid;
40+
else if(nums[mid] >= nums[start]){
41+
if(nums[start] <= target && nums[mid] >= target)
42+
end = mid - 1;
43+
else start = mid + 1;
44+
}
45+
else if(nums[mid] <= nums[end]){
46+
if(target >= nums[mid] && target <= nums[end])
47+
start = mid + 1;
48+
else end = mid - 1;
49+
}
50+
}
51+
return -1;
52+
}
53+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// You are given a sorted array consisting of only integers where every element
2+
// appears exactly twice, except for one element which appears exactly once.
3+
// Find this single element that appears only once.
4+
5+
// Follow up: Your solution should run in O(log n) time and O(1) space.
6+
7+
// Example 1:
8+
9+
// Input: nums = [1,1,2,3,3,4,4,8,8]
10+
// Output: 2
11+
// Example 2:
12+
13+
// Input: nums = [3,3,7,7,10,11,11]
14+
// Output: 10
15+
16+
// Constraints:
17+
// 1 <= nums.length <= 10^5
18+
// 0 <= nums[i] <= 10^5
19+
class Solution {
20+
public int singleNonDuplicate(int[] nums) {
21+
int start = 0, end = nums.length - 1;
22+
if(end == 0) return nums[0];
23+
if(nums[0] != nums[1]) return nums[0];
24+
if(nums[end - 1] != nums[end]) return nums[end];
25+
while(start <= end){
26+
int mid = start + (end - start) / 2;
27+
if(nums[mid] != nums[mid + 1] && nums[mid] != nums[mid-1])
28+
return nums[mid];
29+
if(((mid % 2 == 0) && nums[mid] == nums[mid + 1]) ||
30+
((mid % 2 == 1) && nums[mid] == nums[mid - 1]))
31+
start = mid + 1;
32+
else
33+
end = mid - 1;
34+
}
35+
return -1;
36+
}
37+
}

Trees/Invert-Binary-Tree-226.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Given the root of a binary tree, invert the tree, and return its root.
2+
3+
// Example 1:
4+
5+
// Input: root = [4,2,7,1,3,6,9]
6+
// Output: [4,7,2,9,6,3,1]
7+
// Example 2:
8+
9+
10+
// Input: root = [2,1,3]
11+
// Output: [2,3,1]
12+
// Example 3:
13+
14+
// Input: root = []
15+
// Output: []
16+
17+
// Constraints:
18+
19+
// The number of nodes in the tree is in the range [0, 100].
20+
// -100 <= Node.val <= 100
21+
/**
22+
* Definition for a binary tree node.
23+
* public class TreeNode {
24+
* int val;
25+
* TreeNode left;
26+
* TreeNode right;
27+
* TreeNode() {}
28+
* TreeNode(int val) { this.val = val; }
29+
* TreeNode(int val, TreeNode left, TreeNode right) {
30+
* this.val = val;
31+
* this.left = left;
32+
* this.right = right;
33+
* }
34+
* }
35+
*/
36+
class Solution {
37+
public TreeNode invertTree(TreeNode root) {
38+
if(root == null)
39+
return root;
40+
TreeNode temp = root.left;
41+
root.left = invertTree(root.right);
42+
root.right = invertTree(temp);
43+
return root;
44+
}
45+
}

0 commit comments

Comments
 (0)