Skip to content

Commit 8aafb11

Browse files
committed
- Add: Java solutions
1 parent f9164a4 commit 8aafb11

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public int search(int[] nums, int target) {
3+
int size = nums.length;
4+
int start = 0;
5+
int end = size - 1;
6+
while (start <= end) {
7+
int mid = (end + start) / 2;
8+
if (nums[mid] == target) return mid;
9+
if (nums[start] > nums[mid]) {
10+
if (target > nums[end] ||
11+
target < nums[mid]) end = mid - 1;
12+
else start = mid + 1;
13+
} else {
14+
if (target < nums[start] ||
15+
target > nums[mid]) start = mid + 1;
16+
else end = mid - 1;
17+
}
18+
}
19+
20+
return -1;
21+
}
22+
} //TC: O(logn), SC: O(1)

LeetCode/39. Combination Sum.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum(int[] cands, int target) {
3+
List<List<Integer>> res = new ArrayList<>();
4+
class Helper {
5+
void backtrack(int i, List<Integer> cur, int diff) {
6+
if (diff == 0) res.add(new ArrayList<>(cur));
7+
if (i >= cands.length || diff <= 0) return;
8+
cur.add(cands[i]);
9+
backtrack(i, cur, diff - cands[i]);
10+
cur.remove(cur.size() - 1);
11+
backtrack(i + 1, cur, diff);
12+
}
13+
}
14+
new Helper().backtrack(0, new ArrayList<>(), target);
15+
return res;
16+
}
17+
} //TC: O(N^(M/T + 1)), SC: O(T/M), where T is the target value, N is the number of candidates, M is the smallest candidate among all the given integers

0 commit comments

Comments
 (0)