-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0980eb3
commit fe1d0f4
Showing
2 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
* @lc app=leetcode.cn id=14 lang=java | ||
* | ||
* [14] 最长公共前缀 | ||
*/ | ||
|
||
// @lc code=start | ||
class Solution { | ||
public String longestCommonPrefix(String[] strs) { | ||
// StringBuilder ans = new StringBuilder(); | ||
if(strs.length == 1){ | ||
return strs[0]; | ||
} | ||
if(strs.length == 0){ | ||
return ""; | ||
} | ||
String index = strs[0]; | ||
for(int i=1;i<strs.length - 1;i++){ | ||
if(index.length() > strs[i].length()){ | ||
index = strs[i]; | ||
} | ||
} | ||
int flag = 0; | ||
while(index.length() > 0){ | ||
for(int i = 0;i<strs.length;i++){ | ||
if(strs[i].startsWith(index)){ | ||
flag = 1; | ||
continue; | ||
}else{ | ||
flag = 0; | ||
break; | ||
} | ||
} | ||
if(flag == 1){ | ||
return index; | ||
}else{ | ||
index = index.substring(0, index.length() - 1); | ||
} | ||
} | ||
return ""; | ||
} | ||
} | ||
// @lc code=end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/* | ||
* @lc app=leetcode.cn id=15 lang=java | ||
* | ||
* [15] 三数之和 | ||
*/ | ||
|
||
// @lc code=start | ||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
List<List<Integer>> ans = new ArrayList<>(); | ||
|
||
if(nums == null || nums.length < 3){ | ||
return ans; | ||
} | ||
|
||
Arrays.sort(nums); | ||
|
||
if(nums[0]> 0){ | ||
return ans; | ||
} | ||
int start,end = 0; | ||
for(int i = 0;i<nums.length - 2;i++){ | ||
if(i > 0 && (nums[i] == nums[i - 1])){ | ||
continue; | ||
} | ||
start = i + 1; | ||
end = nums.length -1; | ||
while(start < end){ | ||
if(nums[i] + nums[start] + nums[end] == 0){ | ||
ans.add(new ArrayList<>(Arrays.asList(nums[i],nums[start],nums[end]))); | ||
start++; | ||
end--; | ||
while(start < end && nums[start] == nums[start - 1]){ | ||
start++; | ||
} | ||
while(start < end && nums[end] == nums[end + 1]){ | ||
end--; | ||
} | ||
}else if(nums[i] + nums[start] + nums[end] < 0){ | ||
start++; | ||
}else if(nums[i] + nums[start] + nums[end] > 0){ | ||
end--; | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
} | ||
// @lc code=end | ||
|