Menu Sidebar
Menu

Recursion

[LintCode] Number of Islands

public int numIslands(boolean[][] grid) { // Write your code here if(grid == null) return 0; int res = 0; for(int i = 0 ; i < grid.length; i++) { for(int j = 0; j < grid[0].length; j++) { if(grid[i][j]) res++; dfs(grid,i,j); } } return res; } public void dfs(boolean[][] m, int i, int j) { […]

[LintCode] Permutations

/** * @param nums: A list of integers. * @return: A list of permutations. */ public ArrayList<ArrayList<Integer>> permute(ArrayList<Integer> nums) { // write your code here ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(nums == null || nums.size() == 0) return res; boolean[] visited = new boolean[nums.size()]; ArrayList<Integer> tmp = new ArrayList<Integer>(); dfs(res,visited, tmp, nums); return res; } […]

[LintCode] Subsets

public ArrayList<ArrayList<Integer>> subsets(ArrayList<Integer> S) { // write your code here ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>(); if(S == null || S.size() == 0) return res; Collections.sort(S); ArrayList<Integer> tmp = new ArrayList<Integer>(); res.add(tmp); dfs(res, S, tmp, 0); return res; } public void dfs(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> S, ArrayList<Integer> tmp, int pos) { for(int i = pos; i < […]

书脊

这青苔碧瓦堆, 俺曾睡风流觉, 将五十年兴亡看饱.

November 2024
M T W T F S S
 123
45678910
11121314151617
18192021222324
252627282930