Skip to content

Commit

Permalink
2020.9.17
Browse files Browse the repository at this point in the history
  • Loading branch information
1170300619 committed Sep 17, 2020
1 parent 0980eb3 commit fe1d0f4
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
44 changes: 44 additions & 0 deletions 14.最长公共前缀.java
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

54 changes: 54 additions & 0 deletions 15.三数之和.java
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

0 comments on commit fe1d0f4

Please sign in to comment.