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
/** | |
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: | |
Only one letter can be changed at a time | |
Each intermediate word must exist in the dictionary | |
For example, | |
Given: | |
start = "hit" | |
end = "cog" |
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
/* | |
* Time complexity : O(n), Space Complexity : O(n), | |
* We could just use a n size char array instead of using nRows' StringBuilder array. See the convert0 for the detail | |
*/ | |
public static String convert(String s, int nRows){ | |
if (s == null || s.length() <= 1 || nRows <= 1) | |
return s; | |
StringBuilder[] sbs = new StringBuilder[nRows]; | |
for (int i=0; i<nRows; 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 static int solve(int N){ | |
int[] primes = new int[N]; | |
int cnt = 0; | |
boolean[] isp = new boolean[N+1]; | |
Arrays.fill(isp, true); | |
isp[0] = false; | |
isp[1] = false; | |
for (int i=2; i<=N; i++){ | |
if (isp[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
static int daysSinceYearStart(int y, int m, int d, boolean flag){ | |
boolean leapYear = isLeapYear(y); | |
int total = leapYear ? 366 : 365; | |
int days = d; | |
days += DayOfMonth[m-1]; | |
if (leapYear && m > 2) | |
days++; | |
return flag ? days : total - days; | |
} | |
static int dateDiff(int y1, int m1, int d1, int y2, int m2, int d2){ |
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
/* | |
Implement wildcard pattern matching with support for '?' and '*'. | |
'?' Matches any single character. | |
'*' Matches any sequence of characters (including the empty sequence). | |
The matching should cover the entire input string (not partial). | |
The function prototype should be: | |
bool isMatch(const char *s, const char *p) |
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
/* | |
Given a set of distinct integers, S, return all possible subsets. | |
Note: | |
Elements in a subset must be in non-descending order. | |
The solution set must not contain duplicate subsets. | |
For example, | |
If S = [1,2,3], a solution is: |
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
/* | |
Given two words (start and end), and a dictionary, find all shortest transformation sequence(s) from start to end, such that: | |
•Only one letter can be changed at a time | |
•Each intermediate word must exist in the dictionary | |
For example, | |
Given: | |
start = "hit" | |
end = "cog" |
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 static void morrisInOrderTraverse(TreeNode root){ | |
TreeNode ptr = root; | |
while (ptr != null){ | |
if (ptr.left == null){ | |
visit(ptr); | |
ptr = ptr.right; | |
}else{ | |
TreeNode node = ptr.left; | |
while (node.right != null && node.right != ptr) | |
node = node.right; |
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
/* | |
* Divide two integers without using multiplication, division and mod operator. | |
*/ | |
public int divide(int dividend, int divisor) { | |
if (divisor == 0) throw new IllegalArgumentException("divisor cannot be 0"); | |
if (dividend == 0) return 0; | |
boolean neg = (dividend > 0 != divisor > 0); | |
long dend = dividend; | |
long dsor = divisor; | |
dend = Math.abs(dend); |
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
/* | |
Given a binary tree, flatten it to a linked list in-place. | |
For example, | |
Given | |
1 | |
/ \ | |
2 5 | |
/ \ \ |
NewerOlder