Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitad9f834

Browse files
author
zhenyu zhang
committed
update LeetCode 18、34、40
1 parentc1d5321 commitad9f834

File tree

6 files changed

+254
-2
lines changed

6 files changed

+254
-2
lines changed

‎src/com/hadley/Main.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,34 @@
11
packagecom.hadley;
22

3+
4+
importjava.util.Stack;
5+
36
publicclassMain {
47

5-
publicstaticvoidmain(String[]args) {
6-
// write your code here
8+
publicintlongestValidParentheses(Strings) {
9+
intresult =0;
10+
//用栈来进行处理
11+
Stack<Integer>stack =newStack<>();
12+
intindex = -1;//指针
13+
14+
for(inti =0;i <s.length();i++){
15+
if(s.charAt(i) =='('){
16+
stack.push(i);
17+
}else{//s.charAt(i) == ')'
18+
if(stack.isEmpty()){
19+
index =i;
20+
}else{
21+
stack.pop();
22+
if(stack.isEmpty()){//i - index是当前的子串长度
23+
result =Math.max(result,i -index);
24+
}else{
25+
result =Math.max(result,i -stack.peek());
26+
}
27+
}
28+
29+
}
30+
}
31+
returnresult;
732
}
33+
834
}

‎src/com/hadley/Main1.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
packagecom.hadley;
2+
3+
importjava.util.Arrays;
4+
importjava.util.Scanner;
5+
6+
publicclassMain1 {
7+
8+
publicstaticvoidmain(String[]args) {
9+
Scannersc =newScanner(System.in);
10+
intpeople =sc.nextInt();
11+
inttime =sc.nextInt();
12+
int[]coinList =newint[people];
13+
14+
for(inti =0;i <people;i++){
15+
coinList[i] =sc.nextInt();
16+
}
17+
18+
Arrays.sort(coinList);
19+
intmax =coinList[people-1];
20+
intmin =coinList[0];
21+
22+
23+
}
24+
}

‎src/com/hadley/_018.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.05.24 18.4Sum
5+
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
6+
7+
Note:
8+
9+
The solution set must not contain duplicate quadruplets.
10+
11+
Example:
12+
13+
Given array nums = [1, 0, -1, 0, -2, 2], and target = 0.
14+
15+
A solution set is:
16+
[
17+
[-1, 0, 0, 1],
18+
[-2, -1, 1, 2],
19+
[-2, 0, 0, 2]
20+
]
21+
*/
22+
23+
importjava.util.ArrayList;
24+
importjava.util.Arrays;
25+
importjava.util.List;
26+
27+
publicclass_018 {
28+
publicList<List<Integer>>fourSum(int[]nums,inttarget) {
29+
Arrays.sort(nums);
30+
List<List<Integer>>resultList =newArrayList<>();
31+
//the difference between ArrayList and List:https://www.cnblogs.com/zcscnn/p/7743507.html
32+
33+
if(nums.length <4){
34+
returnresultList;
35+
}else{
36+
//TODO
37+
returnnull;
38+
}
39+
40+
41+
}
42+
}

‎src/com/hadley/_034.java

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
packagecom.hadley;
2+
3+
/*
4+
2020.06.20
5+
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
6+
7+
Your algorithm's runtime complexity must be in the order of O(log n).
8+
9+
If the target is not found in the array, return [-1, -1].
10+
11+
Example 1:
12+
13+
Input: nums = [5,7,7,8,8,10], target = 8
14+
Output: [3,4]
15+
Example 2:
16+
17+
Input: nums = [5,7,7,8,8,10], target = 6
18+
Output: [-1,-1]
19+
*/
20+
21+
publicclass_034 {
22+
23+
//time complexity: O(n)
24+
publicint[]searchRange(int[]nums,inttarget) {
25+
int[]result = {-1,-1};
26+
27+
booleanflag =false;
28+
29+
for(inti =0;i <nums.length;i++){
30+
if(nums[i] <target){
31+
continue;
32+
}
33+
34+
if(nums[i] ==target && !flag){
35+
result[0] =i;
36+
result[1] =i;
37+
flag =true;
38+
}elseif(nums[i] ==target &&flag){
39+
result[1] =i;
40+
}
41+
}
42+
43+
returnresult;
44+
}
45+
46+
//come from the Solution time complexity: O(logN)
47+
publicint[]searchRange1(int[]nums,inttarget){
48+
intleft =0;
49+
intright =nums.length -1;
50+
intl =left;
51+
intr =right;
52+
53+
//find the left border of target
54+
while(left <right){
55+
intmid = (left +right)/2;
56+
if(nums[mid] <target){
57+
left =mid +1;
58+
}else{
59+
right =mid;
60+
}
61+
}
62+
63+
if(left >right||nums[left]!=target){
64+
returnnewint[]{-1,-1};
65+
}
66+
67+
//find the right border of target
68+
while(l <r){
69+
intmid = (l +r)/2 +1;
70+
if(nums[mid] >target){
71+
r =mid -1;
72+
}else{
73+
l =mid;
74+
}
75+
}
76+
77+
if(left >right ||left >r){
78+
returnnewint[]{-1,-1};
79+
}else{
80+
returnnewint[]{left,r};
81+
}
82+
}
83+
}

‎src/com/hadley/_040.java

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
packagecom.hadley;
2+
3+
importjava.util.ArrayList;
4+
importjava.util.Arrays;
5+
importjava.util.List;
6+
7+
/*
8+
2020.05.07
9+
Given a collection of candidate numbers (candidates) and a target number (target),
10+
find all unique combinations in candidates where the candidate numbers sums to target.
11+
12+
Each number in candidates may only be used once in the combination.
13+
14+
Note:
15+
16+
All numbers (including target) will be positive integers.
17+
The solution set must not contain duplicate combinations.
18+
Example 1:
19+
20+
Input: candidates = [10,1,2,7,6,1,5], target = 8,
21+
A solution set is:
22+
[
23+
[1, 7],
24+
[1, 2, 5],
25+
[2, 6],
26+
[1, 1, 6]
27+
]
28+
Example 2:
29+
30+
Input: candidates = [2,5,2,1,2], target = 5,
31+
A solution set is:
32+
[
33+
[1,2,2],
34+
[5]
35+
]
36+
*/
37+
publicclass_040 {
38+
39+
publicList<List<Integer>>combinationSum2(int[]candidates,inttarget) {
40+
List<List<Integer>>ans =newArrayList<>();
41+
Arrays.sort(candidates);
42+
dfs(candidates,target,0,newArrayList<Integer>(),ans);
43+
44+
returnans;
45+
}
46+
47+
/*
48+
curList = current operating List
49+
*/
50+
publicvoiddfs(int[]candidates,inttarget,intindex,List<Integer>curList,List<List<Integer>>ans){
51+
if(target ==0){
52+
ans.add(newArrayList<>(curList));
53+
return;
54+
}
55+
56+
for(inti =index;i <candidates.length;i++){
57+
if(candidates[i] >target){
58+
break;
59+
}
60+
61+
//skip the same element
62+
if(i >index &&candidates[i] ==candidates[i-1]){
63+
continue;
64+
}
65+
curList.add(candidates[i]);
66+
dfs(candidates,target -candidates[i],i +1,curList,ans);
67+
curList.remove(curList.size()-1);
68+
}
69+
70+
}
71+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
packagecom.hadley.challenge30days;
2+
3+
publicclassLRUCache {
4+
5+
//TODO
6+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp