diff --git "a/src/BackTracking/N347円232円207円345円220円216円.java" "b/src/BackTracking/N347円232円207円345円220円216円.java" index d6065d1..a40657e 100644 --- "a/src/BackTracking/N347円232円207円345円220円216円.java" +++ "b/src/BackTracking/N347円232円207円345円220円216円.java" @@ -4,4 +4,8 @@ * Created by 周杰伦 on 2018年4月2日. */ public class N皇后 { + public static void main(String[] args) { + StringBuilder sb = new StringBuilder(); + + } } diff --git "a/src/BackTracking/346円225円260円345円255円227円351円224円256円346円211円223円345円207円272円345円255円227円346円257円215円347円273円204円345円220円210円.java" "b/src/BackTracking/346円225円260円345円255円227円351円224円256円346円211円223円345円207円272円345円255円227円346円257円215円347円273円204円345円220円210円.java" index f4e0943..dd25557 100644 --- "a/src/BackTracking/346円225円260円345円255円227円351円224円256円346円211円223円345円207円272円345円255円227円346円257円215円347円273円204円345円220円210円.java" +++ "b/src/BackTracking/346円225円260円345円255円227円351円224円256円346円211円223円345円207円272円345円255円227円346円257円215円347円273円204円345円220円210円.java" @@ -6,7 +6,7 @@ /** * Created by 周杰伦 on 2018年4月1日. */ -public class 数字键打出字母组合 { +public class 数字键打出字母组合 { public List letterCombinations(String digits) { if (digits == null || digits.equals(""))return new ArrayList(); List list = new ArrayList(); diff --git "a/src/BackTracking/350円204円221円346円264円236円345円244円247円345円274円200円347円232円204円347円237円251円351円230円265円346円237円245円346円211円276円345円215円225円350円257円215円.java" "b/src/BackTracking/350円204円221円346円264円236円345円244円247円345円274円200円347円232円204円347円237円251円351円230円265円346円237円245円346円211円276円345円215円225円350円257円215円.java" new file mode 100644 index 0000000..45eef09 --- /dev/null +++ "b/src/BackTracking/350円204円221円346円264円236円345円244円247円345円274円200円347円232円204円347円237円251円351円230円265円346円237円245円346円211円276円345円215円225円350円257円215円.java" @@ -0,0 +1,56 @@ +package BackTracking; + +/** + * Created by 周杰伦 on 2018年7月20日. + */ +public class 脑洞大开的矩阵查找单词 { + + static class StopMsgException extends RuntimeException { + + } + + static boolean flag; + + public boolean exist(char[][] board, String word) { + if (word.equals("")) { + return true; + } + int[][] visit = new int[board.length][board[0].length]; + flag = false; + try { + for (int i = 0; i < board.length; i++) { + for (int j = 0; j < board[0].length; j++) { + if (word.charAt(0) == board[i][j]) { + dfs(board, word, visit, i, j); + } + } + } + } catch (StopMsgException e) { + System.out.println(e); + } + return flag; + } + + public void dfs(char[][] board, String word, int[][] visit, int i, int j) { + if (word.equals("")) { + flag = true; + throw new StopMsgException(); + } + if (i> board.length - 1 || i < 0 || j> board[0].length - 1 || j < 0) { + return; + } + if (visit[i][j] == 1) { + return; + } + + if (word.charAt(0) == board[i][j]) { + visit[i][j] = 1; + dfs(board, word.length() == 1 ? "" : word.substring(1, word.length()), visit, i + 1, j); + dfs(board, word.length() == 1 ? "" : word.substring(1, word.length()), visit, i - 1, j); + dfs(board, word.length() == 1 ? "" : word.substring(1, word.length()), visit, i, j - 1); + dfs(board, word.length() == 1 ? "" : word.substring(1, word.length()), visit, i, j + 1); + visit[i][j] = 0; + } + } + +} diff --git a/src/BinSearch/Sqrt.java b/src/BinSearch/Sqrt.java index 5507d3c..98aa79f 100644 --- a/src/BinSearch/Sqrt.java +++ b/src/BinSearch/Sqrt.java @@ -1,5 +1,5 @@ package BinSearch; -import java.lang.Math.*; + /** * Created by 周杰伦 on 2018/3/16. */ diff --git a/src/BinSearch/singleNonDuplicate.java b/src/BinSearch/singleNonDuplicate.java index dee236f..b015c32 100644 --- a/src/BinSearch/singleNonDuplicate.java +++ b/src/BinSearch/singleNonDuplicate.java @@ -4,7 +4,11 @@ * Created by 周杰伦 on 2018/3/16. */ public class singleNonDuplicate { - public int singleNonDuplicate(int[] nums) { + public static void main(String[] args) { + int []num = {1,1,2,3,3,4,4,8,8}; + System.out.println(singleNonDuplicate(num)); + } + public int singleNonDuplicate1(int[] nums) { // binary search int n=nums.length, lo=0, hi=n/2; while (lo < hi) { @@ -14,4 +18,30 @@ public int singleNonDuplicate(int[] nums) { } return nums[2*lo]; } + public static int singleNonDuplicate(int[] nums) { + int l = 0,r = nums.length - 1; + if (nums.length <= 2) { + return nums[0]; + } + if (nums[1] != nums[0]) { + return nums[0]; + } + if (nums[nums.length - 1] != nums[nums.length - 2]) { + return nums[nums.length - 1]; + } + while (l <= r) { + int m = l + (r - l)/2; + if (nums[m + 1] != nums[m] && nums[m] != nums[m - 1]) { + return nums[m]; + } + if (m - 1>= 0 && nums[m] == nums[m - 1]) { + r = m - 1; + }else if (m + 1 < nums.length && nums[m] == nums[m + 1]) { + l = m + 1; + }else { + return nums[m]; + } + } + return nums[l]; + } } diff --git "a/src/DFS/345円233円276円347円232円204円350円277円236円351円200円232円345円210円206円351円207円217円344円270円252円346円225円260円.java" "b/src/DFS/345円233円276円347円232円204円350円277円236円351円200円232円345円210円206円351円207円217円344円270円252円346円225円260円.java" index fc1e42e..c031b1c 100644 --- "a/src/DFS/345円233円276円347円232円204円350円277円236円351円200円232円345円210円206円351円207円217円344円270円252円346円225円260円.java" +++ "b/src/DFS/345円233円276円347円232円204円350円277円236円351円200円232円345円210円206円351円207円217円344円270円252円346円225円260円.java" @@ -29,5 +29,6 @@ public void dfs (int [][]M, int j, int []visit) { } } + } diff --git "a/src/DFS/345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.java" "b/src/DFS/345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.java" new file mode 100644 index 0000000..f4e15df --- /dev/null +++ "b/src/DFS/345円262円233円345円261円277円347円232円204円346円234円200円345円244円247円351円235円242円347円247円257円.java" @@ -0,0 +1,41 @@ +package DFS; + +/** + * Created by 周杰伦 on 2018/7/18. + */ +public class 岛屿的最大面积 { + public static void main(String[] args) { + int [][]a = {{1,1,0,0,0},{1,1,0,0,0},{0,0,0,1,1},{0,0,0,1,1}}; + System.out.println(maxAreaOfIsland(a)); + } + public static int maxAreaOfIsland(int[][] grid) { + int [][]visit = new int[grid.length][grid[0].length]; + int max = 0; + for (int i = 0;i < grid.length;i ++) { + for (int j = 0;j < grid[0].length;j ++) { + if (grid[i][j] == 1) { + max = Math.max(max, dfs(grid, i, j, visit, 0)); + } + } + } + return max; + } + + public static int dfs(int [][]grid, int x, int y, int [][]visit, int count) { + if (x < 0 || x> grid.length - 1 || y < 0 || y> grid[0].length - 1) { + return count; + } + if (visit[x][y] == 1 || grid[x][y] == 0) { + return count; + } + + visit[x][y] = 1; + count ++; + + count += dfs(grid, x + 1, y, visit, 0); + count += dfs(grid, x - 1, y, visit, 0); + count += dfs(grid, x, y + 1, visit, 0); + count += dfs(grid, x, y - 1, visit, 0); + return count; + } +} diff --git "a/src/DFS/346円234円213円345円217円213円345円234円210円.java" "b/src/DFS/346円234円213円345円217円213円345円234円210円.java" new file mode 100644 index 0000000..3c52c56 --- /dev/null +++ "b/src/DFS/346円234円213円345円217円213円345円234円210円.java" @@ -0,0 +1,55 @@ +package DFS; + +/** + * Created by 周杰伦 on 2018年7月19日. + */ +public class 朋友圈 { + public static void main(String[] args) { + int [][]a = {{1,1,0}, {1,1,0}, {0,0,1}}; + System.out.println(findCircleNum(a)); + } + private static int n; + + public static int findCircleNum(int[][] M) { + n = M.length; + int circleNum = 0; + boolean[] hasVisited = new boolean[n]; + for (int i = 0; i < n; i ++) { + if (!hasVisited[i]) { + dfs(M, i, hasVisited); + circleNum++; + } + } + return circleNum; + } + + private static void dfs(int[][] M, int i, boolean[] hasVisited) { + hasVisited[i] = true; + for (int k = 0; k < n; k++) { + if (M[i][k] == 1 && !hasVisited[k]) { + dfs(M, k, hasVisited); + } + } + } +// private static int n; +// public static int findCircleNum(int[][] M) { +// n = M.length; +// int cnt = 0 ; +// int []visit = new int[n]; +// for (int i = 0;i < M.length;i ++) { +// if(visit[i] == 0) { +// dfs(M, visit, i); +// cnt ++; +// } +// } +// return cnt; +// } +// public static void dfs(int[][]M, int[] visit, int i) { +// visit[i] = 1; +// for (int j = 0;j < M.length;j ++) { +// if(visit[j] == 0 && M[i][j] == 1) { +// dfs(M, visit, i); +// } +// } +// } +} diff --git "a/src/DFS/346円237円245円346円211円276円346円234円200円345円244円247円347円232円204円350円277円236円351円200円232円351円235円242円347円247円257円.java" "b/src/DFS/346円237円245円346円211円276円346円234円200円345円244円247円347円232円204円350円277円236円351円200円232円351円235円242円347円247円257円.java" index 9f07765..c1646ee 100644 --- "a/src/DFS/346円237円245円346円211円276円346円234円200円345円244円247円347円232円204円350円277円236円351円200円232円351円235円242円347円247円257円.java" +++ "b/src/DFS/346円237円245円346円211円276円346円234円200円345円244円247円347円232円204円350円277円236円351円200円232円351円235円242円347円247円257円.java" @@ -1,6 +1,7 @@ package DFS; import java.util.Arrays; +import java.util.Stack; /** * Created by 周杰伦 on 2018/3/30. @@ -45,4 +46,34 @@ public static void dfs(int [][]grid,int [][]visit, int x, int y) { dfs(grid,visit,x ,y - 1); return; } + + //非递归写法 +// class Pos{ +// int x; +// int y; +// int count; +// +// public Pos(int x, int y, int count) { +// this.x = x; +// this.y = y; +// this.count = count; +// } +// } +// public int stack(int [][]grid,int [][]visit, int x, int y) { +// Stack stack = new Stack(); +// stack.push(new Pos(x, y, 0)); +// int [][]pos = {{0,1}, {0, -1}, {1, 0}, {-1, 0}}; +// while (!stack.isEmpty()) { +// Pos init = stack.peek(); +// for (int i = 0; i < pos.length; i++) { +// int x0 = x + pos[i][0]; +// int y0 = y + pos[i][1]; +// if (visit[x0][y0] == 0 && grid[x0][y0] == 1) { +// stack.push(new Pos(x0, y0, init.count + 1)); +// break; +// } +// } +// stack.pop(); +// } +// } } diff --git "a/src/DFS/350円242円253円345円233円264円347円273円225円347円232円204円345円214円272円345円237円237円.java" "b/src/DFS/350円242円253円345円233円264円347円273円225円347円232円204円345円214円272円345円237円237円.java" new file mode 100644 index 0000000..1f62a62 --- /dev/null +++ "b/src/DFS/350円242円253円345円233円264円347円273円225円347円232円204円345円214円272円345円237円237円.java" @@ -0,0 +1,62 @@ +package DFS; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by 周杰伦 on 2018/7/19. + */ +public class 被围绕的区域 { + private int m, n; + private int[][] matrix; + private int[][] direction = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}}; + + public List pacificAtlantic(int[][] matrix) { + List ret = new ArrayList(); + if (matrix == null || matrix.length == 0) { + return ret; + } + + m = matrix.length; + n = matrix[0].length; + this.matrix = matrix; + boolean[][] canReachP = new boolean[m][n]; + boolean[][] canReachA = new boolean[m][n]; + + for (int i = 0; i < m; i++) { + dfs(i, 0, canReachP); + dfs(i, n - 1, canReachA); + } + for (int i = 0; i < n; i++) { + dfs(0, i, canReachP); + dfs(m - 1, i, canReachA); + } + + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + if (canReachP[i][j] && canReachA[i][j]) { + ret.add(new int[]{i, j}); + } + } + } + + return ret; + } + + private void dfs(int r, int c, boolean[][] canReach) { + if (canReach[r][c]) { + return; + } + canReach[r][c] = true; + for (int[] d : direction) { + int nextR = d[0] + r; + int nextC = d[1] + c; + if (nextR < 0 || nextR>= m || nextC < 0 || nextC>= n + || matrix[r][c]> matrix[nextR][nextC]) { + + continue; + } + dfs(nextR, nextC, canReach); + } + } +} diff --git "a/src/346円216円222円345円272円217円/345円207円272円347円216円260円351円242円221円347円216円207円346円234円200円351円253円230円347円232円204円k344円270円252円346円225円260円.java" "b/src/346円216円222円345円272円217円/345円207円272円347円216円260円351円242円221円347円216円207円346円234円200円351円253円230円347円232円204円k344円270円252円346円225円260円.java" new file mode 100644 index 0000000..95cdd8a --- /dev/null +++ "b/src/346円216円222円345円272円217円/345円207円272円347円216円260円351円242円221円347円216円207円346円234円200円351円253円230円347円232円204円k344円270円252円346円225円260円.java" @@ -0,0 +1,40 @@ +package 排序; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Created by 周杰伦 on 2018年7月7日. + */ +public class 出现频率最高的k个数 { + public List topKFrequent(int[] nums, int k) { + Map map = new HashMap(); + for (int i : nums) { + if (map.containsKey(i)) { + map.put(i, map.get(i) + 1); + }else { + map.put(i, 1); + } + } + ArrayList[] timesMap = new ArrayList[nums.length + 1]; + for (int key : map.keySet()) { + int times = map.get(key); + if (timesMap[times] == null) { + timesMap[times] = new ArrayList(); + timesMap[times].add(key); + } + else { + timesMap[times].add(key); + } + } + List top = new ArrayList(); + for (int i = timesMap.length - 1;i> 0 && top.size() < k;i ++) { + if (timesMap[i] != null) { + top.addAll(timesMap[i]); + } + } + return top; + } +} diff --git "a/src/346円216円222円345円272円217円/346円240円271円346円215円256円345円255円227円347円254円246円345円207円272円347円216円260円351円242円221円347円216円207円346円216円222円345円272円217円.java" "b/src/346円216円222円345円272円217円/346円240円271円346円215円256円345円255円227円347円254円246円345円207円272円347円216円260円351円242円221円347円216円207円346円216円222円345円272円217円.java" new file mode 100644 index 0000000..0c1ad15 --- /dev/null +++ "b/src/346円216円222円345円272円217円/346円240円271円346円215円256円345円255円227円347円254円246円345円207円272円347円216円260円351円242円221円347円216円207円346円216円222円345円272円217円.java" @@ -0,0 +1,43 @@ +package 排序; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by 周杰伦 on 2018/7/7. + */ +public class 根据字符出现频率排序 { + public static void main(String[] args) { + System.out.println(frequencySort("tree")); + } + public static String frequencySort(String s) { + int []arr = new int[128]; + char []crr = s.toCharArray(); + for (char c : crr) { + arr[c]++; + } + + List[]times = new ArrayList[s.length() + 1]; + for (int i = 0;i < arr.length;i ++) { + if (times[arr[i]] == null) { + times[arr[i]] = new ArrayList(); + times[arr[i]].add((char) (i)); + }else { + times[arr[i]].add((char) (i)); + } + } + StringBuilder sb = new StringBuilder(); + for (int i = times.length - 1;i> 0 ;i --) { + if (times[i] != null) { + for (char c : times[i]) { + int time = 0; + while (time < i) { + sb.append(c); + time ++; + } + } + } + } + return sb.toString(); + } +} diff --git "a/src/346円216円222円345円272円217円/350円215円267円345円205円260円345円233円275円346円227円227円.java" "b/src/346円216円222円345円272円217円/350円215円267円345円205円260円345円233円275円346円227円227円.java" new file mode 100644 index 0000000..b53bde5 --- /dev/null +++ "b/src/346円216円222円345円272円217円/350円215円267円345円205円260円345円233円275円346円227円227円.java" @@ -0,0 +1,25 @@ +package 排序; + +/** + * Created by 周杰伦 on 2018/7/7. + */ +public class 荷兰国旗 { + public void sortColors(int[] nums) { + if (nums.length <= 1)return; + int zero = -1, one = 0,two = nums.length; + while (one < two) { + if (nums[one] == 0) { + swap(nums, ++zero, one++); + }else if (nums[one] == 2) { + swap(nums, --two, one); + }else { + one ++; + } + } + } + public void swap(int []nums, int i, int j) { + int tmp = nums[i]; + nums[i] = nums[j]; + nums[j] = tmp; + } +}

AltStyle によって変換されたページ (->オリジナル) /