Skip to content

Commit 074ab5a

Browse files
committed
add leetcode : offer-37-38-59.1-59.2
1 parent a37e9c8 commit 074ab5a

4 files changed

+172
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package offer;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* @author : CodeWater
8+
* @create :2022-07-20-18:45
9+
* @Function Description :37.序列化二叉树
10+
*/
11+
public class _37SerializedBinaryTree {
12+
public class Codec {
13+
public String serialize(TreeNode root) {
14+
if(root == null) return "[]";
15+
StringBuilder res = new StringBuilder("[");
16+
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
17+
while(!queue.isEmpty()) {
18+
TreeNode node = queue.poll();
19+
if(node != null) {
20+
res.append(node.val + ",");
21+
queue.add(node.left);
22+
queue.add(node.right);
23+
}
24+
else res.append("null,");
25+
}
26+
res.deleteCharAt(res.length() - 1);
27+
res.append("]");
28+
return res.toString();
29+
}
30+
31+
public TreeNode deserialize(String data) {
32+
if(data.equals("[]")) return null;
33+
String[] vals = data.substring(1, data.length() - 1).split(",");
34+
TreeNode root = new TreeNode(Integer.parseInt(vals[0]));
35+
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
36+
int i = 1;
37+
while(!queue.isEmpty()) {
38+
TreeNode node = queue.poll();
39+
if(!vals[i].equals("null")) {
40+
node.left = new TreeNode(Integer.parseInt(vals[i]));
41+
queue.add(node.left);
42+
}
43+
i++;
44+
if(!vals[i].equals("null")) {
45+
node.right = new TreeNode(Integer.parseInt(vals[i]));
46+
queue.add(node.right);
47+
}
48+
i++;
49+
}
50+
return root;
51+
}
52+
}
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package offer;
2+
3+
import java.util.HashSet;
4+
import java.util.LinkedList;
5+
import java.util.List;
6+
7+
/**
8+
* @author : CodeWater
9+
* @create :2022-07-20-18:46
10+
* @Function Description :38.字符串的排列
11+
*/
12+
public class _38ArrangementOfString {
13+
class Solution {
14+
List<String > res = new LinkedList<>();
15+
char[] c;
16+
public String[] permutation(String s) {
17+
c = s.toCharArray();
18+
dfs(0);
19+
return res.toArray( new String[res.size()] );
20+
}
21+
22+
public void dfs( int x ){
23+
if ( x == c.length - 1){
24+
res.add( String.valueOf(c));
25+
return ;
26+
}
27+
HashSet<Character> set = new HashSet<>();
28+
for( int i = x ; i < c.length ; i++ ){
29+
if( set.contains(c[i]) ) continue;
30+
set.add( c[i] );
31+
// 将第i位字符固定在x位
32+
swap( i , x );
33+
dfs( x + 1 );
34+
swap( i , x );
35+
}
36+
}
37+
38+
public void swap(int a , int b){
39+
char temp = c[a];
40+
c[a] = c[b];
41+
c[b] = temp;
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package offer;
2+
3+
/**
4+
* @author : CodeWater
5+
* @create :2022-07-20-15:58
6+
* @Function Description :59.1滑动窗口的最大值
7+
*/
8+
public class _59_1TheMaximumValueOfSlidingWindow {
9+
class Solution {
10+
11+
public int[] maxSlidingWindow(int[] nums, int k) {
12+
int length = nums.length , hh = 0 , tt = -1 ;
13+
// 虽然题目说数组不空,但是测试用例有。。。。。
14+
if( length == 0 ) return new int[0];
15+
// length - k + 1求得最大值个数
16+
int[] q = new int[length] , res = new int[length - k + 1];
17+
18+
for( int i = 0 ; i < length ; i++ ){
19+
if( hh <= tt && q[hh] < i - k + 1 )hh ++;
20+
while( hh <= tt && nums[q[tt]] <= nums[i] ) tt--;
21+
q[++tt] = i;
22+
// i - k + 1让res数组下标从0开始,不然开的数组会越界
23+
if( i >= k - 1 ) res[i - k + 1] = nums[q[hh]] ;
24+
}
25+
26+
return res;
27+
}
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package offer;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* @author : CodeWater
8+
* @create :2022-07-20-16:32
9+
* @Function Description :59.2 队列的最大值
10+
*/
11+
public class _59_2TheMaximumValueOfTheQueue {
12+
class MaxQueue {
13+
14+
Deque<Integer> res , max ;
15+
public MaxQueue() {
16+
res = new LinkedList<>();
17+
max = new LinkedList<>();
18+
}
19+
20+
public int max_value() {
21+
if( max.isEmpty() ) return -1;
22+
return max.peekFirst();
23+
}
24+
25+
public void push_back(int value) {
26+
res.addLast( value );
27+
while( !max.isEmpty() && max.peekLast() < value ) max.removeLast();
28+
max.addLast(value);
29+
}
30+
31+
public int pop_front() {
32+
if( res.isEmpty() ) return -1;
33+
int temp = res.pollFirst();
34+
if( temp == max.peekFirst() ) max.removeFirst();
35+
return temp;
36+
}
37+
}
38+
39+
/**
40+
* Your MaxQueue object will be instantiated and called as such:
41+
* MaxQueue obj = new MaxQueue();
42+
* int param_1 = obj.max_value();
43+
* obj.push_back(value);
44+
* int param_3 = obj.pop_front();
45+
*/
46+
}

0 commit comments

Comments
 (0)