随笔分类 -  DFS

摘要:class Solution { public int numIslands(char[][] grid) { int count=0; for(int i=0;i=grid.length||j=grid[0].length||grid[i][j]!='1') return; grid[i][j]='2'; ... 阅读全文
posted @ 2017-10-26 01:48 Weiyu Wang 阅读(125) 评论(0) 推荐(0)
摘要:class Solution { public List wordBreak(String s, List wordDict) { Map> map=new HashMap>(); wordBreak(s, map, wordDict); return map.get(s); } private void wordBreak... 阅读全文
posted @ 2017-10-10 12:20 Weiyu Wang 阅读(140) 评论(0) 推荐(0)
摘要:class Solution { public List> partition(String s) { List> res=new ArrayList>(); generatePartition(0,new ArrayList(),res,s); return res; } private void generatePart... 阅读全文
posted @ 2017-10-05 01:43 Weiyu Wang 阅读(122) 评论(0) 推荐(0)
摘要:public class Solution { public void solve(char[][] board) { if (board.length == 0 || board[0].length == 0) return; int m = board.length; int n = board[0].length; bool... 阅读全文
posted @ 2017-10-04 10:54 Weiyu Wang 阅读(114) 评论(0) 推荐(0)
摘要:class Solution { int maxPathSum; public int maxPathSum(TreeNode root) { maxPathSum=Integer.MIN_VALUE; seachPathSum(root); return maxPathSum; } private int seac... 阅读全文
posted @ 2017-10-03 02:12 Weiyu Wang 阅读(126) 评论(0) 推荐(0)
摘要:class Solution { public List> pathSum(TreeNode root, int sum) { List> res=new ArrayList>(); pathSum(root, sum, new ArrayList(), res); return res; } private void pa... 阅读全文
posted @ 2017-09-30 02:22 Weiyu Wang 阅读(127) 评论(0) 推荐(0)
摘要:class Solution { ListNode lnode=null; public TreeNode sortedListToBST(ListNode head) { int size=0; ListNode p=head; while(p!=null) { p=p.next; ... 阅读全文
posted @ 2017-09-30 02:08 Weiyu Wang 阅读(155) 评论(0) 推荐(0)
摘要:public class Solution { public TreeNode buildTree(int[] inorder, int[] postorder) { return buildTree(inorder, 0, inorder.length-1, postorder, 0, postorder.length-1); } private TreeNo... 阅读全文
posted @ 2017-09-29 12:30 Weiyu Wang 阅读(112) 评论(0) 推荐(0)
摘要:public class Solution { public TreeNode buildTree(int[] preorder, int[] inorder) { return buildTree(preorder, 0, preorder.length-1, inorder, 0, inorder.length); } private TreeNode bu... 阅读全文
posted @ 2017-09-29 12:12 Weiyu Wang 阅读(119) 评论(0) 推荐(0)
摘要:class Solution { public List generateTrees(int n) { if(n==0) return new ArrayList(); return generateTrees(1, n); } private List generateTrees(int i, int j) { ... 阅读全文
posted @ 2017-09-29 02:19 Weiyu Wang 阅读(148) 评论(0) 推荐(0)
摘要:class Solution { public List restoreIpAddresses(String s) { List res=new ArrayList(); restoreIpAddresses("", 0, 0, s, res); return res; } private void restoreIpAdd... 阅读全文
posted @ 2017-09-29 01:52 Weiyu Wang 阅读(179) 评论(0) 推荐(0)
摘要:public class Solution { public List> subsetsWithDup(int[] nums) { List> res=new ArrayList>(); Arrays.sort(nums); subsetsWithDup(0, new ArrayList(), res, nums); ret... 阅读全文
posted @ 2017-09-28 12:39 Weiyu Wang 阅读(128) 评论(0) 推荐(0)
摘要:class Solution { public boolean isScramble(String s1, String s2) { if(s1.length()==0||s1.equals(s2)) return true; int[] cnt=new int[128]; for(int i=0;i<s1.leng... 阅读全文
posted @ 2017-09-28 06:56 Weiyu Wang 阅读(117) 评论(0) 推荐(0)
摘要:class Solution { public boolean exist(char[][] board, String word) { if(board.length==0||board[0].length==0) return false; boolean[][] used=new boolean[board.length][b... 阅读全文
posted @ 2017-09-27 05:12 Weiyu Wang 阅读(156) 评论(0) 推荐(0)
摘要:class Solution { public List> subsets(int[] nums) { List> res=new ArrayList>(); subsets(0, new ArrayList(), res, nums); return res; } private void subsets(int idx,... 阅读全文
posted @ 2017-09-27 05:02 Weiyu Wang 阅读(175) 评论(0) 推荐(0)
摘要:class Solution { public List> combine(int n, int k) { List> res=new ArrayList>(); combine(n, k, new ArrayList(), res); return res; } private void combine(int num, ... 阅读全文
posted @ 2017-09-27 04:57 Weiyu Wang 阅读(142) 评论(0) 推荐(0)
摘要:class Solution { public int totalNQueens(int n) { int[][] board=new int[n][n]; return totalNQueens(0, board); } private int totalNQueens(int i, int[][] board) { ... 阅读全文
posted @ 2017-09-26 01:12 Weiyu Wang 阅读(159) 评论(0) 推荐(0)
摘要:class Solution { public List> solveNQueens(int n) { int[][] board=new int[n][n]; List> res=new ArrayList>(); nQueens(0,board,res); return res; } private vo... 阅读全文
posted @ 2017-09-25 12:45 Weiyu Wang 阅读(174) 评论(0) 推荐(0)
摘要:public class Solution { public List> permuteUnique(int[] nums) { List> ret=new ArrayList>(); Arrays.sort(nums); boolean[] used=new boolean[nums.length]; permute(ne... 阅读全文
posted @ 2017-09-25 08:07 Weiyu Wang 阅读(128) 评论(0) 推荐(0)
摘要:class Solution { public List> combinationSum2(int[] candidates, int target) { List> ret=new ArrayList>(); Arrays.sort(candidates); boolean[] used=new boolean[candidates.le... 阅读全文
posted @ 2017-09-24 04:22 Weiyu Wang 阅读(95) 评论(0) 推荐(0)