File tree

9 files changed

+265
-24
lines changed

9 files changed

+265
-24
lines changed
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,38 @@
88
*/
99
public class Main {
1010
public static void main(String[] args) {
11-
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9};
12-
int target = 7;
13-
int left = 0, right = arr.length - 1;
14-
while(left < right) {
15-
int mid = (left + right) >>> 1;
16-
if(target > arr[mid]) {
17-
left = mid + 1;
18-
} else {
19-
right = mid;
11+
Scanner in = new Scanner(System.in);
12+
int m = in.nextInt();
13+
int n = in.nextInt();
14+
in.nextLine();
15+
int numStr = n / 3 * m;
16+
String[] strs = new String[numStr];
17+
char[][] chars = new char[numStr][5];
18+
char[][] map = new char[m][n];
19+
for (int i = 0; i < numStr; i++) {
20+
strs[i] = in.next();
21+
chars[i] = strs[i].toCharArray();
22+
}
23+
int k = 0;
24+
for (int i = 0, j = 0; i < m; i++,j++) {
25+
k = 0;
26+
map[j][k++] = chars[i][4];
27+
map[j][k++] = chars[i][0];
28+
map[j][k++] = chars[i][2];
29+
30+
}
31+
for (int i = numStr - 1, j = 0; i >= m; i--, j++) {
32+
k = 3;
33+
map[j][k++] = chars[i][4];
34+
map[j][k++] = chars[i][0];
35+
map[j][k++] = chars[i][2];
36+
}
37+
38+
for (int i = 0; i < m; i++) {
39+
for (int j = 0; j < n; j++) {
40+
System.out.print(map[i][j]);
2041
}
42+
System.out.println();
2143
}
22-
//return arr[left] == target ? left : -1;
23-
System.out.println(left);
2444
}
2545
}
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ public static void main(String[] args) {
99
int m = in.nextInt();
1010
int n = in.nextInt();
1111
in.nextLine();
12-
//输入的字符串数量为:(n+2)/3*m;4*6的为:(4+2)/3*4=8
12+
//输入的字符串数量为:(n+2)/3*m; 4*6的为:(4+2)/3*4=8
1313
int numStr = (n + 2) / 3 * m;
14-
System.out.println(numStr);
1514
String[] strs = new String[numStr];
1615
char[][] chars = new char[numStr][5];
1716
char[][] map = new char[m][n];
1817
for (int i = 0; i < numStr; i++) {
1918
strs[i] = in.next();
20-
}
21-
22-
for (int i = 0; i < numStr; i++) {
2319
chars[i] = strs[i].toCharArray();
2420
}
2521

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package tencent_cloud;
2+
3+
4+
import java.util.*;
5+
6+
public class Solution {
7+
8+
private static TreeNode root;
9+
10+
11+
static class TreeNode {
12+
int val;
13+
TreeNode left;
14+
TreeNode right;
15+
16+
public TreeNode(int val) {
17+
this.val = val;
18+
this.left = null;
19+
this.right = null;
20+
}
21+
22+
public TreeNode(int val, TreeNode left, TreeNode right) {
23+
this.val = val;
24+
this.left = null;
25+
this.right = null;
26+
}
27+
}
28+
29+
public static LinkedList<Integer> isBST(TreeNode root, LinkedList<Integer> list) {
30+
if (root == null) return list;
31+
isBST(root.left, list);
32+
list.add(root.val);
33+
isBST(root.right, list);
34+
return list;
35+
}
36+
37+
public static void add(int value) {
38+
root = add(root, value);
39+
}
40+
41+
private static TreeNode add(TreeNode root, int value) {
42+
if (root == null) {
43+
return new TreeNode(value, null, null);
44+
}
45+
int compare = (value - root.val);
46+
if (compare > 0) root.right = add(root.right, value);
47+
if (compare < 0) root.left = add(root.left, value);
48+
return root;
49+
}
50+
51+
public static void main(String[] args) {
52+
Solution tree = new Solution();
53+
int[] arr = {9,4,10,2,5,8,7,6,1,3};
54+
for (int i = 0; i < arr.length; i++) {
55+
tree.add(arr[i]);
56+
}
57+
58+
LinkedList<Integer> list = new LinkedList<>();
59+
60+
System.out.println(isBST(root, list));
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package tencent_cloud;
2+
3+
4+
import java.util.*;
5+
6+
public class Solution2 {
7+
8+
private static TreeNode root;
9+
10+
static class TreeNode {
11+
int val;
12+
TreeNode left;
13+
TreeNode right;
14+
15+
public TreeNode() {
16+
this.val = val;
17+
this.left = null;
18+
this.right = null;
19+
}
20+
21+
public TreeNode(int val) {
22+
this.val = val;
23+
this.left = null;
24+
this.right = null;
25+
}
26+
}
27+
28+
public static boolean isValidBST(TreeNode root) {
29+
long pre = Long.MIN_VALUE;
30+
Stack<TreeNode> stack = new Stack<>();
31+
while (root != null) {
32+
stack.push(root);
33+
root = root.left;
34+
}
35+
while (!stack.empty()) {
36+
TreeNode temp = stack.pop();
37+
if (temp.val <= pre)
38+
return false;
39+
pre = temp.val;
40+
if (temp.right != null) {
41+
TreeNode r = temp.right;
42+
while (r != null) {
43+
stack.push(r);
44+
r = r.left;
45+
}
46+
}
47+
}
48+
return true;
49+
}
50+
51+
public static void addTreeNode(TreeNode node, String[] nodes) {
52+
// if (root == null) root = new TreeNode(data);
53+
//
54+
// //采用随机方式创建二叉树
55+
// if (Math.random() > 0.5) {
56+
// if (node.left == null) {
57+
// node.left = new TreeNode(data);
58+
// } else {
59+
// addTreeNode(node.left, data);
60+
// }
61+
// } else {
62+
// if (node.right == null) {
63+
// node.right = new TreeNode(data);
64+
// } else {
65+
// addTreeNode(node.right, data);
66+
// }
67+
// }
68+
69+
}
70+
71+
public static void creat(int[] nodes) {
72+
List<TreeNode> datas = new ArrayList<>();
73+
// 将一个数组的值依次转换为Node节点
74+
for (int val : nodes) {
75+
datas.add(new TreeNode(val));
76+
}
77+
// 第一个数为根节点
78+
root = datas.get(0);
79+
// 建立二叉树
80+
for (int i = 0; i < nodes.length / 2; i++) {
81+
// 左孩子
82+
datas.get(i).left = datas.get(i * 2 + 1);
83+
// 右孩子
84+
if (i * 2 + 2 < datas.size()) {//避免偶数的时候 下标越界
85+
datas.get(i).right = datas.get(i * 2 + 2);
86+
}
87+
}
88+
}
89+
90+
91+
public static void levelOrder(TreeNode root) {
92+
if (root == null) return;
93+
94+
Queue<TreeNode> queue = new LinkedList<>();
95+
queue.add(root);
96+
while (!queue.isEmpty()) {
97+
TreeNode node = queue.poll();
98+
System.out.print(node.val + " ");
99+
if (node.left != null) queue.add(node.left);
100+
if (node.right != null) queue.add(node.right);
101+
}
102+
}
103+
104+
public static void main(String[] args) {
105+
int[] nodes = {1, 8, 9, 11, 10, 6, 7, 3};
106+
creat(nodes);
107+
levelOrder(root);
108+
}
109+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package tencent_cloud;
2+
3+
4+
import java.util.*;
5+
6+
public class Solution3 {
7+
public static void main(String[] args) {
8+
9+
}
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package tw;
2+
3+
import java.util.*;
4+
5+
public class Solution {
6+
public static void main(String[] args) {
7+
8+
}
9+
}
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ public static void printList(Node head) {
4040
System.out.println();
4141
}
4242

43-
// HashSet-O(n), 删除无序链表重复元素
43+
/**
44+
* 删除无序链表重复元素 (保留一个重复)
45+
* HashSet-O(n)
46+
*/
4447
public static void removeListI(Node head) {
4548
if (head == null) return;
4649
Node pre = head;
@@ -58,7 +61,12 @@ public static void removeListI(Node head) {
5861
}
5962
}
6063

61-
//O(n^2), 删除无序链表重复元素
64+
/**
65+
* 删除无序链表重复元素 (保留一个重复)
66+
* O(n^2)
67+
*
68+
* @param head
69+
*/
6270
public static void removeListII(Node head) {
6371
if (head == null) return;
6472
Node pre = null;
@@ -80,7 +88,12 @@ public static void removeListII(Node head) {
8088
}
8189

8290

83-
//O(n^2), 删除有序链表重复元素
91+
/**
92+
* 删除有序链表重复元素 (保留一个重复)
93+
* O(n^2)
94+
*
95+
* @param head
96+
*/
8497
public static void removeSortList(Node head) {
8598
if (head == null) return;
8699
Node pre = head;
@@ -95,6 +108,32 @@ public static void removeSortList(Node head) {
95108
}
96109
}
97110

111+
/**
112+
* 删除有序链表重复元素 (不保留重复元素)
113+
* 先用end和end.next元素比较,
114+
* - 如果相等,则left移动。之后循环判断left和right两个指针是否指向同一个元素。如果相等,则说明没有相同的元素。哨兵cur向后移动。反之,则说明存在相同的元素,哨兵则将当前next指针指向right.next,将重复元素都删除
115+
*
116+
* @param head
117+
*/
118+
public static Node removeNoRepeat(Node head) {
119+
if (head == null) return head;
120+
Node dummy = new Node(0);
121+
dummy.next = head;
122+
Node node = dummy;
123+
while (node != null && node.next != null) {
124+
Node nextNode = node.next.next;
125+
while (nextNode != null && nextNode.value == node.next.value) {
126+
nextNode = nextNode.next;
127+
}
128+
if (node.next.next == nextNode) {
129+
node = node.next;
130+
} else {
131+
node.next = nextNode;
132+
}
133+
}
134+
return dummy.next;
135+
}
136+
98137
public static void main(String[] args) throws IOException {
99138
//BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
100139
//int n = Integer.parseInt(bufferedReader.readLine());

0 commit comments

Comments
 (0)