diff --git a/20.valid-parentheses.java b/20.valid-parentheses.java new file mode 100644 index 0000000..0b8f1f4 --- /dev/null +++ b/20.valid-parentheses.java @@ -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 op_type_stack = new ArrayDeque(); + for(int i=0;i generateParenthesis(int n) { + List r = new ArrayList(); + dfsGenParenthesis(r, "", 0, 0, n); + return r; + } + + private void dfsGenParenthesis(List 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 + diff --git a/35.search-insert-position.java b/35.search-insert-position.java new file mode 100644 index 0000000..24a16e1 --- /dev/null +++ b/35.search-insert-position.java @@ -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= target) { + r = m; + } else { + l = m + 1; + } + } + return l; + } +} +// @lc code=end + diff --git a/45.jump-game-ii.java b/45.jump-game-ii.java new file mode 100644 index 0000000..919608b --- /dev/null +++ b/45.jump-game-ii.java @@ -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> solveNQueens(int n) { + int[][] m = createMap(n); + List> r = new ArrayList<>(); + dfsNQueen(r, m, 0, n, 0); + return r; + } + + private void dfsNQueen(List> 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{ + 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{ + return new Point(pos.x+1, pos.y); + })) { + return false; + } + } + return true; + } + + private boolean checkLeftSlash(int[][] m, int n) { + for(int i = 0; i{ + 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{ + 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 incrStrategy) { + int c = 0; + if(m[pos.x][pos.y] !=0) { + c++; + } + for(int i=0;i=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 buildRetStr(int[][] m) { + List r = new ArrayList(); + if(m == null) { + return r; + } + for(int i = 0; i> combine(int n, int k) { + List> r = new ArrayList>(); + List rt = new ArrayList(); + dfsIterate(r,rt, 1, n, k); + return r; + } + + private void dfsIterate(List> r, List 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 p = shallowCopy(list); + p.add(i); + dfsIterate(r, p, i+1, n, k); + } + } + + private List shallowCopy(List list) { + return new ArrayList<>(list); + } +} +// @lc code=end +