Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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"