Skip to content

Instantly share code, notes, and snippets.

@bittib
bittib / WordLadderII.java
Created August 12, 2013 07:32
Word Ladder II @leetcode
/**
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"
@bittib
bittib / ZigZagConvertion.java
Created August 11, 2013 14:52
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSIIGYIR" Write the code that will take a string and make this conversion given a number of rows: string conver…
/*
* 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++){
@bittib
bittib / SmallestMultiple.java
Last active December 9, 2016 06:52
Project Euler Problem 5 本题求的是小于某个数的所有数集合的LCM,比较快的做法参考维基百科 利用整数的唯一分解定理,还可以用质因子分解法。将每个整数进行质因数分解。对每个质数,在质因数分解的表达式中寻找次数最高的乘幂,最后将所有这些质数乘幂相乘就可以得到最小公倍数。
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]){
@bittib
bittib / DaysDiff.java
Created June 22, 2013 17:39
Diff Days
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){
@bittib
bittib / WildCardMatch.java
Created June 21, 2013 08:28
Wildcard Match
/*
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)
@bittib
bittib / Subsets.java
Created June 15, 2013 10:28
Subsets
/*
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:
@bittib
bittib / WordLadderII.java
Last active December 9, 2016 06:54
WordLadder II
/*
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"
@bittib
bittib / MorrisInOrderTraverse.java
Created June 10, 2013 04:14
Morris InOrder Traverse Algorithm
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;
@bittib
bittib / DivideTwoIntegers.java
Created June 8, 2013 09:44
Divide Two Integers
/*
* 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);
@bittib
bittib / FlattenBinaryTreeToLinkedList.java
Last active July 20, 2017 15:18
Flatten Binary Tree to Linked List
/*
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1
/ \
2 5
/ \ \