Skip to content

Commit be4b4d5

Browse files
committed
20181113
1 parent 4c9c108 commit be4b4d5

30 files changed

+720
-594
lines changed

src/main/java/leetcode/array/_41_FirstMissingPositive.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,23 @@
2121
*/
2222
public class _41_FirstMissingPositive {
2323

24-
public int firstMissingPositive(int[] A) {
25-
if(A.length==0){
24+
public int firstMissingPositive(int[] nums) {
25+
if(nums.length==0){
2626
return 1;
2727
}
28-
int n=A.length,i=0;
28+
int n=nums.length,i=0;
2929
while(i<n){
30-
if(A[i]!=i+1&&(A[i]>0&&A[i]<=n)&&A[i]!=A[A[i]-1]){
31-
int temp=A[A[i]-1];
32-
A[A[i]-1]=A[i];
33-
A[i]=temp;
30+
if(nums[i]!=i+1&&(nums[i]>0&&nums[i]<=n)&&nums[i]!=nums[nums[i]-1]){
31+
int temp=nums[nums[i]-1];
32+
nums[nums[i]-1]=nums[i];
33+
nums[i]=temp;
3434
}else{
3535
i++;
3636
}
3737

3838
}
3939
for(i=0;i<n;i++){
40-
if(A[i]!=i+1){
40+
if(nums[i]!=i+1){
4141
return i+1;
4242
}
4343
}

src/main/java/leetcode/array/_4_findMedianSortedArrays.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,33 +22,33 @@
2222
*/
2323
public class _4_findMedianSortedArrays {
2424

25-
public double findMedianSortedArrays(int A[], int B[]) {
25+
public double findMedianSortedArrays(int nums1[], int nums2[]) {
2626
double mid = 0.0;
27-
int m = A.length;
28-
int n = B.length;
29-
if (A == null || B == null || m + n == 0) {
27+
int m = nums1.length;
28+
int n = nums2.length;
29+
if (nums1 == null || nums2 == null || m + n == 0) {
3030
return mid;
3131
}
3232
int[] C = new int[m + n];
3333
int i = 0, j = 0, k = 0;
3434
while (i < m && j < n) {
35-
if (A[i] < B[j]) {
36-
C[k] = A[i];
35+
if (nums1[i] < nums2[j]) {
36+
C[k] = nums1[i];
3737
k++;
3838
i++;
3939
} else {
40-
C[k] = B[j];
40+
C[k] = nums2[j];
4141
k++;
4242
j++;
4343
}
4444
}
4545
while (j < n) {
46-
C[k] = B[j];
46+
C[k] = nums2[j];
4747
k++;
4848
j++;
4949
}
5050
while (i < m) {
51-
C[k] = A[i];
51+
C[k] = nums1[i];
5252
k++;
5353
i++;
5454
}
Lines changed: 54 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,66 @@
11
package leetcode.dfsbfsbacktracing;
22

3+
import java.util.ArrayList;
4+
import java.util.List;
5+
36
/**
47
* 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
5-
6-
按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
7-
8-
"123"
9-
"132"
10-
"213"
11-
"231"
12-
"312"
13-
"321"
14-
给定 n 和 k,返回第 k 个排列。
15-
16-
说明:
17-
18-
给定 n 的范围是 [1, 9]。
19-
给定 k 的范围是[1, n!]。
20-
示例 1:
21-
22-
输入: n = 3, k = 3
23-
输出: "213"
24-
示例 2:
25-
26-
输入: n = 4, k = 9
27-
输出: "2314"
8+
* <p>
9+
* 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
10+
* <p>
11+
* "123"
12+
* "132"
13+
* "213"
14+
* "231"
15+
* "312"
16+
* "321"
17+
* 给定 n 和 k,返回第 k 个排列。
18+
* <p>
19+
* 说明:
20+
* <p>
21+
* 给定 n 的范围是 [1, 9]。
22+
* 给定 k 的范围是[1, n!]。
23+
* 示例 1:
24+
* <p>
25+
* 输入: n = 3, k = 3
26+
* 输出: "213"
27+
* 示例 2:
28+
* <p>
29+
* 输入: n = 4, k = 9
30+
* 输出: "2314"
2831
*/
2932
public class _60_PermutationSequence {
30-
//
33+
/**
34+
* n个数的permutation总共有n阶乘个,基于这个性质我们可以得到某一位对应的数字是哪一个。
35+
* 思路是这样的,比如当前长度是n,我们知道每个相同的起始元素对应(n-1)!个permutation,
36+
* 也就是(n-1)!个permutation后会换一个起始元素。因此,只要当前的k进行(n-1)!取余,
37+
* 得到的数字就是当前剩余数组的index,如此就可以得到对应的元素。如此递推直到数组中没有元素结束。
38+
* 实现中我们要维护一个数组来记录当前的元素,每次得到一个元素加入结果数组,然后从剩余数组中移除,
39+
* 因此空间复杂度是O(n)。时间上总共需要n个回合,而每次删除元素如果是用数组需要O(n),所以总共是O(n^2)。
40+
*
41+
* @param n
42+
* @param k
43+
* @return
44+
*/
3145
public String getPermutation(int n, int k) {
32-
int[] perm = new int[n];
33-
int total = 1;
34-
for(int i=1;i<=n;i++){
35-
perm[i-1] = i;//这里边界出错了
36-
total *= i;
46+
k--; // to translate index from 0 rather than 1
47+
List<Integer> list = new ArrayList<Integer>();
48+
for (int i = 1; i <= n; i++) {
49+
list.add(i);
3750
}
38-
k %= total;
39-
if(k==0) k = total;
40-
int count = 0;
41-
while(true){
42-
count++;
43-
if(count==k) break;
44-
int i;
45-
for(i=n-1;i>0;i--){
46-
if(perm[i]>perm[i-1]){
47-
int temp = perm[i-1];
48-
int j;
49-
for(j=n-1;j>i&&perm[j]<temp;j--);
50-
perm[i-1] = perm[j];
51-
perm[j] = temp;
52-
break;
53-
}
54-
}
55-
for(int m=i,j=n-1;m<j;m++,j--){
56-
int temp = perm[j];
57-
perm[j] = perm[m];
58-
perm[m] = temp;
59-
}
51+
int factorial = 1;
52+
for (int i = 2; i < n; i++) {
53+
factorial *= i;
54+
}
55+
int round = n - 1;
56+
StringBuilder sb = new StringBuilder();
57+
while (round >= 0) {
58+
sb.append(list.remove(k / factorial));//new list
59+
k %= factorial;//new k
60+
if (round != 0) factorial /= round;//new factorial
61+
round--;// new round
6062
}
61-
StringBuffer sb = new StringBuffer();
62-
for(int num:perm) sb.append(num);
6363
return sb.toString();
6464
}
65+
6566
}

src/main/java/leetcode/dpandgreedy/_62_UniquePaths.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
/**
44
* Created by fay on 2017/12/14.
55
* 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).
6+
* The robot can only move either down or right at any point in time.
7+
* The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
78
* How many possible unique paths are there?
89
* 一个机器人位于一个 m x n 网格的左上角 (起始点在下图中标记为“Start” )。
910
*

src/main/java/leetcode/dpandgreedy/_63_UniquePaths2.java

Lines changed: 48 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,77 @@
22

33
/**
44
* 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. 向下 -> 向下 -> 向右 -> 向右
5+
* <p>
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. 向下 -> 向下 -> 向右 -> 向右
2424
*/
2525
public class _63_UniquePaths2 {
2626

27+
//改进的方法,使用一维数组
2728
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){
29+
int n = obstacleGrid[0].length;
30+
int m = obstacleGrid.length;
31+
int[] dp = new int[n];
32+
dp[0] = 1;
33+
if (obstacleGrid[0][0] == 1) {
3334
return 0;
3435
}
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];
36+
for (int i = 0; i < m; i++) {
37+
for (int j = 0; j < n; j++) {
38+
if (obstacleGrid[i][j] == 1) {
39+
dp[j] = 0;
40+
} else if (j > 0) {
41+
dp[j] += dp[j - 1];
4142
}
4243
}
4344
}
44-
return dp[n-1];
45+
return dp[n - 1];
4546
}
4647

4748
public int uniquePathsWithObstacles1(int[][] obstacleGrid) {
48-
if(obstacleGrid==null || obstacleGrid.length==0)
49+
if (obstacleGrid == null || obstacleGrid.length == 0)
4950
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++) //第一行障碍处理
51+
int m = obstacleGrid.length;
52+
int n = obstacleGrid[0].length;
53+
int[][] dp = new int[m][n]; //dp[i][j]表示从start到[i,j]位置不同路径条
54+
for (int i = 0; i < n; i++) //第一行障碍处理
5855
{
59-
if(obstacleGrid[0][i]!=1)
60-
dp[0][i]=1;
56+
if (obstacleGrid[0][i] != 1)
57+
dp[0][i] = 1;
6158
else
6259
break;
6360
}
6461

65-
for(int j=0;j<m;j++) //第一列障碍处理
62+
for (int j = 0; j < m; j++) //第一列障碍处理
6663
{
67-
if(obstacleGrid[j][0]!=1)
68-
dp[j][0]=1;
64+
if (obstacleGrid[j][0] != 1)
65+
dp[j][0] = 1;
6966
else
7067
break;
7168
}
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;
69+
for (int i = 1; i < m; i++)
70+
for (int j = 1; j < n; j++) {
71+
if (obstacleGrid[i][j] == 1) //如果该位置是障碍,则到达该点的路径条数为0
72+
dp[i][j] = 0;
7773
else
78-
dp[i][j]=dp[i-1][j]+dp[i][j-1];
74+
dp[i][j] = dp[i - 1][j] + dp[i][j - 1];
7975
}
80-
return dp[m-1][n-1];
76+
return dp[m - 1][n - 1];
8177
}
8278
}

src/main/java/leetcode/dpandgreedy/_70_ClimbingStairs.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,16 @@ public int climbStairs(int n) {
1212
if(n<2){
1313
return 1;
1414
}
15-
int[] dp=new int[n+1];
15+
int[] dp=new int[n];
1616
dp[0]=1;
17-
dp[1]=1;
18-
for(int i=2;i<=n;i++){
17+
dp[1]=2;
18+
for(int i=2;i<n;i++){
1919
dp[i]=dp[i-1]+dp[i-2];
2020
}
21-
return dp[n];
21+
return dp[n-1];
2222
}
23+
24+
2325
public static void main(String[] args){
2426
_70_ClimbingStairs test=new _70_ClimbingStairs();
2527
System.out.print(test.climbStairs(5));

src/main/java/leetcode/dpandgreedy/_77_EditDistance.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
/**
44
* Created by fay on 2017/12/15.
5-
* Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.)
5+
* Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2.
6+
* (each operation is counted as 1 step.)
67
You have the following 3 operations permitted on a word:
78
a) insert a character
89
b) Delete a character

0 commit comments

Comments
 (0)