Skip to content

Commit

Permalink
2020.11.9
Browse files Browse the repository at this point in the history
  • Loading branch information
1170300619 committed Nov 9, 2020
1 parent fe1d0f4 commit 2e99596
Show file tree
Hide file tree
Showing 28 changed files with 1,060 additions and 1 deletion.
1 change: 0 additions & 1 deletion 14.最长公共前缀.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
// @lc code=start
class Solution {
public String longestCommonPrefix(String[] strs) {
// StringBuilder ans = new StringBuilder();
if(strs.length == 1){
return strs[0];
}
Expand Down
15 changes: 15 additions & 0 deletions 15.三数之和.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* @lc app=leetcode.cn id=15 lang=cpp
*
* [15] 三数之和
*/

// @lc code=start
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {

}
};
// @lc code=end

40 changes: 40 additions & 0 deletions 16.最接近的三数之和.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.Arrays;
/*
* @lc app=leetcode.cn id=16 lang=java
*
* [16] 最接近的三数之和
*/

// @lc code=start
class Solution {
public int threeSumClosest(int[] nums, int target) {
int ans = nums[0] + nums[1] + nums[2];
int index = 0;
int t = Integer.MAX_VALUE;
Arrays.sort(nums);
int length = nums.length;
if(nums.length == 3){
return nums[0] + nums[1] + nums[2];
}
if( nums[0] + nums[1] + nums[2] > target){
return nums[0] + nums[1] + nums[2];
}
if(nums[length - 1] + nums[length - 2] + nums[length- 3] < target){
return nums[length - 1] + nums[length - 2] + nums[length- 3];
}
for(int i = 0;i<nums.length - 2;i++){
for(int j = i+ 1;j<nums.length - 1;j++){
for(int k = j + 1;k<nums.length;k++){
index = Math.abs(nums[i] + nums[j] + nums[k] - target);
if(index < t){
t = index;
ans = nums[i] + nums[j] + nums[k];
}
}
}
}
return ans;
}
}
// @lc code=end

37 changes: 37 additions & 0 deletions 17.电话号码的字母组合.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* @lc app=leetcode.cn id=17 lang=cpp
*
* [17] 电话号码的字母组合
*/

// @lc code=start
class Solution
{
public:
string tmp;
vector<string> res;
vector<string> board = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
void DFS(int pos, string digits)
{
if (pos == digits.size())
{
res.push_back(tmp);
return;
}
int num = digits[pos] - '0';
for (int i = 0; i < board[num].size(); i++)
{
tmp.push_back(board[num][i]);
DFS(pos + 1, digits);
tmp.pop_back();
}
}
vector<string> letterCombinations(string digits)
{
if (digits.size() == 0)
return res;
DFS(0, digits);
return res;
}
};
// @lc code=end
14 changes: 14 additions & 0 deletions 17.电话号码的字母组合.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* @lc app=leetcode.cn id=17 lang=java
*
* [17] 电话号码的字母组合
*/

// @lc code=start
class Solution {
public List<String> letterCombinations(String digits) {

}
}
// @lc code=end

15 changes: 15 additions & 0 deletions 18.四数之和.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
* @lc app=leetcode.cn id=18 lang=cpp
*
* [18] 四数之和
*/

// @lc code=start
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {

}
};
// @lc code=end

53 changes: 53 additions & 0 deletions 18.四数之和.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;

/*
* @lc app=leetcode.cn id=18 lang=java
*
* [18] 四数之和
*/

// @lc code=start
class Solution {
List<List<Integer>> res;
public List<List<Integer>> fourSum(int[] nums, int target) {
res = new ArrayList<>();
if(nums.length == 0){
return res;
}
Arrays.sort(nums);
DFS(target, nums, 0, 0, 0, new ArrayList<Integer>());
return res;
}

void DFS(int target, int[] nums,int cur, int size, int sum, List<Integer> list){
if(list.size() == 4){
if(sum == target){
res.add(new ArrayList<>(list));
}
return;
}

Set<Integer> set = new HashSet<>();
for(int i = cur;i<nums.length;i++){
//去重
if(set.contains(nums[i])) continue;
//剪枝: 当剩余元素个数不满足所需个数时返回
if(nums.length - i < (4 - size)) return;
//剪枝: 当剩余所需元素都用后续最小元素填充时还是超过了target返回
if(i < nums.length - 1 && sum + nums[i] + (3 - size) * nums[i + 1] > target) return;
//剪枝: 当剩余所需元素都用后续最大元素填充时还是小于target,则返回
if(i < nums.length - 1 && sum + nums[i] + (3 - size) * nums[nums.length - 1] < target) continue;

//回溯
set.add(nums[i]);
list.add(nums[i]);
DFS(target, nums, i + 1, size + 1, sum + nums[i], list);
list.remove(list.size() - 1);
}
}
}
// @lc code=end

51 changes: 51 additions & 0 deletions 19.删除链表的倒数第n个节点.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* @lc app=leetcode.cn id=19 lang=java
*
* [19] 删除链表的倒数第N个节点
*/

// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode removeNthFromEnd(ListNode head, int n) {
ListNode index = head;
int sum = 0;
while(index.next != null){
sum++;
index = index.next;
}
sum++;//5
if(sum == n){
head = head.next;
return head;
}

int i = 0;
ListNode left = head;
index = head;
while(i < sum - n){
index = index.next;
if(i > 0){
left = left.next;
}
i++;
}
if(index.next == null){
left.next = null;
return head;
}
left.next = index.next;
return head;
}
}
// @lc code=end

23 changes: 23 additions & 0 deletions 20.有效的括号.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import java.util.LinkedList;

/*
* @lc app=leetcode.cn id=20 lang=java
*
* [20] 有效的括号
*/

// @lc code=start
class Solution {
public boolean isValid(String s) {
LinkedList<Character> stack = new LinkedList<>();
for(char c : s.toCharArray()){
if(c == '[') stack.push(']');
else if(c == '{') stack.push('}');
else if(c == '(') stack.push(')');
else if(stack.isEmpty() || c != stack.pop()) return false;
}
return stack.isEmpty();
}
}
// @lc code=end

67 changes: 67 additions & 0 deletions 21.合并两个有序链表.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.ListModel;

/*
* @lc app=leetcode.cn id=21 lang=java
*
* [21] 合并两个有序链表
*/

// @lc code=start
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode res = new ListNode();
if(l1 == null && l2 != null){
return l2;
}else if(l1 != null && l2 == null){
return l1;
}
else if(l1 == null && l2 == null){
return null;
}
ListNode head = res;
ArrayList<Integer> list = new ArrayList<>();
list.add(l1.val);
list.add(l2.val);
int i = 2;
while(l1.next != null){
l1 = l1.next;
list.add(l1.val);
i++;
}
while(l2.next != null){
l2 = l2.next;
list.add(l2.val);
i++;
}
list.sort(Comparator.naturalOrder());
int length = i;
i = 0;
while(i < length){
ListNode index = new ListNode();
res.val = list.get(i);
if(i < length - 1){
res.next = index;
res = res.next;
i++;
}
else{
i++;
}
}
return head;
}
}
// @lc code=end
38 changes: 38 additions & 0 deletions 22.括号生成.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import java.util.ArrayList;

/*
* @lc app=leetcode.cn id=22 lang=java
*
* [22] 括号生成
*/

// @lc code=start
class Solution {
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
if(n <= 0){
return res;
}
getParenthesis("",n,n);
return res;
}

private void getParenthesis(String str, int left, int right){
if(left == 0 && right == 0){
res.add(str);
return;
}

if(left == right){
getParenthesis(str + '(', left - 1, right);
}
else if(left < right){
if(left > 0){
getParenthesis(str + '(', left - 1, right);
}
getParenthesis(str + ')', left, right - 1);
}
}
}
// @lc code=end

Loading

0 comments on commit 2e99596

Please sign in to comment.