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
# 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 |
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(object): | |
# O(nlogn) | |
def isAnagram(self, s, t): | |
""" | |
:type s: str | |
:type t: str | |
:rtype: bool | |
""" | |
ss = sorted(s) | |
st = sorted(t) |
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(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 |
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 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 { |
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 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; |
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 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]; | |
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[][] 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++) { |
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[] findWords(String[] words) { | |
return Stream.of(words).filter(s -> s.toLowerCase().matches("[qwertyuiop]*|[asdfghjkl]*|[zxcvbnm]*")).toArray(String[]::new); | |
} | |
} |
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 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; | |
} |
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(object): | |
// O(n) O(n) | |
def fizzBuzz(self, n): | |
""" | |
:type n: int | |
:rtype: List[str] | |
""" | |
res = [] | |
for i in range(1, n+1): |