Skip to content

Commit 8d9fd23

Browse files
committed
20181027
1 parent 07fd25a commit 8d9fd23

Some content is hidden

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

44 files changed

+434
-219
lines changed

src/main/java/leetcode/array/_121_BestTimeToBuyAndSellStock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
(ie, buy one and sell one share of the stock),
77
design an algorithm to find the maximum profit.
88
*/
9-
public class BestTimeToBuyAndSellStock {
9+
public class _121_BestTimeToBuyAndSellStock {
1010

1111
public int maxProfit(int[] prices){
1212
if(prices==null||prices.length==0){

src/main/java/leetcode/array/_122_BestTimeToBuyAndSellStock2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ transactions as you like (ie, buy one and sell one share of the stock multiple
88
(ie, you must sell the stock before you buy again).
99
*/
1010

11-
public class BestTimeToBuyAndSellStock2 {
11+
public class _122_BestTimeToBuyAndSellStock2 {
1212
public int maxProfit(int[] prices) {
1313
if(prices==null||prices.length==0){
1414
return 0;

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤
1818
(-1, 0, 1)
1919
(-1, -1, 2)
2020
*/
21-
public class Sum3 {
21+
public class _15_Sum3 {
2222
public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
2323
ArrayList<ArrayList<Integer>> result=new ArrayList<>();
2424
Arrays.sort(num);
@@ -55,7 +55,7 @@ public ArrayList<ArrayList<Integer>> threeSum(int[] num) {
5555
return result;
5656
}
5757
public static void main(String[] args){
58-
Sum3 test=new Sum3();
58+
_15_Sum3 test=new _15_Sum3();
5959
int[] num={-1, 0 ,1 ,2 ,-1 ,-4};
6060
int[] num1={0,0,0,0};
6161
int[] num2={-2,0,1,1,2};

src/main/java/leetcode/array/_16_SumClosest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
1212
*/
13-
public class SumClosest {
13+
public class _16_SumClosest {
1414
public int threeSumClosest(int[] num, int target) {
1515
int closet=num[0]+num[1]+num[2];
1616
int diff=Math.abs(closet-target);

src/main/java/leetcode/array/_18_Sum4.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Elements in a quadruplet (a,b,c,d) must be in non-descending order. (ie,
2121
(-2, -1, 1, 2)
2222
(-2, 0, 0, 2)
2323
*/
24-
public class Sum4 {
24+
public class _18_Sum4 {
2525
public ArrayList<ArrayList<Integer>> fourSum(int[] num, int target) {
2626

2727
//Set<ArrayList<Integer>> result = new TreeSet<>();
@@ -157,7 +157,7 @@ public static void main(String[] args){
157157
int[] arr1={-3,-2,-1,0,0,1,2,3};
158158
int[] arr2={-1,-5,-5,-3,2,5,0,4};
159159
int[] arr3={-1,0,-5,-2,-2,-4,0,1,-2};
160-
Sum4 test=new Sum4();
160+
_18_Sum4 test=new _18_Sum4();
161161
// ArrayList<ArrayList<Integer>> result1=test.fourSum1(arr2,-7);
162162
//有重复
163163
ArrayList<ArrayList<Integer>> result1=test.fourSum1(arr3,-9);

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,41 @@
33
import java.util.HashSet;
44
import java.util.LinkedList;
55

6-
public class _126_WordLadder {
6+
/**
7+
*给定两个单词(beginWord 和 endWord)和一个字典,找到从 beginWord 到 endWord 的最短转换序列的长度。转换需遵循如下规则:
8+
*
9+
* 每次转换只能改变一个字母。
10+
* 转换过程中的中间单词必须是字典中的单词。
11+
* 说明:
12+
*
13+
* 如果不存在这样的转换序列,返回 0。
14+
* 所有单词具有相同的长度。
15+
* 所有单词只由小写字母组成。
16+
* 字典中不存在重复的单词。
17+
* 你可以假设 beginWord 和 endWord 是非空的,且二者不相同。
18+
* 示例 1:
19+
*
20+
* 输入:
21+
* beginWord = "hit",
22+
* endWord = "cog",
23+
* wordList = ["hot","dot","dog","lot","log","cog"]
24+
*
25+
* 输出: 5
26+
*
27+
* 解释: 一个最短转换序列是 "hit" -> "hot" -> "dot" -> "dog" -> "cog",
28+
* 返回它的长度 5。
29+
* 示例 2:
30+
*
31+
* 输入:
32+
* beginWord = "hit"
33+
* endWord = "cog"
34+
* wordList = ["hot","dot","dog","lot","log"]
35+
*
36+
* 输出: 0
37+
*
38+
* 解释: endWord "cog" 不在字典中,所以无法进行转换。
39+
*/
40+
public class _127_WordLadder {
741

842
public int ladderLength(String start, String end, HashSet<String> dict) {
943
if (start == null || end == null || start.length() == 0 || end.length() == 0 || start.length() != end.length())

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main.java.leetcode.search;
1+
package leetcode.dfsbfsbacktracing;
22

33
/**
44
* Given a 2D board and a word, find if the word exists in the grid.
@@ -17,8 +17,26 @@
1717
word ="ABCCED", -> returnstrue,
1818
word ="SEE", -> returnstrue,
1919
word ="ABCB", -> returnsfalse.
20+
给定一个二维网格和一个单词,找出该单词是否存在于网格中。
21+
22+
单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格。同一个单元格内的字母不允许被重复使用。
23+
24+
示例:
25+
26+
board =
27+
[
28+
['A','B','C','E'],
29+
['S','F','C','S'],
30+
['A','D','E','E']
31+
]
32+
33+
给定 word = "ABCCED", 返回 true.
34+
给定 word = "SEE", 返回 true.
35+
给定 word = "ABCB", 返回 false.
36+
2037
*/
21-
public class WordSearch {
38+
public class _79_WordSearch {
39+
2240
public boolean exist(char[][] board, String word) {
2341
if(board==null||board.length==0||board[0].length==0){
2442
return false;

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.

0 commit comments

Comments
 (0)