Leetcode 18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0. A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
本题和3Sum差不多,但是更复杂一点的是,需要用两个for loop来循环 i, j, k, l 的前两个。然后 k,l 的处理方式和3Sum中一样。
class Solution(object):
def fourSum(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: List[List[int]]
""" nums.sort()
n = len(nums)
ans = []
for i in range(n):
if i>0 and nums[i] == nums[i-1]: continue
for j in range(i+1, n):
if j>i+1 and nums[j] == nums[j-1]: continue
k, l = j+1, n-1
while k < l:
if nums[i] + nums[j] + nums[k] + nums[l] < target:
k += 1
elif nums[i] + nums[j] + nums[k] + nums[l] > target:
l -= 1
else:
ans.append([nums[i], nums[j], nums[k], nums[l]])
while k < l and nums[k] == nums[k+1]: k += 1
while k < l and nums[l] == nums[l-1]: l -= 1
k += 1; l -= 1
return ans
Leetcode 18. 4Sum的更多相关文章
- [LeetCode] 18. 4Sum 四数之和
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- LeetCode——18. 4Sum
一.题目链接:https://leetcode.com/problems/4sum/ 二.题目大意: 给定一个数组A和一个目标值target,要求从数组A中找出4个数来使之构成一个4元祖,使得这四个数 ...
- LeetCode 18 4Sum (4个数字之和等于target)
题目链接 https://leetcode.com/problems/4sum/?tab=Description 找到数组中满足 a+b+c+d=0的所有组合,要求不重复. Basic idea is ...
- [LeetCode] 18. 4Sum ☆☆
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- LeetCode 18. 4Sum (四数之和)
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- leetcode 15 3sum & leetcode 18 4sum
3sum: 1 class Solution { public: vector<vector<int>> threeSum(vector<int>& num ...
- Java [leetcode 18]4Sum
问题描述: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d ...
- C#解leetcode 18. 4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = tar ...
- [leetcode]18. 4Sum四数之和
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums s ...
随机推荐
- System.nanoTime与System.currentTimeMillis的理解与区别
System类代表系统,系统级的很多属性和控制方法都放置在该类的内部.该类位于java.lang包. 平时产生随机数时我们经常拿时间做种子,比如用System.currentTimeMillis的结果 ...
- 分析js中的constructor 和prototype
在javascript的使用过程中,constructor 和prototype这两个概念是相当重要的,深入的理解这两个概念对理解js的一些核心概念非常的重要. 我们在定义函数的时候,函数定义的时候函 ...
- 面向初学者之烦人的mainactivity启动前的actionBAR
相信各位初学者的童鞋都遇到过一个问题,(大神们就别喷我哦,多多帮帮指正,嘿嘿)那就是当你点开你开发的软件或者是dome时,会发现这么一个问题: 你曾今以为你的软件点开的时候是这样的: 然而事实是残酷的 ...
- 【译】Spring 4 基于TaskScheduler实现定时任务(注解)
前言 译文链接:http://websystique.com/spring/spring-job-scheduling-with-scheduled-enablescheduling-annotati ...
- tftp-nfs开发环境搭建
嵌入式开发通常使用主机-开发板的开发模式,在裸板开发中,我们通常使用串口调试工具传递文件,比如windows平台的超级终端,SecuCRT以及Linux平台的ckermit(题外话:ckermit比w ...
- RabbitMq 集群搭建
实验环境: 操作系统为 Centos 7.2 IP hostName 192.168.190.132 node132 192.168.190.139 node139 192.168.190.1 ...
- Android 强制设置横屏或竖屏 设置全屏
(转自:http://blog.csdn.net/yuejingjiahong/article/details/6636981) 强制横屏: @Override protected void onRe ...
- Linux文件操作的主要接口API及相关细节
操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...
- python学习3
这部分学习python函数的写法: 在写python函数之前,首先看一下python中已经写好的函数,就像C中的<math>中的那些函数一样的东西有哪些:网址为https://docs.p ...
- 安装Windows更新程序遇到错误:0x80070422
看看服务那里 windows update服务是不是被禁用了? 还有一个问题可能是由于Windows Modules Installer被禁用了.