n数求和,固定n-2个数,最后两个数在连续区间内一左一右根据当前求和与目标值比较移动,如果sum<target,移动较小数,否则,移动较大数

重复数处理:

使i为左至右第一个不重复数:while(i != 0 && i<n-2 && a[i] == a[i-1]) i++;

使r为右至左第一个不重复数(原序最后一个):while(r>i && r+1<n && a[r] == a[r+1]) r--;

15. 3Sum

class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums) {
sort(nums.begin(), nums.end());
vector<vector<int>> ans;
int n = nums.size();
vector<int> &a = nums;
for(int i=; i<n-; i++) {
while(i != && i<n- && a[i] == a[i-]) i++;
int l = i+, r = n-;
while(l < r) {
int s = a[i]+a[r]+a[l];
if(s == ) {
ans.push_back({a[i], a[l], a[r]});
l++; r = n-;
while(l<r && a[l] == a[l-]) l++;
} else if(s < ) {
l++;
while(l<r && a[l] == a[l-]) l++;
} else {
r--;
while(r>i && r+<n && a[r] == a[r+]) r--;
}
}
}
return ans;
}
};

16. 3Sum Closest

class Solution {
public:
int threeSumClosest(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int n = nums.size();
int mind = INT_MAX, ans = ;
vector<int> &a = nums;
for(int i=; i<n-; i++) {
int l = i+, r = n-;
while(l < r) {
int s = a[i]+a[r]+a[l];
int d = abs(s - target);
if(s == target)
return s;
else if(d < mind) {
mind = d;
ans = s;
}
if(s < target)
l++;
else
r--;
}
}
return ans;
}
};

18. 4Sum

class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target) {
vector<int> &a = nums;
sort(a.begin(), a.end());
vector<vector<int>> ans;
int n = a.size();
if(n < ) return ans;
for(int i=; i<n-; i++) {
while(i != && i<n- && a[i] == a[i-]) i++;
for(int j=i+; j<n-; j++) {
while(j != i+ && j<n- && a[j] == a[j-]) j++;
int l = j+, r = n-;
while(l < r) {
int s = a[i]+a[j]+a[r]+a[l];
if(s == target) {
ans.push_back({a[i], a[j], a[l], a[r]});
l++; r = n-;
while(l<r && a[l] == a[l-]) l++;
} else if(s < target) {
l++;
while(l<r && a[l] == a[l-]) l++;
} else {
r--;
while(r>j && r+<n && a[r] == a[r+]) r--;
}
}
}
}
return ans;
}
};

LeetCode 15. 3Sum 16. 3Sum Closest 18. 4Sum的更多相关文章

  1. 1. Two Sum + 15. 3 Sum + 16. 3 Sum Closest + 18. 4Sum + 167. Two Sum II - Input array is sorted + 454. 4Sum II + 653. Two Sum IV - Input is a BST

    ▶ 问题:给定一个数组 nums 及一个目标值 target,求数组中是否存在 n 项的和恰好等于目标值 ▶ 第 1题,n = 2,要求返回解 ● 代码,160 ms,穷举法,时间复杂度 O(n2), ...

  2. 【LeetCode】18. 4Sum 四数之和

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:four sum, 4sum, 四数之和,题解,leet ...

  3. leetcode 1.Two Sum 、167. Two Sum II - Input array is sorted 、15. 3Sum 、16. 3Sum Closest 、 18. 4Sum 、653. Two Sum IV - Input is a BST

    1.two sum 用hash来存储数值和对应的位置索引,通过target-当前值来获得需要的值,然后再hash中寻找 错误代码1: Input:[3,2,4]6Output:[0,0]Expecte ...

  4. 15. 3Sum、16. 3Sum Closest和18. 4Sum

    15 3sum Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = ...

  5. [LeetCode][Python]16: 3Sum Closest

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 16: 3Sum Closesthttps://oj.leetcode.com ...

  6. 1. Two Sum&&15. 3Sum&&18. 4Sum

    题目: 1. Two Sum Given an array of integers, return indices of the two numbers such that they add up t ...

  7. LeetCode 16. 3Sum Closest(最接近的三数之和)

    LeetCode 16. 3Sum Closest(最接近的三数之和)

  8. [LeetCode] 16. 3Sum Closest 最近三数之和

    Given an array nums of n integers and an integer target, find three integers in nums such that the s ...

  9. 《LeetBook》leetcode题解(16):3Sum Closest [M]

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

随机推荐

  1. LOJ 2409「THUPC 2017」小 L 的计算题 / Sum

    思路 和玩游戏一题类似 定义\(A_k(x)=\sum_{i=0}^\infty a_k^ix^i=\frac{1}{1-a_kx}\) 用\(\ln 'x\)代替\(\frac{1}{x}\), 所 ...

  2. (Code) Python implementation of phrase extraction from sentence

    import os import numpy as np import pandas as pd from tqdm import tqdm import numpy as np import str ...

  3. 使用Spring MVC实现数据绑定

    使用Spring MVC实现数据绑定 ——Spring MVC支持将表单参数自动绑定在映射方法的参数. ①绑定标量数据 前端代码: <form action="${pageContex ...

  4. ASP.NET MVC WebAPI Put和Delete请求出现405(Method not allowed)错误

    解决办法: 在站点根目录下的web.config设置如下(主要参考添加项): <system.webServer> <modules> <remove name=&quo ...

  5. javascript 删除 url 中指定参数,并返回 url

    // 删除url中某个参数,并跳转function funcUrlDel(name){ var loca = window.location; var baseUrl = loca.origin + ...

  6. pytest-xdist分布式执行测试用例

    前言 平常我们手工测试用例非常多时,比如有1千条用例,假设每个用例执行需要1分钟.如果一个测试人员执行需要1000分钟才能执行完,当项目非常紧急的时候,我们会用测试人力成本换取时间成本,这个时候多找个 ...

  7. oledb

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...

  8. c#格林治时间实现

    C#时间戳的简单实现   Introduction: 在项目开发中,我们都经常会用到时间戳来进行时间的存储和传递,最常用的Unix时间戳(TimeStamp)是指格林尼治时间1970年1月1日0时(北 ...

  9. Nginx启动,证书报错SSL_CTX_use_PrivateKey_file.....

    报错nginx: [emerg] SSL_CTX_use_PrivateKey_file("/etc/nginx/ssl/myxxxxgame201904.key") failed ...

  10. AutoField的话就报错:'AutoField' object has no attribute 'rel'

    def data_inspect(self, data, extra=None): if isinstance(data, (QuerySet, Page, list)): convert_data ...