Skip to content

Commit 1d73fc1

Browse files
committed
增加code
1 parent 0a8d0cf commit 1d73fc1

File tree

2 files changed

+70
-0
lines changed

2 files changed

+70
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* @lc app=leetcode.cn id=28 lang=java
3+
*
4+
* [28] 找出字符串中第一个匹配项的下标
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public int strStr(String haystack, String needle) {
10+
if(haystack == null || needle == null || haystack.length() == 0 || needle.length() == 0) {
11+
return -1;
12+
}
13+
14+
int hLen = haystack.length();
15+
int nLen = needle.length();
16+
int i = 0;
17+
int j = 0;
18+
int temp = i;
19+
while(i < hLen && j < nLen) {
20+
if(haystack.charAt(i) == needle.charAt(j)) {
21+
i++;
22+
j++;
23+
if(j == nLen) {
24+
return temp;
25+
}
26+
} else {
27+
i = temp+1;
28+
j = 0;
29+
temp = i;
30+
}
31+
}
32+
33+
return -1;
34+
}
35+
}
36+
// @lc code=end
37+

java/78.子集.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* @lc app=leetcode.cn id=78 lang=java
3+
*
4+
* [78] 子集
5+
*/
6+
7+
// @lc code=start
8+
class Solution {
9+
public List<List<Integer>> subsets(int[] nums) {
10+
List<List<Integer>> result = new ArrayList<>();
11+
12+
if(nums == null) {
13+
return null;
14+
}
15+
16+
List<Integer> list = new ArrayList();
17+
18+
backtrack(nums, 0, nums.length, list, result);
19+
return result;
20+
}
21+
22+
private void backtrack(int[] nums, int index, int nLen,List<Integer> list, List<List<Integer>> result) {
23+
result.add(new ArrayList(list));
24+
25+
for(int i = index; i < nLen; i++) {
26+
list.add(nums[i]);
27+
backtrack(nums, i+1, nLen, list, result);
28+
list.remove(list.size()-1);
29+
}
30+
}
31+
}
32+
// @lc code=end
33+

0 commit comments

Comments
 (0)