Skip to content

Commit ec31d5b

Browse files
committed
算法
1 parent 1424730 commit ec31d5b

38 files changed

+1497
-3
lines changed

src/main/java/code/algorithm/wordcount/WordCountMain.java renamed to src/main/java/code/algorithm/divideconquer/WordCountMain.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package code.algorithm.wordcount;
1+
package code.algorithm.divideconquer;
22

33
import code.collection.MinHeap;
44
import code.collection.MinArray;
@@ -11,7 +11,7 @@
1111

1212
/**
1313
* 〈WorkCount〉<p>
14-
* 〈功能详细描述〉
14+
* MapReduce,分治算法
1515
*
1616
* @author zixiao
1717
* @date 2019/2/21

src/main/java/code/algorithm/ConsistentHash.java renamed to src/main/java/code/algorithm/hash/ConsistentHash.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package code.algorithm;
1+
package code.algorithm.hash;
22

33
import java.nio.ByteBuffer;
44
import java.nio.ByteOrder;
Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package code.algorithm.hash;
2+
3+
/**
4+
*  MurMurHash算法,是非加密HASH算法,性能很高,
5+
*  比传统的CRC32,MD5,SHA-1(这两个算法都是加密HASH算法,复杂度本身就很高,带来的性能上的损害也不可避免)
6+
*  等HASH算法要快很多,而且据说这个算法的碰撞率很低.
7+
*  http://murmurhash.googlepages.com/
8+
*
9+
* @author zixiao
10+
* @date 2019/11/28
11+
*/
12+
public final class MurmurHash {
13+
14+
// all methods static; private constructor.
15+
private MurmurHash() {}
16+
17+
/**
18+
* Generates 32 bit hash from byte array of the given length and
19+
* seed.
20+
*
21+
* @param data byte array to hash
22+
* @param length length of the array to hash
23+
* @param seed initial seed value
24+
* @return 32 bit hash of the given array
25+
*/
26+
public static int hash32(final byte[] data, int length, int seed) {
27+
// 'm' and 'r' are mixing constants generated offline.
28+
// They're not really 'magic', they just happen to work well.
29+
final int m = 0x5bd1e995;
30+
final int r = 24;
31+
32+
// Initialize the hash to a random value
33+
int h = seed^length;
34+
int length4 = length/4;
35+
36+
for (int i=0; i<length4; i++) {
37+
final int i4 = i*4;
38+
int k = (data[i4+0]&0xff) +((data[i4+1]&0xff)<<8)
39+
+((data[i4+2]&0xff)<<16) +((data[i4+3]&0xff)<<24);
40+
k *= m;
41+
k ^= k >>> r;
42+
k *= m;
43+
h *= m;
44+
h ^= k;
45+
}
46+
47+
// Handle the last few bytes of the input array
48+
switch (length%4) {
49+
case 3: h ^= (data[(length&~3) +2]&0xff) << 16;
50+
case 2: h ^= (data[(length&~3) +1]&0xff) << 8;
51+
case 1: h ^= (data[length&~3]&0xff);
52+
h *= m;
53+
}
54+
55+
h ^= h >>> 13;
56+
h *= m;
57+
h ^= h >>> 15;
58+
59+
return h;
60+
}
61+
62+
/**
63+
* Generates 32 bit hash from byte array with default seed value.
64+
*
65+
* @param data byte array to hash
66+
* @param length length of the array to hash
67+
* @return 32 bit hash of the given array
68+
*/
69+
public static int hash32(final byte[] data, int length) {
70+
return hash32(data, length, 0x9747b28c);
71+
}
72+
73+
/**
74+
* Generates 32 bit hash from a string.
75+
*
76+
* @param text string to hash
77+
* @return 32 bit hash of the given string
78+
*/
79+
public static int hash32(final String text) {
80+
final byte[] bytes = text.getBytes();
81+
return hash32(bytes, bytes.length);
82+
}
83+
84+
/**
85+
* Generates 32 bit hash from a substring.
86+
*
87+
* @param text string to hash
88+
* @param from starting index
89+
* @param length length of the substring to hash
90+
* @return 32 bit hash of the given string
91+
*/
92+
public static int hash32(final String text, int from, int length) {
93+
return hash32(text.substring( from, from+length));
94+
}
95+
96+
/**
97+
* Generates 64 bit hash from byte array of the given length and seed.
98+
*
99+
* @param data byte array to hash
100+
* @param length length of the array to hash
101+
* @param seed initial seed value
102+
* @return 64 bit hash of the given array
103+
*/
104+
public static long hash64(final byte[] data, int length, int seed) {
105+
final long m = 0xc6a4a7935bd1e995L;
106+
final int r = 47;
107+
108+
long h = (seed&0xffffffffl)^(length*m);
109+
110+
int length8 = length/8;
111+
112+
for (int i=0; i<length8; i++) {
113+
final int i8 = i*8;
114+
long k = ((long)data[i8+0]&0xff) +(((long)data[i8+1]&0xff)<<8)
115+
+(((long)data[i8+2]&0xff)<<16) +(((long)data[i8+3]&0xff)<<24)
116+
+(((long)data[i8+4]&0xff)<<32) +(((long)data[i8+5]&0xff)<<40)
117+
+(((long)data[i8+6]&0xff)<<48) +(((long)data[i8+7]&0xff)<<56);
118+
119+
k *= m;
120+
k ^= k >>> r;
121+
k *= m;
122+
123+
h ^= k;
124+
h *= m;
125+
}
126+
127+
switch (length%8) {
128+
case 7: h ^= (long)(data[(length&~7)+6]&0xff) << 48;
129+
case 6: h ^= (long)(data[(length&~7)+5]&0xff) << 40;
130+
case 5: h ^= (long)(data[(length&~7)+4]&0xff) << 32;
131+
case 4: h ^= (long)(data[(length&~7)+3]&0xff) << 24;
132+
case 3: h ^= (long)(data[(length&~7)+2]&0xff) << 16;
133+
case 2: h ^= (long)(data[(length&~7)+1]&0xff) << 8;
134+
case 1: h ^= (long)(data[length&~7]&0xff);
135+
h *= m;
136+
};
137+
138+
h ^= h >>> r;
139+
h *= m;
140+
h ^= h >>> r;
141+
142+
return h;
143+
}
144+
145+
/**
146+
* Generates 64 bit hash from byte array with default seed value.
147+
*
148+
* @param data byte array to hash
149+
* @param length length of the array to hash
150+
* @return 64 bit hash of the given string
151+
*/
152+
public static long hash64(final byte[] data, int length) {
153+
return hash64(data, length, 0xe17a1465);
154+
}
155+
156+
/**
157+
* Generates 64 bit hash from a string.
158+
*
159+
* @param text string to hash
160+
* @return 64 bit hash of the given string
161+
*/
162+
public static long hash64(final String text) {
163+
final byte[] bytes = text.getBytes();
164+
return hash64(bytes, bytes.length);
165+
}
166+
167+
/**
168+
* Generates 64 bit hash from a substring.
169+
*
170+
* @param text string to hash
171+
* @param from starting index
172+
* @param length length of the substring to hash
173+
* @return 64 bit hash of the given array
174+
*/
175+
public static long hash64(final String text, int from, int length) {
176+
return hash64(text.substring( from, from+length));
177+
}
178+
179+
180+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package code.algorithm.hash;
2+
3+
import code.collection.hashtable.HashMap;
4+
import code.util.Encrypt;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Data;
7+
8+
import java.io.Serializable;
9+
import java.util.UUID;
10+
11+
/**
12+
* 〈密码加密〉<p>
13+
* sha256(md5(password) + slat)
14+
*
15+
* @author zixiao
16+
* @date 2019/12/10
17+
*/
18+
public class PasswordEncrypt {
19+
20+
private static HashMap<String, UserLogin> userLoginTable = new HashMap<>();
21+
22+
@Data
23+
@AllArgsConstructor
24+
class UserLogin implements Serializable {
25+
26+
String password;
27+
28+
String slat;
29+
30+
}
31+
32+
public boolean register(String userName, String password){
33+
if(userLoginTable.contains(userName)){
34+
return false;
35+
}
36+
userLoginTable.put(userName, register(password));
37+
return true;
38+
}
39+
40+
private UserLogin register(String password){
41+
String slat = UUID.randomUUID().toString().substring(0, 8);
42+
String pwd = Encrypt.encrypt(password, slat);
43+
System.out.println("加密password:"+pwd + ",size:"+pwd.length());
44+
return new UserLogin(pwd, slat);
45+
}
46+
47+
public boolean login(String userName, String password){
48+
UserLogin login = userLoginTable.get(userName);
49+
if(login == null){
50+
System.out.println("用户不存在");
51+
return false;
52+
}
53+
String pwd = Encrypt.encrypt(password, login.getSlat());
54+
if(pwd.equals(login.getPassword())){
55+
System.out.println("用户["+userName+"]登录成功");
56+
return true;
57+
}
58+
System.out.println("密码错误");
59+
return false;
60+
}
61+
62+
public static void main(String[] args) {
63+
PasswordEncrypt encrypt = new PasswordEncrypt();
64+
encrypt.register("beston", "123456");
65+
encrypt.register("a", "z3lll39399440030sk2828000300e30d200309a9393er99392f9499lels");
66+
encrypt.register("b", "AAA"+System.currentTimeMillis());
67+
encrypt.register("c", UUID.randomUUID().toString());
68+
encrypt.login("beston", "123456");
69+
}
70+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package code.algorithm.recall;
2+
3+
/**
4+
* 〈8皇后问题〉<p>
5+
* 有一个8x8的棋盘,希望往里放8个棋子(皇后),每个棋子所在的行、列、对角线都不能有另一个棋子
6+
*
7+
* @author zixiao
8+
* @date 2019/12/19
9+
*/
10+
public class EightQueen {
11+
12+
private static int EIGHT = 8;
13+
14+
/**
15+
* 下标索引代表行row
16+
* 值代表列col
17+
*/
18+
private int[] _8Queen = new int[EIGHT];
19+
20+
public void build8Queen() {
21+
for (int i = 0; i < _8Queen.length; i++) {
22+
_8Queen[i] = -1;
23+
}
24+
calc8Queen(0);
25+
}
26+
27+
private void calc8Queen(int row) {
28+
if (row == EIGHT) {
29+
print();
30+
return;
31+
}
32+
//8个列的位置可选
33+
for (int col = 0; col < EIGHT; col++) {
34+
if (isOk(row, col)) {
35+
_8Queen[row] = col;
36+
calc8Queen(row + 1);
37+
}
38+
}
39+
}
40+
41+
/**
42+
* 与已放置的棋子,逐个比较
43+
* 1 不能同一列
44+
* 2 不能在对角线上
45+
*
46+
* @param row
47+
* @param col
48+
* @return
49+
*/
50+
private boolean isOk(int row, int col) {
51+
for (int i = 0; i < row; i++) {
52+
//在同一列,不符合要求
53+
if (_8Queen[i] == col) {
54+
return false;
55+
}
56+
//在对角线上,不符合要求
57+
if (Math.abs(_8Queen[i] - col) == Math.abs(i - row)) {
58+
return false;
59+
}
60+
}
61+
return true;
62+
}
63+
64+
private void print() {
65+
StringBuilder sb = new StringBuilder();
66+
for (int i = 0; i < _8Queen.length; i++) {
67+
//第一行
68+
for (int j = 0; j < EIGHT; j++) {
69+
if (_8Queen[i] == j) {
70+
sb.append(1).append("\t");
71+
} else {
72+
sb.append(0).append("\t");
73+
}
74+
}
75+
sb.append("\n\r");
76+
}
77+
System.out.println(sb);
78+
}
79+
80+
public static void main(String[] args) {
81+
EightQueen eightQueen = new EightQueen();
82+
eightQueen.build8Queen();
83+
}
84+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package code.algorithm.recursion;
2+
3+
/**
4+
* 〈阶乘〉<p>
5+
* 0!=1, 1!=1*0!, 2!=2*1!, 3!=3*2!, 4!=4*3!
6+
* 递归公式:f(n) = n * f(n-1), 终止条件:f(0) = 1
7+
*
8+
* @author zixiao
9+
* @date 2019/11/27
10+
*/
11+
public class Factorial {
12+
13+
public static int f(int n) {
14+
if (n == 0) {
15+
return 1;
16+
}
17+
return n * f(n - 1);
18+
}
19+
20+
public static void main(String[] args) {
21+
for (int i = 0; i < 10; i++) {
22+
System.out.println(i + "! = " + f(i));
23+
}
24+
}
25+
26+
}

0 commit comments

Comments
 (0)