Skip to content

Instantly share code, notes, and snippets.

@cixuuz
cixuuz / 139_1014.py
Last active October 15, 2017 03:10
[139. Word Break] #leetcode
# memo string
class Solution(object):
def __init__(self):
self.memo = dict()
def wordBreak(self, s, wordDict):
"""
:type s: str
:type wordDict: List[str]
:rtype: bool
@cixuuz
cixuuz / 242_1014.py
Created October 14, 2017 22:10
[242. Valid Anagram] #leetcode
class Solution(object):
# O(nlogn)
def isAnagram(self, s, t):
"""
:type s: str
:type t: str
:rtype: bool
"""
ss = sorted(s)
st = sorted(t)
@cixuuz
cixuuz / 76_1014.py
Created October 14, 2017 21:53
[76. Minimum Window Substring] #leetcode
class Solution(object):
# O(n) O(n)
def minWindow(self, s, t):
# get counts of each character and length of t
need, missing = collections.Counter(t), len(t)
# initialize indice
i = I = J = 0
# loop s
@cixuuz
cixuuz / 56_1012.java
Created October 12, 2017 14:02
[56. Merge Intervals] #leetcode
/**
* 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; }
* }
*/
class Solution {
@cixuuz
cixuuz / 232_1011.java
Created October 11, 2017 18:50
[232. Implement Queue using Stacks] #leetcode
class MyQueue {
private Deque<Integer> deque1;
private Deque<Integer> deque2;
private int count;
/** Initialize your data structure here. */
public MyQueue() {
deque1 = new LinkedList<Integer>();
deque2 = new LinkedList<Integer>();
count = 0;
@cixuuz
cixuuz / 53_1010.java
Created October 10, 2017 16:22
[53. Maximum Subarray] #leetcode
class Solution {
public int maxSubArray(int[] nums) {
int res = 0;
if (nums == null || nums.length == 0) return 0;
int[] dp = new int[nums.length];
dp[0] = nums[0];
res = dp[0];
@cixuuz
cixuuz / 566_1009.java
Created October 9, 2017 21:58
[566. Reshape the Matrix] #leetcode
class Solution {
public int[][] matrixReshape(int[][] nums, int r, int c) {
int m = nums.length;
int n = nums[0].length;
if (m * n != r * c) return nums;
int[][] res = new int[r][c];
for (int i=0; i < r; i++) {
for (int j=0; j<c; j++) {
@cixuuz
cixuuz / 500_1009.java
Last active October 9, 2017 16:17
[500. Keyboard Row] #leetcode
class Solution {
public String[] findWords(String[] words) {
return Stream.of(words).filter(s -> s.toLowerCase().matches("[qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*")).toArray(String[]::new);
}
}
@cixuuz
cixuuz / 146_1009.java
Last active October 9, 2017 14:41
[146. LRU Cache] #leetcode
class LRUCache {
private LinkedHashMap<Integer, Integer> map;
private final int CAPACITY;
public LRUCache(int capacity) {
CAPACITY = capacity;
map = new LinkedHashMap<Integer, Integer>(CAPACITY, 0.75f, true){
protected boolean removeEldestEntry(Map.Entry eldest) {
return size() > CAPACITY;
}
@cixuuz
cixuuz / 412_1007.java
Last active October 8, 2017 18:29
[412. Fizz Buzz] #leetcode
class Solution(object):
// O(n) O(n)
def fizzBuzz(self, n):
"""
:type n: int
:rtype: List[str]
"""
res = []
for i in range(1, n+1):