Skip to content

Commit 555e921

Browse files
committed
20181027
2 parents 406fa4a + 8d9fd23 commit 555e921

33 files changed

+435
-243
lines changed

src/main/java/leetcode/array/_153_FindMinimumInRotatedSortedArry.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,18 @@ public int findMin(int[] nums) {
2424
if(nums==null||nums.length==0) return -1;
2525
int mid=0;
2626
int left=0,right=nums.length-1;
27+
//控制nums[left]>=nums[right].使第一个指针永远指向第一个递增子区间,第二个指针永远指向第二个递增子区间
2728
while(nums[left]>=nums[right]&&left<right){
29+
//第一个指针已经到达第一个子区间的末尾
2830
if(right-left==1){
2931
mid=right;
3032
break;
3133
}
3234
mid=(left+right)>>1;
3335
if(nums[mid]>nums[left]){
34-
left=mid;
36+
left=mid+1;
3537
}else{
36-
right=mid;
38+
right=mid-1;
3739
}
3840
}
3941

src/main/java/leetcode/array/_15_Sum3.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ public static void main(String[] args) {
6161
int[] num = {-1, 0, 1, 2, -1, -4};
6262
int[] num1 = {0, 0, 0, 0};
6363
int[] num2 = {-2, 0, 1, 1, 2};
64-
List<List<Integer>> result = test.threeSum(num);
6564
/*for(ArrayList<Integer> res:result){
6665
for(int a:res){
6766
System.out.print(a);

src/main/java/leetcode/dfsbfsbacktracing/_126_WordLadder.java renamed to src/main/java/leetcode/dfsbfsbacktracing/_127_WordLadder.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.util.List;
66
import java.util.Set;
77

8+
<<<<<<< HEAD:src/main/java/leetcode/dfsbfsbacktracing/_126_WordLadder.java
89
public class _126_WordLadder {
910
/**
1011
* bfs层次遍历的思想
@@ -14,6 +15,44 @@ public class _126_WordLadder {
1415
* @param dict
1516
* @return
1617
*/
18+
=======
19+
/**
20+
*给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:
21+
*
22+
* 每次转换只能改变一个字母。
23+
* 转换过程中的中间单词必须是字典中的单词。
24+
* 说明:
25+
*
26+
* 如果不存在这样的转换序列,返回 0。
27+
* 所有单词具有相同的长度。
28+
* 所有单词只由小写字母组成。
29+
* 字典中不存在重复的单词。
30+
* 你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
31+
* 示例 1:
32+
*
33+
* 输入:
34+
* beginWord = "hit",
35+
* endWord = "cog",
36+
* wordList = ["hot","dot","dog","lot","log","cog"]
37+
*
38+
* 输出: 5
39+
*
40+
* 解释: 一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog",
41+
* 返回它的长度 5。
42+
* 示例 2:
43+
*
44+
* 输入:
45+
* beginWord = "hit"
46+
* endWord = "cog"
47+
* wordList = ["hot","dot","dog","lot","log"]
48+
*
49+
* 输出: 0
50+
*
51+
* 解释: endWord "cog" 不在字典中,所以无法进行转换。
52+
*/
53+
public class _127_WordLadder {
54+
55+
>>>>>>> 8d9fd232620a92e2f658ded073d959eb094bcaa1:src/main/java/leetcode/dfsbfsbacktracing/_127_WordLadder.java
1756
public int ladderLength(String start, String end, HashSet<String> dict) {
1857
if (start == null || end == null || start.length() == 0 || end.length() == 0 || start.length() != end.length())
1958
return 0;

src/main/java/leetcode/search/WordSearch.java renamed to src/main/java/leetcode/dfsbfsbacktracing/_79_WordSearch.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
<<<<<<< HEAD:src/main/java/leetcode/search/WordSearch.java
12
package leetcode.search;
3+
=======
4+
package leetcode.dfsbfsbacktracing;
5+
>>>>>>> 8d9fd232620a92e2f658ded073d959eb094bcaa1:src/main/java/leetcode/dfsbfsbacktracing/_79_WordSearch.java
26

37
/**
48
* Given a 2D board and a word, find if the word exists in the grid.
@@ -17,8 +21,29 @@
1721
word ="ABCCED", -> returnstrue,
1822
word ="SEE", -> returnstrue,
1923
word ="ABCB", -> returnsfalse.
24+
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
25+
26+
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
27+
28+
示例:
29+
30+
board =
31+
[
32+
['A','B','C','E'],
33+
['S','F','C','S'],
34+
['A','D','E','E']
35+
]
36+
37+
给定 word = "ABCCED", 返回 true.
38+
给定 word = "SEE", 返回 true.
39+
给定 word = "ABCB", 返回 false.
40+
2041
*/
42+
<<<<<<< HEAD:src/main/java/leetcode/search/WordSearch.java
2143
public class WordSearch {
44+
=======
45+
public class _79_WordSearch {
46+
>>>>>>> 8d9fd232620a92e2f658ded073d959eb094bcaa1:src/main/java/leetcode/dfsbfsbacktracing/_79_WordSearch.java
2247

2348
public boolean exist(char[][] board, String word) {
2449
if(board==null||board.length==0||board[0].length==0){

src/main/java/leetcode/dynamic/GrayCode.java renamed to src/main/java/leetcode/dpandgreedy/GrayCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package leetcode.dynamic;
1+
package leetcode.dpandgreedy;
22

33
import java.util.ArrayList;
44

src/main/java/leetcode/dynamic/SrambleString.java renamed to src/main/java/leetcode/dpandgreedy/SrambleString.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main.java.leetcode.dynamic;
1+
package leetcode.dpandgreedy;
22

33
/**
44
* Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrings recursively.

src/main/java/leetcode/dynamic/WildcardMatching.java renamed to src/main/java/leetcode/dpandgreedy/WildcardMatching.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main.java.leetcode.dynamic;
1+
package leetcode.dpandgreedy;
22

33
/**
44
*
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package leetcode.dpandgreedy;
2+
3+
/**
4+
* Created by fay on 2017/12/14.
5+
* A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
6+
* The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
7+
* How many possible unique paths are there?
8+
* 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
9+
*
10+
* 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角(在下图中标记为“Finish”)。
11+
*
12+
* 问总共有多少条不同的路径?
13+
* 说明:m 和 n 的值均不超过 100。
14+
*
15+
* 示例 1:
16+
*
17+
* 输入: m = 3, n = 2
18+
* 输出: 3
19+
* 解释:
20+
* 从左上角开始,总共有 3 条路径可以到达右下角。
21+
* 1. 向右 -> 向右 -> 向下
22+
* 2. 向右 -> 向下 -> 向右
23+
* 3. 向下 -> 向右 -> 向右
24+
*
25+
*/
26+
public class _62_UniquePaths {
27+
28+
public int uniquePaths(int m, int n) {
29+
if (m == 0 || n == 0) {
30+
return 0;
31+
}
32+
int[][] dp = new int[m][n];
33+
for (int i = 0; i < m; i++) {
34+
dp[i][0] = 1;
35+
}
36+
for (int j = 0; j < n; j++) {
37+
dp[0][j] = 1;
38+
}
39+
for (int i = 1; i < m; i++) {
40+
for (int j = 1; j < n; j++) {
41+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
42+
}
43+
}
44+
return dp[m - 1][n - 1];
45+
}
46+
47+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package leetcode.dpandgreedy;
2+
3+
/**
4+
* Created by fay on 2017/12/14.
5+
*
6+
Follow up for "Unique Paths":
7+
Now consider if some obstacles are added to the grids. How many unique paths would there be?
8+
An obstacle and empty space is marked as1and0respectively in the grid.
9+
For example,
10+
There is one obstacle in the middle of a 3x3 grid as illustrated below.
11+
[
12+
[0,0,0],
13+
[0,1,0],
14+
[0,0,0]
15+
]
16+
现在考虑网格中有障碍物。那么从左上角到右下角将会有多少条不同的路径?
17+
网格中的障碍物和空位置分别用 1 和 0 来表示。
18+
The total number of unique paths is 2.
19+
Note: m and n will be at most 100.
20+
3x3 网格的正中间有一个障碍物。
21+
从左上角到右下角一共有 2 条不同的路径:
22+
1. 向右 -> 向右 -> 向下 -> 向下
23+
2. 向下 -> 向下 -> 向右 -> 向右
24+
*/
25+
public class _63_UniquePaths2 {
26+
27+
public int uniquePathsWithObstacles(int[][] obstacleGrid) {
28+
int n=obstacleGrid[0].length;
29+
int m=obstacleGrid.length;
30+
int[] dp=new int[n];
31+
dp[0]=1;
32+
if(obstacleGrid[0][0]==1){
33+
return 0;
34+
}
35+
for(int i=0;i<m;i++){
36+
for(int j=0;j<n;j++){
37+
if(obstacleGrid[i][j]==1){
38+
dp[j]=0;
39+
}else if(j>0){
40+
dp[j]+=dp[j-1];
41+
}
42+
}
43+
}
44+
return dp[n-1];
45+
}
46+
47+
public int uniquePathsWithObstacles1(int[][] obstacleGrid) {
48+
if(obstacleGrid==null || obstacleGrid.length==0)
49+
return 0;
50+
int m=obstacleGrid.length;
51+
int n=obstacleGrid[0].length;
52+
int[][] dp=new int[m][n]; //dp[i][j]表示从start到[i,j]位置不同路径条
53+
// 不需要初始化,默认初始化。
54+
//for(int i=0;i<m;i++)
55+
// for(int j=0;j<n;j++)
56+
//dp[i][j]=0;
57+
for(int i=0;i<n;i++) //第一行障碍处理
58+
{
59+
if(obstacleGrid[0][i]!=1)
60+
dp[0][i]=1;
61+
else
62+
break;
63+
}
64+
65+
for(int j=0;j<m;j++) //第一列障碍处理
66+
{
67+
if(obstacleGrid[j][0]!=1)
68+
dp[j][0]=1;
69+
else
70+
break;
71+
}
72+
for(int i=1;i<m;i++)
73+
for(int j=1;j<n;j++)
74+
{
75+
if(obstacleGrid[i][j]==1) //如果该位置是障碍,则到达该点的路径条数为0
76+
dp[i][j]=0;
77+
else
78+
dp[i][j]=dp[i-1][j]+dp[i][j-1];
79+
}
80+
return dp[m-1][n-1];
81+
}
82+
}

src/main/java/leetcode/dynamic/MinmumPathSum.java renamed to src/main/java/leetcode/dpandgreedy/_64_MinmumPathSum.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,26 @@
1-
package leetcode.dynamic;
1+
package leetcode.dpandgreedy;
22

33
/**
4-
* Created by fay on 2017/12/14.
5-
* Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.
6-
Note: You can only move either down or right at any point in time.
4+
给定一个包含非负整数的 m x n 网格,请找出一条从左上角到右下角的路径,使得路径上的数字总和为最小。
5+
6+
说明:每次只能向下或者向右移动一步。
7+
8+
示例:
9+
10+
输入:
11+
[
12+
[1,3,1],
13+
[1,5,1],
14+
[4,2,1]
15+
]
16+
输出: 7
17+
解释: 因为路径 1→3→1→1→1 的总和最小。
718
*/
19+
<<<<<<< HEAD:src/main/java/leetcode/dynamic/MinmumPathSum.java
820
public class MinmumPathSum {
21+
=======
22+
public class _64_MinmumPathSum {
23+
>>>>>>> 8d9fd232620a92e2f658ded073d959eb094bcaa1:src/main/java/leetcode/dpandgreedy/_64_MinmumPathSum.java
924

1025
public int minPathSum(int[][] grid) {
1126
if(grid==null||grid.length==0||grid[0]==null||grid[0].length==0){

0 commit comments

Comments
 (0)