Skip to content

Commit 4bcea22

Browse files
committed
1118
1 parent 9ef484f commit 4bcea22

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ Feel free to submit pull requests, add issues and be a contributer.
4141
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
4242
| Leetcode | [12. Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/) | [Java](./java/intToRoman.java) | _O(n)_ | _O(1)_ | Medium | |
4343
| Leetcode | [31. Next Permutation](https://leetcode.com/problems/next-permutation/description/) | [Java](./java/nextPermutation.java) | _O(n)_ | _O(1)_ | Easy | |
44+
| Leetcode | [39. Combination Sum](https://leetcode.com/problems/combination-sum/description/) | [Java](./java/combinationSum.java) | _O(2^n)_ | _O(1)_ | Medium | |
45+
| Leetcode | [40. Combination Sum II](https://leetcode.com/problems/combination-sum-ii/description/) | [Java](./java/combinationSumii.java) | _O(2^n)_ | _O(1)_ | Medium | |
4446
| Leetcode | [46. Permutations](https://leetcode.com/problems/permutations/description/) | [Java](./java/permute.java) | _O(2^n)_ | _O(1)_ | Medium | |
4547
| Leetcode | [47. Permutations II](https://leetcode.com/problems/permutations-ii/description/) | [Java](./java/permuteUnique.java) | _O(2^n)_ | _O(1)_ | Medium | |
4648
| Leetcode | [66. Plus One](https://leetcode.com/problems/plus-one/description/) | [Java](./java/plusOne.java) | _O(n)_ | _O(1)_ | Easy | |
@@ -75,6 +77,7 @@ Feel free to submit pull requests, add issues and be a contributer.
7577
|---------------- |---------------- | ----------- | --------------- | --------------- | ------------- |-----|
7678
| Leetcode | [6. ZigZag Conversion](https://leetcode.com/problems/zigzag-conversion/description/) | [Java](./java/ZigZag.java) | _O(n)_ | _O(n)_ | Medium | |
7779
| Leetcode | [125. Valid Palindrome](https://leetcode.com/problems/valid-palindrome/description/) | [Java](./java/isPalindrome.java) | _O(n)_ | _O(1)_ | Easy | |
80+
| Leetcode | [131. Palindrome Partitioning](https://leetcode.com/problems/palindrome-partitioning/description/) | [Java](./java/PalindromePartition.java) | _O(2^n)_ | _O(1)_ | Medium | |
7881
| Leetcode | [139. Word Break](https://leetcode.com/problems/word-break/description/) | [Java](./java/wordBreak.java) | _O(n^2)_ | _O(n)_ | Medium | |
7982
| Leetcode | [151. Reverse Words in a String](https://leetcode.com/problems/reverse-words-in-a-string/description/) | [Java](./java/reverseWords1.java) | _O(n)_ | _O(1)_ | Easy | |
8083
| Leetcode | [165. Compare Version Numbers](https://leetcode.com/problems/compare-version-numbers/description/) | [Java](./java/compareVersion.java) | _O(n)_ | _O(1)_ | Medium | |

java/PalindromePartition.java

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution {
2+
public List<List<String>> partition(String s) {
3+
List<List<String>> result = new ArrayList<>();
4+
backtrack(result, s, new ArrayList<String>(), 0);
5+
return result;
6+
}
7+
8+
public void backtrack(List<List<String>> result, String s, List<String> temp, int start)
9+
{
10+
if(start == s.length())
11+
result.add(new ArrayList<String>(temp));
12+
else
13+
{
14+
for(int i=start; i<s.length(); i++)
15+
{
16+
if(isPalindrome(s, start, i))
17+
{
18+
temp.add(s.substring(start, i+1));
19+
backtrack(result, s, temp, i+1);
20+
temp.remove(temp.size()-1);
21+
}
22+
}
23+
}
24+
}
25+
26+
public boolean isPalindrome(String s, int left, int right)
27+
{
28+
while(left < right)
29+
{
30+
if(s.charAt(left++) != s.charAt(right--)) return false;
31+
}
32+
return true;
33+
}
34+
}

java/combinationSum.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum(int[] candidates, int target) {
3+
List<List<Integer>> result = new ArrayList<>();
4+
Arrays.sort(candidates);
5+
backtrack(candidates, result, new ArrayList<Integer>(), target, 0);
6+
return result;
7+
}
8+
9+
public void backtrack(int [] candidates, List<List<Integer>> result, List<Integer> temp, int stepDiff, int start)
10+
{
11+
if(stepDiff < 0) return;
12+
else if(stepDiff == 0) result.add(new ArrayList<Integer>(temp));
13+
else
14+
{
15+
for(int i=start; i<candidates.length; i++)
16+
{
17+
temp.add(candidates[i]);
18+
backtrack(candidates, result, temp, stepDiff-candidates[i], i);
19+
temp.remove(temp.size()-1);
20+
}
21+
}
22+
}
23+
}

java/combinationSumii.java

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
public List<List<Integer>> combinationSum2(int[] candidates, int target) {
3+
List<List<Integer>> result = new ArrayList<>();
4+
Arrays.sort(candidates);
5+
backtrack(candidates, result, new ArrayList<Integer>(), target, 0);
6+
return result;
7+
}
8+
9+
public void backtrack(int [] candidates, List<List<Integer>> result, List<Integer> temp, int stepDiff, int start)
10+
{
11+
if(stepDiff < 0) return;
12+
else if(stepDiff == 0) result.add(new ArrayList<Integer>(temp));
13+
else
14+
{
15+
for(int i=start; i<candidates.length; i++)
16+
{
17+
if(i>start && candidates[i] == candidates[i-1]) continue;
18+
temp.add(candidates[i]);
19+
backtrack(candidates, result, temp, stepDiff-candidates[i], i+1);
20+
temp.remove(temp.size()-1);
21+
}
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)