This file contains hidden or 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 singly-linked list. | |
* public class ListNode { | |
* int val; | |
* ListNode next; | |
* ListNode(int x) { val = x; } | |
* } | |
*/ | |
class Solution { | |
public ListNode plusOne(ListNode head) { |
This file contains hidden or 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<String> findMissingRanges(int[] nums, int lower, int upper) { | |
List<String> res = new ArrayList<>(); | |
for(int i : nums) { | |
if(i > lower) res.add(lower+((i-1 > lower)?"->"+(i-1):"")); | |
if(i == upper) return res; // Avoid overflow | |
lower = i+1; | |
} | |
if(lower <= upper) res.add(lower + ((upper > lower)?"->"+(upper):"")); | |
return res; |
This file contains hidden or 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
class Solution { | |
private final static int[][] dir = {{-1, 0} , {1, 0}, {0, 1}, {0, -1}}; | |
public int cutOffTree(List<List<Integer>> forest) { | |
if (forest == null || forest.size() == 0) return 0; | |
int m = forest.size(); | |
int n = forest.get(0).size(); | |
// put trees in heap |
This file contains hidden or 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
class Solution { | |
// O(n) | |
public int findMin(int[] nums) { | |
int res = nums[0]; | |
for (int n : nums) { | |
if (n >= res) { | |
res = n; | |
} else { | |
return n; | |
} |
This file contains hidden or 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
class Solution { | |
public String frequencySort(String s) { | |
Map<Character, Integer> map = new HashMap<>(); | |
for (char c : s.toCharArray()) { | |
if (map.containsKey(c)) { | |
map.put(c, map.get(c) + 1); | |
} else { | |
map.put(c, 1); | |
} | |
} |
This file contains hidden or 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
class Solution { | |
public int calPoints(String[] ops) { | |
int sum = 0; | |
if (ops.length == 0 || ops == null) return sum; | |
Deque<Integer> stack = new LinkedList<Integer>(); | |
for (int i = 0 ; i < ops.length ; i++) { | |
if (ops[i].matches("-?\\d+(\\.\\d+)?")) { | |
int val = Integer.valueOf(ops[i]); | |
stack.addLast(val); |
This file contains hidden or 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
class MapSum { | |
class TrieNode { | |
int val; | |
Map<Character, TrieNode> next; | |
boolean isWord; | |
public TrieNode() { | |
val = 0; | |
next = new HashMap<Character, TrieNode>(); | |
isWord = false; |
This file contains hidden or 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
class Solution { | |
// O(n^3) O(n) | |
public boolean repeatedSubstringPattern(String s) { | |
int n = s.length(); | |
for (int i=1; i<=n/2; i++) { | |
if (n%i == 0) { | |
StringBuilder sb = new StringBuilder(); | |
for (int j=0; j<n/i; j++) { | |
sb.append(s.substring(0, i)); |
This file contains hidden or 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
class Solution { | |
public int minCut(String s) { | |
char[] c = s.toCharArray(); | |
int n = c.length; | |
int[] cut = new int[n]; | |
boolean[][] pal = new boolean[n][n]; | |
for(int i = 0; i < n; i++) { | |
int min = i; | |
for(int j = 0; j <= i; j++) { |
This file contains hidden or 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
class Solution { | |
// O(n!) O(n!) | |
private List<List<String>> res = new ArrayList<List<String>>(); | |
private HashMap<String, Boolean> ps = new HashMap<String, Boolean>(); | |
public List<List<String>> partition(String s) { | |
findP(s, new ArrayList<String>()); | |
return res; | |
} | |