Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 369_1005.java
Last active October 5, 2017 14:30
[369. Plus One Linked List] #leetcode
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public ListNode plusOne(ListNode head) {
@cixuuz
cixuuz / 163_1004.java
Created October 4, 2017 15:13
[163. Missing Ranges] #leetcode
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;
@cixuuz
cixuuz / 675_0929.java
Created September 29, 2017 13:49
[675. Cut Off Trees for Golf Event] #leetcode
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
@cixuuz
cixuuz / 153_0928.java
Last active September 28, 2017 21:31
[153. Find Minimum in Rotated Sorted Array] #leetcode
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;
}
@cixuuz
cixuuz / 451_0927.java
Last active September 27, 2017 13:44
[451. Sort Characters By Frequency] #leetcode
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);
}
}
@cixuuz
cixuuz / 682_0923.java
Created September 24, 2017 02:06
[682. Baseball Game] #leetcode
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);
@cixuuz
cixuuz / 677_0923.java
Last active September 24, 2017 00:22
[677. Map Sum Pairs] #leetcode
class MapSum {
class TrieNode {
int val;
Map<Character, TrieNode> next;
boolean isWord;
public TrieNode() {
val = 0;
next = new HashMap<Character, TrieNode>();
isWord = false;
@cixuuz
cixuuz / 459_0918.java
Created September 19, 2017 00:14
[459. Repeated Substring Pattern] #leetcode
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));
@cixuuz
cixuuz / 132_0918.java
Created September 18, 2017 22:27
[132. Palindrome Partitioning II] #leetcode
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++) {
@cixuuz
cixuuz / 131_0915.java
Created September 16, 2017 03:33
[131. Palindrome Partitioning] #leetcode
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;
}