Skip to content

Commit a03dcc2

Browse files
committed
no message
1 parent cacecbc commit a03dcc2

15 files changed

Lines changed: 287 additions & 10 deletions

File tree

src/main/java/org/tj/OOP.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@
44
* Created by 001 on 16/7/27.
55
*/
66
public class OOP {
7+
public static void main(String[] args) {
8+
System.out.println(Runtime.getRuntime().availableProcessors());
9+
}
710
}

src/main/java/org/tj/algorithms/Sort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static void straightInsertionSort(int []a){
2222
static void quickSort(int [] a){
2323
quickSort(a,0,a.length-1);
2424
}
25-
// 快排 平均时间复杂度logN 最坏情况n2
25+
// 快排 平均时间复杂度 NlogN 最坏情况n2
2626
static void quickSort(int [] a,int begin,int end){
2727
int tmp = a[begin];
2828
int i = begin,j = end;
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.tj.algorithms.artofcoding.gcd;
2+
3+
/**
4+
* Created by 001 on 16/8/25.
5+
*/
6+
public class Euclidean {
7+
8+
// 欧几里德算法 又称辗转相除法 用来求两个数字的最大公因数
9+
static public int gcd(int m,int n){
10+
if (m<n){
11+
int t = m;
12+
m = n;
13+
n = t;
14+
}
15+
int k = m%n;
16+
while (k!=0){
17+
int t = n;
18+
n=k;
19+
m=t;
20+
k=m%n;
21+
}
22+
return n;
23+
}
24+
25+
26+
public static void main(String[] args) {
27+
System.out.println(gcd(544,119));
28+
}
29+
30+
}
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package org.tj.algorithms.artofcoding.strings;
2+
3+
import org.tj.algorithms.strings.StringCutter;
4+
5+
/**
6+
* Created by 001 on 16/8/25.
7+
*/
8+
public class SpinningString {
9+
10+
// 定义字符串的左旋转操作:把字符串前面的若干个字符移动到字符串的尾部。
11+
// 如把字符串 abcdef 左旋转 2 位得到字符串 cdefab。 请实现字符串左旋转的函数,要求对长度为 n 的字符串操作的时间复杂度为 O(n),
12+
13+
14+
15+
// 设计一个算法,把一个含有 N 个元素的数组循环右移 K 位,要求时间复杂度为 O(N), 且只允许使用两个附加变量。
16+
// 最直接解法 一位一位移 每移一位整个数组变动一次 先吧最后一位保存 然后每一位往前移一位
17+
// 时间复杂度为 n * (k%n)
18+
public static char[] rightShift(char[] n,int k){
19+
int length = n.length;
20+
k = k%length;
21+
k = k>0?k:length+k;
22+
for (int i=0;i<k;i++){
23+
char tmp = n[length-1];
24+
for (int j=length-1;j>0;j--){
25+
n[j] = n[j-1];
26+
}
27+
n[0] = tmp;
28+
}
29+
return n;
30+
}
31+
32+
// abcf1234 右移四位变成1234abcf 那么直接吧后四位放倒字符串前面即可 时间复杂度n
33+
public static char[] rightShift2(char[] n,int k){
34+
int length = n.length;
35+
k = k%length;
36+
k = k>0?k:length+k;
37+
// k=length-k;
38+
// 构建一个新的char[];
39+
char[] m = new char[length];
40+
for (int i=0;i<k;i++){
41+
m[i] = n[length-k+i];
42+
}
43+
for (int i=k;i<length;i++){
44+
m[i] = n[i-k];
45+
}
46+
return m;
47+
}
48+
49+
// abcf1234 右移3位 先把前五位倒序 1fcba 后三位倒序432 再整个倒序 234abcf1
50+
public static char[] rightShift3(char[] n,int k){
51+
int length = n.length;
52+
k = k%length;
53+
k = k>0?k:length+k;
54+
for (int i=0;i<=(length-k-1)/2;i++){
55+
char tmp = n[i];
56+
n[i] = n[length-k-i-1];
57+
n[length-k-i-1] = tmp;
58+
}
59+
// System.out.println(String.valueOf(n));
60+
for (int i=length-k;i<=(2*length-k-1)/2;i++){
61+
char tmp = n[i];
62+
n[i] = n[length+(length-k)-1-i];
63+
n[length+(length-k)-1-i] = tmp;
64+
}
65+
// System.out.println(String.valueOf(n));
66+
67+
// 反正n
68+
for (int i=0;i<=(length-1)/2;i++){
69+
char tmp = n[i];
70+
n[i] = n[length-1-i];
71+
n[length-1-i] = tmp;
72+
}
73+
return n;
74+
}
75+
76+
77+
public static void main(String[] args) {
78+
String s = "abcf1234";
79+
System.out.println(String.valueOf(rightShift(s.toCharArray(),-13)));
80+
System.out.println(String.valueOf(rightShift2(s.toCharArray(), -13)));
81+
System.out.println(String.valueOf(rightShift3(s.toCharArray(), -13)));
82+
}
83+
84+
85+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package org.tj.algorithms.hash;
2+
3+
import java.util.HashMap;
4+
import java.util.Random;
5+
6+
/**
7+
* Created by 001 on 16/8/25.
8+
*/
9+
public class DemoHash {
10+
11+
// 随机生成n个数的数组
12+
public static int[] randIntArray(int n){
13+
Random random = new Random();
14+
int []array = new int[n];
15+
for (int i=0;i<n;i++){
16+
array[i] = random.nextInt();
17+
}
18+
return array;
19+
}
20+
// 直接对n取模算hash
21+
public static int[] modhash(int[] array,int n){
22+
int[] array1 = new int[16];
23+
int length = array.length;
24+
for (int i=0;i<length;i++){
25+
int index = array[i]%n;
26+
array1[index>0?index:-index]++;
27+
}
28+
return array1;
29+
}
30+
31+
// 斐波那契(Fibonacci)散列法
32+
public static int[] fibonaccihash(int[] array,int n){
33+
int[] array1 = new int[16];
34+
int length = array.length;
35+
long f = Long.valueOf("2654435769");
36+
for (int i=0;i<length;i++){
37+
int index = (int) ((array[i]*f)%n);
38+
array1[index>0?index:-index]++;
39+
}
40+
return array1;
41+
}
42+
43+
public static void printArray(int[] array){
44+
int length = array.length;
45+
46+
for (int i=0;i<length;i++){
47+
System.out.print(array[i]+" ");
48+
}
49+
System.out.println();
50+
}
51+
52+
53+
54+
public static void main(String[] args) {
55+
56+
// System.out.println(-5%4);
57+
int [] array = randIntArray(16);
58+
printArray(modhash(array,16));
59+
printArray(fibonaccihash(array,16));
60+
// long k = Long.valueOf("2654435769");
61+
// for (long i =0;i<100;i++){
62+
// System.out.println(((i*k)>>28) %16);
63+
// }
64+
}
65+
}

src/main/java/org/tj/designpatterns/creater/factory/abstractfactory/AbstractFactoryTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.tj.designpatterns.creater.factory.simplefactory.Sender;
44

5+
import java.util.Arrays;
6+
import java.util.Calendar;
7+
58
/**
69
* Created by 001 on 16/8/22.
710
*/
@@ -14,5 +17,10 @@ public static void main(String[] args) {
1417
AbstractFactory factory1 = new MessageSenderFactory();
1518
Sender sender1 = factory1.produceSender();
1619
sender1.send();
20+
21+
22+
23+
// Calendar.getInstance()
24+
// Arrays.asList()
1725
}
1826
}

src/main/java/org/tj/designpatterns/creater/singleton/HungrySingleton.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,12 @@
44
* Created by 001 on 16/8/22.
55
*/
66
public class HungrySingleton {
7+
8+
// 饿汉式单例模式 线程安全 因为是在类加载的时候初始化的 但是缺点是 不管你用不用这个单例 每次启动都要实例化 会浪费资源和时间
9+
private HungrySingleton hungrySingleton = new HungrySingleton();
10+
11+
public HungrySingleton getInstance(){
12+
return hungrySingleton;
13+
}
14+
715
}

src/main/java/org/tj/designpatterns/creater/singleton/LazySingleton.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,25 @@
33
/**
44
* Created by 001 on 16/8/8.
55
*/
6-
public class Singleton {
6+
public class LazySingleton {
77

8-
private static Singleton instance = null;
8+
private static LazySingleton instance = null;
99

10-
private Singleton(){
10+
private LazySingleton(){
1111
System.out.println(instance);
1212
}
1313

1414
// 最基本的
15-
private static Singleton getInstance(){
15+
private static LazySingleton getInstance(){
1616
if (instance == null){
17-
instance = new Singleton();
17+
instance = new LazySingleton();
1818
}
1919
return instance;
2020
}
2121
// 每次去拿单例都要的获取锁等待 实际上只要第一次线程竞争的时候获取锁就行了
22-
private static synchronized Singleton getInstance1(){
22+
private static synchronized LazySingleton getInstance1(){
2323
if(instance == null){
24-
instance = new Singleton();
24+
instance = new LazySingleton();
2525
}
2626
return instance;
2727
}
@@ -30,11 +30,11 @@ private static synchronized Singleton getInstance1(){
3030
// 但是还会有一个问题 在JVM中 创建对象和赋值操作实分开进行的 并且不保证执行的先后顺序 也就是说有可能AB线程 ,A获取锁,根据JVM的优化
3131
// 机制,JVM有可能先给 Singletin 实例分配空间,然后就直接赋值给了instance变量,A线程离开同步块,B线程检测instance不为空,直接返回intance,有可能此时
3232
// JVM还没有完成初始化,所以B线程返回的为null;
33-
private static Singleton getInstance2(){
33+
private static LazySingleton getInstance2(){
3434
if (instance == null){
3535
synchronized (instance){
3636
if (instance == null){
37-
instance = new Singleton();
37+
instance = new LazySingleton();
3838
}
3939
}
4040
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.tj.study.thread.abcd.join;
2+
3+
/**
4+
* Created by 001 on 16/8/23.
5+
*/
6+
public class DemoJoin implements Runnable{
7+
8+
Thread thread;
9+
10+
public DemoJoin(Thread thread){
11+
this.thread = thread;
12+
}
13+
14+
public static void main(String[] args) {
15+
Thread thread1 = Thread.currentThread();
16+
Thread threadA = new Thread(new DemoJoin(thread1),"A");
17+
Thread threadB = new Thread(new DemoJoin(threadA),"B");
18+
Thread threadC = new Thread(new DemoJoin(threadB),"C");
19+
Thread threadD = new Thread(new DemoJoin(threadC),"D");
20+
21+
threadA.start();
22+
threadB.start();
23+
threadC.start();
24+
threadD.start();
25+
}
26+
27+
@Override
28+
public void run() {
29+
try {
30+
thread.join();
31+
} catch (InterruptedException e) {
32+
e.printStackTrace();
33+
}
34+
System.out.println(Thread.currentThread().getName());
35+
}
36+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package org.tj.study.thread.locks;
2+
3+
/**
4+
* Created by 001 on 16/8/24.
5+
*/
6+
public class DemoLock {
7+
}

0 commit comments

Comments
 (0)