Skip to content

Commit c1dca5b

Browse files
authored
Merge pull request gzc426#57 from gzc426/master
1
2 parents 8114d39 + c5407ce commit c1dca5b

File tree

111 files changed

+2846
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

111 files changed

+2846
-0
lines changed

2018.11.19-leetcode15/棕榈树.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```
2+
class Solution {
3+
public List<List<Integer>> threeSum(int[] nums) {
4+
List<List<Integer>> res = new ArrayList<>();
5+
Arrays.sort(nums);
6+
for(int i=0;i<nums.length-2;i++){
7+
int low = i+1;
8+
int high = nums.length-1;
9+
int sum = 0 - nums[i];
10+
if(i==0||(i>0&&nums[i]!=nums[i-1])){
11+
while(low<high){
12+
if(sum==nums[low]+nums[high]){
13+
res.add(Arrays.asList(nums[i],nums[low],nums[high]));//将结果添加到数组中
14+
while(low<high&&nums[low]==nums[low+1]) low++;
15+
while(low<high&&nums[high-1]==nums[high]) high--;
16+
low++;
17+
high--;
18+
}
19+
else if(nums[low]+nums[high]<sum){
20+
low++;
21+
}else{
22+
high--;
23+
}
24+
}
25+
}
26+
}
27+
return res;
28+
}
29+
}
30+
```

2019.01.05-leetcode127/sourcema.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# LeetCode 127
2+
class Solution {
3+
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
4+
Set<String> set = new HashSet<>(wordList);
5+
if(!set.contains(endWord)){
6+
return 0;
7+
}
8+
Queue<String> queue=new LinkedList<String>();
9+
queue.add(beginWord);
10+
int step=1;
11+
while(!queue.isEmpty()){
12+
int size=queue.size();
13+
for (int i = 0; i < size; i++) {
14+
String s = queue.poll();
15+
for (int k = 0; k < s.length(); k++) {
16+
char[] chars = s.toCharArray();
17+
for (char j = 'a'; j <= 'z'; j++) {
18+
chars[k] = j;
19+
String temp = String.valueOf(chars);
20+
if (set.contains(temp)) {
21+
if (temp.equals(endWord)) {
22+
return step + 1;
23+
}
24+
queue.add(temp);
25+
set.remove(temp);
26+
}
27+
}
28+
}
29+
}
30+
step++;
31+
}
32+
return 0;
33+
34+
}
35+
}

2019.01.06-leetcode695/sourcema.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# LeetCode 695
2+
class Solution {
3+
public int maxAreaOfIsland(int[][] grid) {
4+
if (grid[0].length == 0 || grid.length == 0) {
5+
return 0;
6+
}
7+
int row=grid.length,col=grid[0].length;
8+
int max=0;
9+
for (int i = 0; i < row; i++) {
10+
for (int j = 0; j < col; j++) {
11+
if (grid[i][j] == 1) {
12+
int count=dfs(grid, i, j);
13+
max = Math.max(max, count);
14+
}
15+
}
16+
}
17+
return max;
18+
}
19+
private int dfs(int[][] grid, int i, int j) {
20+
if (i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || grid[i][j] == 0) {
21+
return 0;
22+
}
23+
grid[i][j]=0;
24+
return 1 + dfs(grid, i - 1, j) + dfs(grid, i + 1, j)
25+
+ dfs(grid, i, j - 1) + dfs(grid, i, j + 1);
26+
}
27+
}

2019.01.07-leetcode200/sourcema.md

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# leetcode 200
2+
class Solution {
3+
static int count=0;
4+
static int[] parent;
5+
public static int numIslands(char[][] arr) {//并查集解法
6+
if (arr.length == 0||arr[0].length == 0 ) {
7+
return 0;
8+
}
9+
int length=arr.length * arr[0].length;
10+
int width=arr[0].length;
11+
parent = new int[length];
12+
for (int i = 0; i < length; i++) {
13+
int x=i/width;
14+
int y=i%width;
15+
if (arr[x][y] - '0' == 1) {
16+
count++;
17+
}
18+
parent[i]=i;
19+
}
20+
for (int i = 0; i < length; i++) {//为什么考查右边和下方 1 0
21+
int x=i/width; //1 1如果不考察下方,右下角的1就无法抵消啦
22+
int y=i%width;
23+
int down=x+1;
24+
int right=y+1;
25+
if (right < width && arr[x][y] - '0' == 1 && arr[x][right] - '0' == 1) {
26+
union(i, i+1);
27+
}
28+
if (down < arr.length && arr[x][y] - '0' == 1 && arr[down][y] - '0' == 1) {
29+
union(i, i+width);
30+
}
31+
}
32+
return count;
33+
34+
}
35+
36+
public static void union(int p,int q) {
37+
int rootP = find(p);
38+
int rootQ = find(q);
39+
if (rootP == rootQ) {
40+
return;
41+
}
42+
parent[rootQ]=rootP;
43+
count--;
44+
}
45+
46+
public static int find(int e) {
47+
while (parent[e] != e) {
48+
parent[e] = parent[parent[e]];//让子节点直接连接父节点
49+
e = parent[e];
50+
}
51+
return e;
52+
}
53+
}

2019.01.08-leetcode547/sourcema.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# leetcode 547
2+
class Solution {
3+
static int[] parent;
4+
static int count;
5+
6+
public static int findCircleNum(int[][] friends) {
7+
if (friends == null || friends.length == 0 || friends[0].length == 0) {
8+
return 0;
9+
}
10+
parent = new int[friends.length];
11+
count = friends.length;
12+
for (int i = 0; i < friends.length; i++) {
13+
parent[i] = i;
14+
}
15+
for (int i = 0; i < friends.length; i++) {
16+
for (int j = i+1; j < friends.length; j++) {
17+
if (friends[i][j] == 1) {
18+
union(i, j);
19+
}
20+
}
21+
}
22+
return count;
23+
}
24+
25+
public static void union(int p, int q) {
26+
int rootP = find(p);
27+
int rootQ = find(q);
28+
if (rootP == rootQ) {
29+
return;
30+
}
31+
parent[rootQ] = rootP;
32+
count--;
33+
}
34+
35+
public static int find(int e) {
36+
while (parent[e] != e) {
37+
parent[e] = parent[parent[e]];//让子节点直接连接父节点
38+
e = parent[e];
39+
}
40+
return e;
41+
}
42+
}

2019.01.10-leetcode417/sourcema.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# leetcode 417
2+
class Solution {
3+
private static int[][] d = {{0,1}, {1,0}, {0,-1}, {-1,0}};
4+
private static int row;
5+
private static int col;
6+
public static List<int[]> pacificAtlantic(int[][] matrix) {
7+
List<int[]> res = new ArrayList<>();
8+
if (matrix == null || matrix.length == 0) {
9+
return res;
10+
}
11+
row=matrix.length;
12+
col=matrix[0].length;
13+
boolean[][] pacific = new boolean[row][col];
14+
boolean[][] atlantic = new boolean[row][col];
15+
for (int i = 0; i <row ; i++) {//从左右边界开始判断
16+
dfs(matrix, pacific, i, 0);
17+
dfs(matrix, atlantic, i, col-1);
18+
}
19+
for (int i = 0; i <col ; i++) {//从左右边界开始判断
20+
dfs(matrix, pacific, 0, i);
21+
dfs(matrix, atlantic, row-1, i);
22+
}
23+
for (int i = 0; i < row; i++) {
24+
for (int j = 0; j < col; j++) {
25+
if (pacific[i][j] && atlantic[i][j]) {
26+
res.add(new int[]{i, j});
27+
}
28+
}
29+
}
30+
return res;
31+
}
32+
33+
private static void dfs(int[][] matrix, boolean[][] arr, int x, int y) {
34+
arr[x][y]=true;
35+
for (int i = 0; i < 4; i++) {
36+
int nextX=x+d[i][0];
37+
int nextY=y+d[i][1];
38+
if (nextX >= 0 && nextX < row && nextY >= 0 && nextY < col && !arr[nextX][nextY]
39+
&& matrix[nextX][nextY] >= matrix[x][y]) {
40+
dfs(matrix,arr,nextX,nextY);
41+
}
42+
}
43+
}
44+
}

2019.01.11-leetcode17/sourcema.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# leetcode 17
2+
class Solution {
3+
4+
static String[] letters = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
5+
public static List<String> letterCombinations(String digits) {
6+
List<String> res = new ArrayList<>();
7+
if (digits == null || digits.length() == 0) {
8+
return res;
9+
}
10+
dfs(digits, res, 0, digits.length(), "");
11+
return res;
12+
}
13+
14+
private static void dfs(String digits, List<String> res, int index, int length, String ret) {
15+
if (index >= length) {
16+
res.add(ret);
17+
return;
18+
}
19+
int digit = digits.charAt(index) - '0';
20+
for (int i = 0; i <letters[digit].length(); i++) {
21+
ret += letters[digit].charAt(i);
22+
dfs(digits, res, index + 1, length, ret);
23+
ret=ret.substring(0, ret.length() - 1);
24+
}
25+
}
26+
}

2019.01.12-leetcode93/sourcema.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# leetcode 93
2+
class Solution {
3+
public static List<String> restoreIpAddresses(String s) {
4+
List<String> res = new ArrayList<>();
5+
if (s == null || s.length() == 0||s.length()<4||s.length()>12) {
6+
return res;
7+
}
8+
dfs(s, res, 0, "");
9+
return res;
10+
}
11+
12+
private static void dfs(String ip, List<String> res, int index, String ret) {
13+
if (index == 3) {
14+
if ((ip.length()==1||(ip.length()>1&&ip.length()<4&&ip.charAt(0)!='0'))
15+
&&Integer.valueOf(ip) <= 255) {//Integer.valueOf(ip)防止ip的位数超出int范围 首位不能以0开头
16+
res.add(ret+ip);
17+
}
18+
return;
19+
}
20+
for (int i = 1; i < 4; i++) {
21+
String split = ip.substring(0, i>ip.length()?ip.length():i);//可能出现ip的长度小于i的情况,不够长度去切分
22+
if ((split.length()==1||(split.length()>1&&split.charAt(0)!='0'))
23+
&&Integer.valueOf(split) <= 255) {
24+
ret = ret + split + ".";
25+
dfs(ip.substring(i>ip.length()?ip.length():i),res,index+1,ret);
26+
ret = ret.substring(0,ret.length() - split.length() - 1);
27+
}
28+
}
29+
}
30+
}

2019.01.13-leetcode79/sourcema.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# leetcode 79
2+
class Solution {
3+
public boolean exist(char[][] board, String word) {
4+
if (board[0].length == 0 || board.length == 0) {
5+
return false;
6+
}
7+
int row=board.length,col=board[0].length,count=0;
8+
boolean flag=false;
9+
go:
10+
for (int i = 0; i < row; i++) {
11+
for (int j = 0; j < col; j++) {
12+
flag=dfs(board,word,i,j,count);
13+
if (flag) {
14+
break go;
15+
}
16+
}
17+
}
18+
return flag;
19+
}
20+
private boolean dfs(char[][] board, String word,int i, int j, int count) {
21+
if (count == word.length()) {
22+
return true;
23+
}
24+
if (i < 0 || i >= board.length || j < 0 || j >= board[0].length || word.charAt(count) != board[i][j]) {
25+
return false;
26+
}
27+
board[i][j]^=256;
28+
boolean flag=dfs(board,word,i+1,j,count+1)||dfs(board,word,i-1,j,count+1)
29+
||dfs(board,word,i,j+1,count+1)||dfs(board,word,i,j-1,count+1);
30+
board[i][j]^=256;
31+
return flag;
32+
}
33+
}

2019.01.14-leetcode257/sourcema.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# leetcode 257
2+
class Solution {
3+
public List<String> binaryTreePaths(TreeNode root) {
4+
List<String> list = new ArrayList<>();
5+
if (root == null) {
6+
return list;
7+
}
8+
dfs(root, list,new String());
9+
return list;
10+
}
11+
private void dfs(TreeNode root, List<String> list,String ret) {
12+
if (root.left==null&&root.right==null) {
13+
ret+=root.val;
14+
list.add(ret);
15+
return;
16+
}
17+
ret += root.val + "->";
18+
if(root.left!=null){
19+
dfs(root.left,list,ret);
20+
}
21+
if(root.right!=null){
22+
dfs(root.right,list,ret);
23+
}
24+
}
25+
}

2019.01.14-leetcode257/棕榈树.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
```
2+
/**
3+
* Definition for a binary tree node.
4+
* public class TreeNode {
5+
* int val;
6+
* TreeNode left;
7+
* TreeNode right;
8+
* TreeNode(int x) { val = x; }
9+
* }
10+
*/
11+
class Solution {
12+
public List<String> binaryTreePaths(TreeNode root) {
13+
List<String> res = new ArrayList<>();
14+
if(null!=root){
15+
DFS(root,"",res);
16+
}
17+
return res;
18+
}
19+
20+
public void DFS(TreeNode node,String path,List<String> res){
21+
if(node.left==null&&node.right==null){
22+
res.add(path+node.val);
23+
}
24+
if(node.left!=null){
25+
DFS(node.left,path+node.val+"->",res);
26+
}
27+
if(node.right!=null){
28+
DFS(node.right,path+node.val+"->",res);
29+
}
30+
}
31+
}
32+
```

0 commit comments

Comments
 (0)