Skip to content

Commit

Permalink
滑动窗口最大值、有效括号生成、
Browse files Browse the repository at this point in the history
  • Loading branch information
songjiang951130 committed Dec 8, 2023
1 parent 6ac1741 commit 8b73a29
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,40 @@ private void dfs(String digits, int i, List<String> res, String tempResult) {
dfs(digits, i + 1, res, tempResult + c);
}
}

public List<String> generateParenthesis(int n) {
char[] chars = new char[n * 2];
List<String> res = new ArrayList<>();
generate(chars, 0, 2 * n, res);
return res;
}

private void generate(char[] chars, int i, int len, List<String> res) {
if (i == len) {
if (validValue(chars)) {
res.add(new String(chars));
}
return;
}

chars[i] = '(';
generate(chars, i + 1, len, res);
chars[i] = ')';
generate(chars, i + 1, len, res);
}

private boolean validValue(char[] chars) {
int left = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == '(') {
left++;
} else if (chars[i] == ')') {
left--;
}
if (left < 0) {
return false;
}
}
return left == 0;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.github.songjiang951130.leetcode.heap;

import java.util.LinkedList;
import java.util.Queue;

public class ArrayMax {

/**
* 思路记录:滑动窗口 打算用linked list。做队列,然后用生成一个队列最大值 max;
* 出队的时候元素a 比较max 1、如果 a==max,重新生成队列最大值max;2、a < max 最大值存入res[] 3、a >max 不可能存在</>
*
* @param nums
* @param k
* @return
*/
public int[] maxSlidingWindow(int[] nums, int k) {
int[] sw = new int[k];
int[] res = new int[nums.length - k + 1];
int max = Integer.MIN_VALUE;
for (int i = 0; i < k; i++) {
sw[i] = nums[i];
max = Math.max(max, sw[i]);
}
int index = 0;
res[index++] = max;

for (int i = k; i < nums.length; i++) {
int num = nums[i];
int offer = offer(sw);
add(sw, num);

if (offer == max) {
max = getMax(sw);
} else if (num > max) {
max = num;
}
res[index++] = max;
}
return res;
}

private int getMax(int[] array) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < array.length; i++) {
max = Math.max(max, array[i]);
}
return max;
}

private int offer(int[] array) {
int res = array[0];
for (int i = 0; i < array.length - 1; i++) {
array[i] = array[i + 1];
}
return res;
}

private void add(int[] array, int val) {
array[array.length - 1] = val;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public void testLetterCombinations() {
assertEquals(9, strings.size());
assertEquals(Lists.newArrayList("ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"), strings);
}

public void testGenerateParenthesis() {
List<String> strings = letterCombination.generateParenthesis(1);
System.out.println(strings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.songjiang951130.leetcode.heap;

import junit.framework.TestCase;

import static org.junit.Assert.assertArrayEquals;

public class ArrayMaxTest extends TestCase {

public void testMaxSlidingWindow() {
//warning 超时
ArrayMax solution = new ArrayMax();
assertArrayEquals(new int[]{3, 3, 5, 5, 6, 7}, solution.maxSlidingWindow(new int[]{1, 3, -1, -3, 5, 3, 6, 7}, 3));
assertArrayEquals(new int[]{1}, solution.maxSlidingWindow(new int[]{1}, 1));
}
}

0 comments on commit 8b73a29

Please sign in to comment.