Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 45ea5a3

Browse files
committed
td
1 parent 463ca38 commit 45ea5a3

File tree

10 files changed

+250
-0
lines changed

10 files changed

+250
-0
lines changed

4/PostOrder.class

1.44 KB
Binary file not shown.

4/PostOrder.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.*;
2+
3+
public class PostOrder {
4+
public List<Integer> postOrder1(TreeNode root) {
5+
List<Integer> result = new ArrayList<Integer>();
6+
if (root == null) {
7+
return result;
8+
}
9+
Deque<TreeNode> dque = new LinkedList<TreeNode>();
10+
dque.offerFirst(root);
11+
while (!dque.isEmpty()) {
12+
TreeNode cur = dque.pollFirst();
13+
result.add(cur.key);
14+
if (cur.left != null) {
15+
dque.offerFirst(cur.left);
16+
}
17+
if (cur.right != null) {
18+
dque.offerFirst(cur.right);
19+
}
20+
}
21+
Collections.reverse(result);
22+
return result;
23+
}
24+
public static void main(String[] arsgs) {
25+
TreeNode tn1 = new TreeNode(5);
26+
TreeNode tn2 = new TreeNode(3);
27+
TreeNode tn3 = new TreeNode(8);
28+
TreeNode tn4 = new TreeNode(1);
29+
TreeNode tn5 = new TreeNode(4);
30+
TreeNode tn6 = new TreeNode(11);
31+
32+
tn1.left = tn2;
33+
tn1.right = tn3;
34+
tn2.left = tn4;
35+
tn2.right = tn5;
36+
tn3.right = tn6;
37+
38+
PostOrder po = new PostOrder();
39+
List<Integer> list = po.postOrder1(tn1);
40+
System.out.println(list.toString());
41+
42+
43+
}
44+
}
45+
46+
/*
47+
5
48+
49+
/ \
50+
51+
3 8
52+
53+
/ \ \
54+
55+
1 4 11
56+
57+
Post-order traversal is [1, 4, 3, 11, 8, 5]
58+
59+
60+
61+
*/

4/PostOrder.java~

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
public class PostOrder {
2+
3+
4+
5+
}

7/#RemoveSpace.java#

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
public class RemoveSpace {
2+
public String removeSpace(String input) {
3+
if (input.isEmpty()) { // check empty, empty -> return
4+
return input;
5+
}
6+
char[] array = input.toCharArray(); // String to Char array
7+
int end = 0; // end = 0
8+
for (int i = 0; i < array.length; i++) { // loop array.length times
9+
if (array[i] == ' ' && (i == 0 || array[i-1] == ' ')) { //
10+
continue;
11+
}
12+
array[end++] = array[i];
13+
}
14+
if (end > 0 && array[end - 1] == ' ') {
15+
return new String(array, 0, end - 1);
16+
}
17+
return new String(array, 0, end);
18+
}
19+
20+
public static void main(String[] args) {
21+
RemoveSpace rm = new RemoveSpace();
22+
System.out.println(rm.removeSpace(" I love MTV "));
23+
}
24+
}
25+
26+
27+
/*
28+
string = " I love MTV "
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
*/
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+

7/RemoveSpace.class

855 Bytes
Binary file not shown.

7/RemoveSpace.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
public class RemoveSpace {
2+
public String removeSpace(String input) {
3+
if (input.isEmpty()) {
4+
return input;
5+
}
6+
char[] array = input.toCharArray();
7+
int end = 0;
8+
for (int i = 0; i < array.length; i++) {
9+
if (array[i] == ' ' && (i == 0 || array[i-1] == ' ')) {
10+
continue;
11+
}
12+
array[end++] = array[i];
13+
}
14+
if (end > 0 && array[end - 1] == ' ') {
15+
return new String(array, 0, end - 1);
16+
}
17+
return new String(array, 0, end);
18+
}
19+
20+
public static void main(String[] args) {
21+
RemoveSpace rm = new RemoveSpace();
22+
System.out.println(rm.removeSpace(" I love MTV "));
23+
}
24+
}
25+
26+
27+
/*
28+
string = " I love MTV "
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
44+
45+
*/
46+
47+
48+
49+
50+
51+
52+
53+
54+
55+
56+

7/RemoveSpace.java~

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
public class RemoveSpace {
2+
public String removeSpace(String input) {
3+
if (input.isEmpty()) {
4+
return input;
5+
}
6+
char[] array = input.toCharArray();
7+
int end = 0;
8+
for (int i = 0; i < array.length; i++) {
9+
if (array[i] == ' ' && (i == 0 || array[i-1] == ' ')) {
10+
continue;
11+
}
12+
if (end > 0 && array[end - 1] == ' ') {
13+
return new String(array, 0, end - 1)
14+
15+
}
16+
return new String(array, 0, end);
17+
}
18+
}
19+
20+
}

8/Permutation.class

1.38 KB
Binary file not shown.

8/Permutation.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
3+
public class Permutation {
4+
5+
public List<String> permutation(String set) {
6+
List<String> result = new ArrayList<String>();
7+
if (set == null) return result;
8+
char[] array = set.toCharArray();
9+
helper(array, 0, result);
10+
return result;
11+
}
12+
13+
private void helper(char[] array, int index, List<String> result) {
14+
if (index == array.length) {
15+
result.add(new String(array));
16+
return;
17+
}
18+
Set<Character> used = new HashSet<Character>();
19+
for (int i = index; i < array.length; i++) {
20+
if (used.add(array[i])) {
21+
swap(array, i, index);
22+
helper(array, index + 1, result);
23+
swap(array, i, index);
24+
}
25+
}
26+
}
27+
28+
private void swap(char[] array, int left, int right) {
29+
char temp = array[left];
30+
array[left] = array[right];
31+
array[right] = temp;
32+
}
33+
34+
public static void main(String[] args) {
35+
Permutation per = new Permutation();
36+
List<String> result;
37+
result = per.permutation("ab");
38+
System.out.println(result.toString());
39+
}
40+
}

8/Permutation.java~

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class Permutation() {
2+
public List<String> permutation(String set) {
3+
4+
}
5+
6+
public static void main(String[] args) {
7+
Permutation per = new Permutation();
8+
List<String> result;
9+
result = per.permutation("ab");
10+
System.out.println(result.toString());
11+
}
12+
}

0 commit comments

Comments
 (0)