Skip to content

Commit 4891293

Browse files
committed
#first commit
1 parent 657138b commit 4891293

File tree

8 files changed

+253
-0
lines changed

8 files changed

+253
-0
lines changed

src/com/blankj/easy/_004/No04.java

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.blankj.easy._004;
2+
3+
public class No04 {
4+
public static void main(String[] args) {
5+
String str = "We are happy";
6+
char[] charArray = str.toCharArray();
7+
System.out.println(change(charArray));
8+
}
9+
10+
private static String change(char[] charArray) {
11+
int n = charArray.length;
12+
int count = 0;
13+
for (int i = 0; i < charArray.length; i++) {
14+
if (charArray[i] == ' ') {
15+
count++;
16+
}
17+
}
18+
if (count == 0) {
19+
return null;
20+
}
21+
char[] temp = new char[n + 2 * count];
22+
int j = n + 2 * count - 1;
23+
int i = n - 1;
24+
while (i >= 0) {
25+
if (charArray[i] == ' ') {
26+
temp[j] = '0';
27+
temp[j - 1] = '2';
28+
temp[j - 2] = '%';
29+
j = j - 3;
30+
} else {
31+
temp[j] = charArray[i];
32+
j--;
33+
}
34+
i--;
35+
}
36+
return new String(temp);
37+
}
38+
}

src/com/blankj/easy/_005/No05.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.blankj.easy._005;
2+
3+
import com.blankj.study.base.Node;
4+
5+
public class No05 {
6+
public static void main(String[] args) {
7+
Node node1 = new Node("A");
8+
Node node2 = new Node("B");
9+
Node node3 = new Node("C");
10+
Node node4 = new Node("D");
11+
Node node5 = new Node("E");
12+
node1.setNext(node2);
13+
node2.setNext(node3);
14+
node3.setNext(node4);
15+
node4.setNext(node5);
16+
Node newNode = reverse2(node1);
17+
while (newNode != null) {
18+
System.out.print(newNode.getData() + " ");
19+
newNode = newNode.getNext();
20+
}
21+
}
22+
23+
private static Node reverse(Node head) {
24+
if (head.getNext() == null) {
25+
return head;
26+
}
27+
Node reverseHead = reverse(head.getNext());
28+
head.getNext().setNext(head);
29+
head.setNext(null);
30+
return reverseHead;
31+
}
32+
33+
private static Node reverse2(Node head) {
34+
Node pre = head;
35+
Node cur = head.getNext();
36+
Node temp;
37+
while (cur != null) {
38+
temp = cur.getNext();
39+
cur.setNext(pre);
40+
pre = cur;
41+
cur = temp;
42+
}
43+
head.setNext(null);
44+
return pre;
45+
}
46+
}

src/com/blankj/easy/_006/No06.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.blankj.easy._006;
2+
3+
4+
import com.blankj.study.base.BinaryTreeNode;
5+
6+
public class No06 {
7+
private String midOrder;
8+
9+
public static void main(String[] args) {
10+
String preOrder = "12473568";
11+
String midOrder = "47215386";
12+
13+
BiTree tree = new BiTree(preOrder, midOrder, preOrder.length());
14+
tree.postRootTraverse(tree.root);
15+
}
16+
}
17+
18+
class BiTree {
19+
BinaryTreeNode root;
20+
21+
public BiTree(String preOrder, String midOrder, int count) {
22+
if (count <= 0) {
23+
return;
24+
}
25+
char c = preOrder.charAt(0);
26+
int i = 0;
27+
for (; i < count; i++) {
28+
if (midOrder.charAt(i) == c) break;
29+
}
30+
root = new BinaryTreeNode(c);
31+
root.setLchild(new BiTree(preOrder.substring(1, i + 1), midOrder.substring(0, i), i).root);
32+
root.setRchild(new BiTree(preOrder.substring(i + 1), midOrder.substring(i + 1), count - i - 1).root);
33+
}
34+
35+
public void postRootTraverse(BinaryTreeNode root) {
36+
if (root != null) {
37+
postRootTraverse(root.getLchild());
38+
postRootTraverse(root.getRchild());
39+
System.out.print(root.getData());
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.blankj.study.base;
2+
3+
public class BinaryTreeNode {
4+
private int data;
5+
private BinaryTreeNode LchildNode;
6+
private BinaryTreeNode RchildNode;
7+
8+
public BinaryTreeNode(int data) {
9+
super();
10+
this.data = data;
11+
}
12+
13+
public int getData() {
14+
return this.data;
15+
}
16+
17+
public void setData(int data) {
18+
this.data = data;
19+
}
20+
21+
public BinaryTreeNode getLchild() {
22+
return LchildNode;
23+
}
24+
25+
public void setLchild(BinaryTreeNode lchildNode) {
26+
this.LchildNode = lchildNode;
27+
}
28+
29+
public BinaryTreeNode getRchild() {
30+
return RchildNode;
31+
}
32+
33+
public void setRchild(BinaryTreeNode rchildNode) {
34+
this.RchildNode = rchildNode;
35+
}
36+
37+
38+
}

src/com/blankj/study/base/Node.java

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.blankj.study.base;
2+
3+
public class Node {
4+
private String data;
5+
private Node next;
6+
7+
public Node(String data) {
8+
super();
9+
this.data = data;
10+
}
11+
12+
public Node(String data, Node next) {
13+
super();
14+
this.data = data;
15+
this.next = next;
16+
}
17+
18+
public void setData(String data) {
19+
this.data = data;
20+
}
21+
22+
public String getData() {
23+
return data;
24+
}
25+
26+
public void setNext(Node next) {
27+
this.next = next;
28+
}
29+
30+
public Node getNext() {
31+
return next;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.blankj.study.singleton;
2+
3+
// 饿汉单例 线程安全
4+
public class HungrySingleton {
5+
private HungrySingleton() {
6+
}
7+
8+
;
9+
10+
private static final HungrySingleton instance = new HungrySingleton();
11+
12+
public static HungrySingleton getInstance() {
13+
return instance;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.blankj.study.singleton;
2+
3+
4+
//静态内部类单例
5+
public class InnerSingleton {
6+
private InnerSingleton() {
7+
}
8+
9+
;
10+
11+
public static InnerSingleton getInstance() {
12+
return SingletonHolder.instance;
13+
}
14+
15+
private static class SingletonHolder {
16+
private static final InnerSingleton instance = new InnerSingleton();
17+
}
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.blankj.study.singleton;
2+
3+
4+
// 懒汉单例 线程安全
5+
public class LazySingleton {
6+
private LazySingleton() {
7+
}
8+
9+
;
10+
11+
private static volatile LazySingleton instance = null;
12+
13+
public static LazySingleton getInstance() {
14+
if (instance == null) {
15+
synchronized (LazySingleton.class) {
16+
if (instance == null)
17+
instance = new LazySingleton();
18+
}
19+
}
20+
return instance;
21+
}
22+
}

0 commit comments

Comments
 (0)