-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwk311.java
154 lines (132 loc) · 3.83 KB
/
wk311.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package weekly;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
public class wk311 {
//ranking: 1157 / 6710
//可以遍历
/* public int smallestEvenMultiple(int n) {
int ans = Integer.MAX_VALUE;
for (int i = n; i <= n * 2; i++) {
if (i % 2 == 0 && i % n == 0) {
ans = Math.min(ans, i);
}
}
return ans;
}*/
//也找规律吧
public int smallestEvenMultiple(int n) {
return (n % 2 + 1) * n;
}
//滑动窗口
public int longestContinuousSubstring(String s) {
char[] chars = s.toCharArray();
int max = 1;
int ans = 1;
for (int i = 1; i < chars.length; i++) {
if (chars[i] - chars[i - 1] == 1) {
ans++;
} else {
ans = 1;
}
max = Math.max(max, ans);
}
return max;
}
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
//层序遍历,注意只交换值即可
public TreeNode reverseOddLevels(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.add(root);
int step = 0;
while (!queue.isEmpty()) {
int size = queue.size();
List<TreeNode> cur = new ArrayList<>();
while (size-- > 0) {
TreeNode poll = queue.poll();
if (poll.left != null) {
queue.add(poll.left);
cur.add(poll.left);
}
if (poll.right != null) {
queue.add(poll.right);
cur.add(poll.right);
}
}
if (step % 2 == 0) {
for (int i = 0; i < cur.size()/2; i++) {
int a = cur.get(i).val;
int b = cur.get(cur.size() - i - 1).val;
cur.get(i).val=b;
cur.get(cur.size() - i - 1).val=a;
}
}
step++;
}
return root;
}
class Trie {
class Node {
//前缀统计
int preCount;
Node[] childrens = new Node[26];
}
Node root;
public Trie() {
root = new Node();
}
public void insert(String word) {
Node node = root;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
if (node.childrens[c - 'a'] == null) node.childrens[c - 'a'] = new Node();
node = node.childrens[c - 'a'];
node.preCount++;
}
}
/**
* Returns if the word is in the trie.
*/
public int search(String word) {
Node node = root;
int sum=0;
for (int i = 0; i < word.length(); i++) {
char c = word.charAt(i);
node = node.childrens[c - 'a'];
if (node == null) return 0;
sum+=node.preCount;
}
return sum;
}
}
//经典字典树
public int[] sumPrefixScores(String[] words) {
Trie t = new Trie();
for (int i = 0; i < words.length; i++) {
t.insert(words[i]);
}
int[] ans = new int[words.length];
for (int i = 0; i < words.length; i++) {
ans[i]=t.search(words[i]);
}
return ans;
}
public static void main(String[] args) {
System.out.println(Integer.toBinaryString(-1));
}
}