2025-05-17提交
This commit is contained in:
78
20.valid-parentheses.java
Normal file
78
20.valid-parentheses.java
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* @lc app=leetcode id=20 lang=java
|
||||
*
|
||||
* [20] Valid Parentheses
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
|
||||
import java.util.Deque;
|
||||
|
||||
class Solution {
|
||||
public boolean isValid(String s) {
|
||||
int br_1_cnt = 0, br_2_cnt = 0, br_3_cnt = 0;
|
||||
Deque<Integer> op_type_stack = new ArrayDeque<Integer>();
|
||||
for(int i=0;i<s.length();i++) {
|
||||
char ch = s.charAt(i);
|
||||
if(isOpenBr(ch)) {
|
||||
int op_type = getOpenBrType(ch);
|
||||
op_type_stack.push(op_type);
|
||||
if(op_type == 1) {
|
||||
br_1_cnt++;
|
||||
} else if(op_type == 2) {
|
||||
br_2_cnt++;
|
||||
} else if(op_type == 3) {
|
||||
br_3_cnt++;
|
||||
}
|
||||
} else {
|
||||
int close_type = getCloseBrType(ch);
|
||||
if(op_type_stack.isEmpty()) {
|
||||
return false;
|
||||
} else if(op_type_stack.peek() != close_type) {
|
||||
return false;
|
||||
}
|
||||
op_type_stack.pop();
|
||||
if(close_type == 1) {
|
||||
br_1_cnt--;
|
||||
} else if(close_type == 2) {
|
||||
br_2_cnt--;
|
||||
} else if(close_type == 3) {
|
||||
br_3_cnt--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return br_1_cnt == 0 && br_2_cnt == 0 && br_3_cnt == 0;
|
||||
}
|
||||
|
||||
private boolean isOpenBr(char ch) {
|
||||
return ch == '(' || ch == '[' || ch == '{';
|
||||
}
|
||||
|
||||
private int getOpenBrType(char ch) {
|
||||
switch(ch) {
|
||||
case '(':
|
||||
return 1;
|
||||
case '[':
|
||||
return 2;
|
||||
case '{':
|
||||
return 3;
|
||||
default:
|
||||
throw new RuntimeException("invalid open brack");
|
||||
}
|
||||
}
|
||||
|
||||
private int getCloseBrType(char ch) {
|
||||
switch(ch) {
|
||||
case ')':
|
||||
return 1;
|
||||
case ']':
|
||||
return 2;
|
||||
case '}':
|
||||
return 3;
|
||||
default:
|
||||
throw new RuntimeException("invalid close brack");
|
||||
}
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
59
21.merge-two-sorted-lists.java
Normal file
59
21.merge-two-sorted-lists.java
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* @lc app=leetcode id=21 lang=java
|
||||
*
|
||||
* [21] Merge Two Sorted Lists
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
/**
|
||||
* Definition for singly-linked list.
|
||||
* public class ListNode {
|
||||
* int val;
|
||||
* ListNode next;
|
||||
* ListNode() {}
|
||||
* ListNode(int val) { this.val = val; }
|
||||
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
|
||||
* }
|
||||
*/
|
||||
class Solution {
|
||||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
|
||||
if(list1 == null || list2 == null) {
|
||||
return list1 == null?list2:list1;
|
||||
}
|
||||
ListNode h = null, cur = null;
|
||||
while(list1 != null && list2!=null) {
|
||||
if(list1.val <= list2.val) {
|
||||
if(h == null) {
|
||||
h = cur = list1;
|
||||
list1 = list1.next;
|
||||
h.next = null;
|
||||
} else {
|
||||
cur.next = list1;
|
||||
list1 = list1.next;
|
||||
cur = cur.next;
|
||||
cur.next = null;
|
||||
}
|
||||
} else {
|
||||
if(h == null) {
|
||||
h = cur = list2;
|
||||
list2 = list2.next;
|
||||
h.next = null;
|
||||
} else {
|
||||
cur.next = list2;
|
||||
list2 = list2.next;
|
||||
cur = cur.next;
|
||||
cur.next = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (list1 !=null) {
|
||||
cur.next = list1;
|
||||
}
|
||||
if(list2 != null) {
|
||||
cur.next = list2;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
33
22.generate-parentheses.java
Normal file
33
22.generate-parentheses.java
Normal file
@@ -0,0 +1,33 @@
|
||||
/*
|
||||
* @lc app=leetcode id=22 lang=java
|
||||
*
|
||||
* [22] Generate Parentheses
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public List<String> generateParenthesis(int n) {
|
||||
List<String> r = new ArrayList<String>();
|
||||
dfsGenParenthesis(r, "", 0, 0, n);
|
||||
return r;
|
||||
}
|
||||
|
||||
private void dfsGenParenthesis(List<String> r, String s, int i, int k, int n) {
|
||||
if(i == n && k == 0) {
|
||||
r.add(s);
|
||||
return;
|
||||
} else if(i > n) {
|
||||
return;
|
||||
}
|
||||
if(i < n) {
|
||||
String t = s + "(";
|
||||
dfsGenParenthesis(r, t, i+1, k+1, n);
|
||||
}
|
||||
if(k > 0) {
|
||||
String t = s + ")";
|
||||
dfsGenParenthesis(r, t, i, k-1, n);
|
||||
}
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
24
35.search-insert-position.java
Normal file
24
35.search-insert-position.java
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* @lc app=leetcode id=35 lang=java
|
||||
*
|
||||
* [35] Search Insert Position
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int searchInsert(int[] nums, int target) {
|
||||
int l=0, r = nums.length;
|
||||
int m;
|
||||
while(l<r) {
|
||||
m = l + (r-l)/2;
|
||||
if(nums[m] >= target) {
|
||||
r = m;
|
||||
} else {
|
||||
l = m + 1;
|
||||
}
|
||||
}
|
||||
return l;
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
41
45.jump-game-ii.java
Normal file
41
45.jump-game-ii.java
Normal file
@@ -0,0 +1,41 @@
|
||||
/*
|
||||
* @lc app=leetcode id=45 lang=java
|
||||
*
|
||||
* [45] Jump Game II
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public int jump(int[] nums) {
|
||||
// create array and zset
|
||||
int[] dp = new int[nums.length];
|
||||
zset(dp, -1);
|
||||
dp[0] = 0;
|
||||
for(int i=0; i<nums.length; i++) {
|
||||
int w = nums[i];
|
||||
if(dp[i] < 0) {
|
||||
// unreachable
|
||||
continue;
|
||||
}
|
||||
for(int j= i+1; j<=i+w && j < nums.length; j++) {
|
||||
if(dp[j] < 0) {
|
||||
dp[j] = dp[i] + 1;
|
||||
} else {
|
||||
dp[j] = Math.min(dp[j], dp[i] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[nums.length-1];
|
||||
}
|
||||
|
||||
private void zset(int[] array, int v) {
|
||||
if(array == null) {
|
||||
return;
|
||||
}
|
||||
for(int i=0;i<array.length; i++) {
|
||||
array[i] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
183
51.n-queens.java
Normal file
183
51.n-queens.java
Normal file
@@ -0,0 +1,183 @@
|
||||
/*
|
||||
* @lc app=leetcode id=51 lang=java
|
||||
*
|
||||
* [51] N-Queens
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
class Solution {
|
||||
public List<List<String>> solveNQueens(int n) {
|
||||
int[][] m = createMap(n);
|
||||
List<List<String>> r = new ArrayList<>();
|
||||
dfsNQueen(r, m, 0, n, 0);
|
||||
return r;
|
||||
}
|
||||
|
||||
private void dfsNQueen(List<List<String>> r, int[][] m, int i, int n, int p) {
|
||||
if(i == n && pass(m, n, i)) {
|
||||
r.add(buildRetStr(m));
|
||||
return;
|
||||
} else if (i>=n) {
|
||||
return;
|
||||
}
|
||||
if(!pass(m,n, i)) {
|
||||
return;
|
||||
}
|
||||
for(int j = p; j<n*n; j++) {
|
||||
int x = j / n;
|
||||
int y = j % n;
|
||||
if(m[x][y] == 0) {
|
||||
m[x][y] = 1;
|
||||
dfsNQueen(r, m, i+1, n, j+1);
|
||||
m[x][y] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean pass(int[][] m , int n, int i) {
|
||||
// todo: check logic
|
||||
return checkRow(m, n, i) && checkColumn(m, n) && checkLeftSlash(m,n) && checkRightSlash(m,n);
|
||||
}
|
||||
|
||||
private boolean checkRow(int[][] m, int n, int s) {
|
||||
for(int i=0; i<n; i++) {
|
||||
if(!checkWithStrategy(m, new Point(i, 0), n, pos->{
|
||||
return new Point(pos.x, pos.y+1);
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
if(i < s && !checkRowNotEmpty(m, n, i)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkRowNotEmpty(int[][] m, int n, int s) {
|
||||
for(int i=0; i<n; i++) {
|
||||
if(m[s][i] != 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean checkColumn(int[][] m, int n) {
|
||||
for(int i = 0; i<n; i++) {
|
||||
if(!checkWithStrategy(m, new Point(0, i), n, pos->{
|
||||
return new Point(pos.x+1, pos.y);
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkLeftSlash(int[][] m, int n) {
|
||||
for(int i = 0; i<n; i++) {
|
||||
if(!checkWithStrategy(m, new Point(i, 0), n, pos->{
|
||||
return new Point(pos.x+1, pos.y+1);
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
if(!checkWithStrategy(m, new Point(0, i), n, pos->{
|
||||
return new Point(pos.x+1, pos.y+1);
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private boolean checkRightSlash(int[][] m, int n) {
|
||||
for(int i = 0; i<n; i++) {
|
||||
if(!checkWithStrategy(m, new Point(0, i), n, pos->{
|
||||
return new Point(pos.x+1, pos.y-1);
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
if(!checkWithStrategy(m, new Point(i, n-1), n, pos->{
|
||||
return new Point(pos.x+1, pos.y-1);
|
||||
})) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private boolean checkWithStrategy(int[][] m, Point pos, int n,Function<Point, Point> incrStrategy) {
|
||||
int c = 0;
|
||||
if(m[pos.x][pos.y] !=0) {
|
||||
c++;
|
||||
}
|
||||
for(int i=0;i<n-1;i++) {
|
||||
pos = incrStrategy.apply(pos);
|
||||
if(pos.x >=n || pos.y>= n || pos.x <0 || pos.y <0) {
|
||||
break;
|
||||
}
|
||||
if(m[pos.x][pos.y] != 0) {
|
||||
c++;
|
||||
if(c>1) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private int[][] createMap(int n) {
|
||||
int[][] r = new int[n][n];
|
||||
zset(r);
|
||||
return r;
|
||||
}
|
||||
|
||||
private void zset(int[][] m) {
|
||||
if(m == null) {
|
||||
return;
|
||||
}
|
||||
for (int i = 0; i < m.length; i++) {
|
||||
if(m[i]==null) {
|
||||
continue;
|
||||
}
|
||||
for(int j = 0; j < m[i].length; j++) {
|
||||
m[i][j] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<String> buildRetStr(int[][] m) {
|
||||
List<String> r = new ArrayList<String>();
|
||||
if(m == null) {
|
||||
return r;
|
||||
}
|
||||
for(int i = 0; i<m.length; i++) {
|
||||
int[] t = m[i];
|
||||
if(t == null) {
|
||||
r.add("");
|
||||
continue;
|
||||
}
|
||||
StringBuilder sbd = new StringBuilder();
|
||||
for(int j = 0; j<t.length; j++) {
|
||||
sbd.append(t[j]==0?".":"Q");
|
||||
}
|
||||
r.add(sbd.toString());
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
public static class Point {
|
||||
public int x;
|
||||
public int y;
|
||||
|
||||
public Point(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
36
77.combinations.java
Normal file
36
77.combinations.java
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
* @lc app=leetcode id=77 lang=java
|
||||
*
|
||||
* [77] Combinations
|
||||
*/
|
||||
|
||||
// @lc code=start
|
||||
class Solution {
|
||||
public List<List<Integer>> combine(int n, int k) {
|
||||
List<List<Integer>> r = new ArrayList<List<Integer>>();
|
||||
List<Integer> rt = new ArrayList<Integer>();
|
||||
dfsIterate(r,rt, 1, n, k);
|
||||
return r;
|
||||
}
|
||||
|
||||
private void dfsIterate(List<List<Integer>> r, List<Integer> list, int c, int n, int k) {
|
||||
if(list.size() == k) {
|
||||
r.add(shallowCopy(list));
|
||||
return;
|
||||
}
|
||||
if(c > n) {
|
||||
return;
|
||||
}
|
||||
for(int i = c; i<=n; i++) {
|
||||
List<Integer> p = shallowCopy(list);
|
||||
p.add(i);
|
||||
dfsIterate(r, p, i+1, n, k);
|
||||
}
|
||||
}
|
||||
|
||||
private List<Integer> shallowCopy(List<Integer> list) {
|
||||
return new ArrayList<>(list);
|
||||
}
|
||||
}
|
||||
// @lc code=end
|
||||
|
||||
Reference in New Issue
Block a user