This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public List<List<Integer>> combine(int n, int k) { | |
List<List<Integer>> result = new ArrayList<>(); | |
if (k > n) { | |
return result; | |
} | |
List<Integer> res = new ArrayList<Integer>(); | |
combine(n, k, result, res, 1); | |
return result; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public List<List<Integer>> subsetsWithDup(int[] num) { | |
List<List<Integer>> result = new ArrayList<>(); | |
if (num == null || num.length == 0) { | |
return result; | |
} | |
Arrays.sort(num); | |
List<Integer> res = new ArrayList<>(); | |
subsets(num, result, res, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public List<List<Integer>> subsets(int[] S) { | |
List<List<Integer>> result = new ArrayList<>(); | |
if (S == null || S.length == 0) { | |
return result; | |
} | |
Arrays.sort(S); | |
List<Integer> res = new ArrayList<>(); | |
subsets(S, result, res, 0); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for an interval. | |
* public class Interval { | |
* int start; | |
* int end; | |
* Interval() { start = 0; end = 0; } | |
* Interval(int s, int e) { start = s; end = e; } | |
* } | |
*/ | |
public class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for an interval. | |
* public class Interval { | |
* int start; | |
* int end; | |
* Interval() { start = 0; end = 0; } | |
* Interval(int s, int e) { start = s; end = e; } | |
* } | |
*/ | |
public class Solution { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public List<Integer> grayCode(int n) { | |
List<Integer> result = new ArrayList<Integer>(); | |
if (n == 0) { | |
result.add(0); | |
return result; | |
} | |
List<Integer> res = grayCode(n - 1); | |
result.addAll(res); | |
for (int i = res.size() - 1; i >= 0; i--) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public boolean isPalindrome(int x) { | |
if (x < 0) { | |
return false; | |
} | |
int digit = 1; | |
while (x / digit >= 10) { // err: x >= 10 * digit, as 10*digit may overflow | |
digit *= 10; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int reverse(int x) { | |
int res = 0; | |
while (x != 0) { | |
// handle overflow/underflow | |
if (Math.abs(res) > Integer.MAX_VALUE / 10) { | |
return 0; | |
} | |
res = res * 10 + x % 10; | |
x /= 10; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int reverse(int x) { | |
long res = 0; | |
int sign = x >= 0 ? 1 : -1; | |
x = Math.abs(x); | |
while (x > 0) { | |
res = res * 10 + x % 10; | |
x /= 10; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Solution { | |
public int climbStairs(int n) { | |
if (n <= 0) { | |
return 0; | |
} | |
int prev = 1; | |
int curr = 1; | |
while (--n > 0) { | |
int tmp = curr; |
NewerOlder