1. 程式人生 > >【LeetCode】LeetCode——第15題:3Sum

【LeetCode】LeetCode——第15題:3Sum

15. 3Sum

   My Submissions Total Accepted: 115091 Total Submissions: 612138 Difficulty: Medium

Given an array S of n integers, are there elements abc in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

  • Elements in a triplet (a
    ,b,c) must be in non-descending order. (ie, a ≤ b ≤ c)
  • The solution set must not contain duplicate triplets.
    For example, given array S = {-1 0 1 2 -1 -4},

    A solution set is:
    (-1, 0, 1)
    (-1, -1, 2)

Subscribe to see which companies asked this question

Show Tags Show Similar Problems

題目的大概意思是:給定一組整數,找出所有能使和為0

的三個數,且不重複。

這道題難度等級:中等

思路:

1、與題目類似,先將陣列nums進行排序,然後從左往右依次列舉,在列舉的過程中左右夾逼;

2、列舉位置為i,當nums[i]為正數時,結束列舉(因為不可能三個正數之和為0);另外,要列舉過整數不再重複列舉;

3、列舉到i時,左、右位置分別用lr表示,當nums[i]+nums[l]+nums[r]>0,右邊界往左夾逼;當nums[i]+nums[l]+nums[r]<0,左邊界往右夾逼;當nums[i]+nums[l]+nums[r]==0,則算找到一個,要存起來,再將左邊界往右夾逼同時要跳過與之前左邊界相同的值,否則會出現重複的三個數。

4、在步驟3的整個過程中,始終要保證l<r

程式碼如下:

class Solution {
public:
    vector<vector<int>> threeSum(vector<int>& nums) {
		vector<vector<int> > res;
		sort(nums.begin(), nums.end());
		vector<int> tmp(3,0);
		int l, r;
		for (int i = 0; i < nums.size() && nums[i] <= 0; ++i){//從左往右列舉
			if (nums[i] != nums[i - 1] || i == 0){
				l = i + 1; r = nums.size() - 1;
				while(l < r ){
					while (l < r && nums[i] + nums[l] + nums[r] > 0){--r;}//限制右邊界
					if (l < r && nums[i] + nums[l] + nums[r] == 0){
					    tmp[0] = nums[i]; tmp[1] = nums[l]; tmp[2] = nums[r];
					    res.push_back(tmp);
						while(l < r && nums[l] == tmp[1]){++l;}//限制左邊界
					}
					else{
						++l;
					}
				}
			}
		}
		return res;
    }
};
提交程式碼後,AC掉該題,Runtime: 52 ms

相關推薦

LeetCodeLeetCode——153Sum

15. 3Sum    My Submissions Question Editorial Solution Total Accepted: 115091 Total Submiss

Leetcode15三數值和

程式碼以及註釋如下所示:import java.util.*; /** * 先升序排序,然後用第一重for迴圈確定第一個數字。 然後在第二重迴圈裡,第二、第三個數字分別從兩端往中間掃。 如果三個數的sum等於0,得到一組解。 如果三個數的sum小於0,說明需要增

leetcode 15兩數求和的擴充套件

題目大意:   給定一個包含n個整數的陣列,試問能否找到三個的元素,使得它們的和為零?返回所有可能的元素集合。 說明:   返回的是一個三元不定長陣列的集合,其中的元素不可重複 示例:   輸入vector S:[-1,0,1,2,-1,-4]

LeetCodeLeetCode——7Reverse Integer

7. Reverse Integer    My Submissions Question Editorial Solution Total Accepted: 134949 Tota

leetcode7逆轉數字

題目如下: package com.leetcode; public class Solution7_reverseNumbers { public int reverseNumber(int

LeetCodeLeetCode——2Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single dig

LeetCodeLeetCode——1Two Sum

Given an array of integers, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numb

LeetCodeLeetCode——20Valid Parentheses

20. Valid Parentheses My Submissions Question Editorial Solution Total Accepted: 106450 Total

LeetCodeLeetCode——11Container With Most Water

11. Container With Most Water    My Submissions Question Editorial Solution Total Accepted: 

LeetCodeLeetCode——184Sum

18. 4Sum    My Submissions Question Editorial Solution Total Accepted: 71353 Total Submissio

OCP-052052考試題庫一變再變,完整庫收集整理-15

manage have table spec bitmap oca alloc true aps 15、Which two are true about space management in tablespaces? A) Locally managed tablespa

leetcode32最長有效括號(遇到一個奇葩的錯誤)

問題描述: 給一個只包含 '(' 和 ')' 的字串,找出最長的有效(正確關閉)括號子串的長度。 對於 "(()",最長有效括號子串為 "()" ,它的長度是 2。 另一個例子 ")()())",最長有效括號子

LeetCode60. k個排列

題目連結:https://leetcode-cn.com/problems/permutation-sequence/description/ 題目描述 給出集合 [1,2,3,…,n],其所有元素共有 n! 種排列。 按大小順序列出所有排列情況,並一一標記,當 n = 3

LeetCode 779. K個語法符號

遞迴 題目 在第一行我們寫上一個 0。接下來的每一行,將前一行中的0替換為01,1替換為10。 給定行數 N 和序數 K,返回第 N 行中第 K個字元。(K從1開始) 例子: 輸入: N = 1, K = 1 輸出: 0 輸入: N = 2, K = 1 輸

演算法LeetCode演算法-Length Of Last Word

這是悅樂書的第155次更新,第157篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第14題(順位題號是58)。給定一個字串,包含戴爾字母、小寫字母和空格,返回最後一個單詞的長度,如果最後一個單詞不存在則返回0。另外,單詞不包含空格。例如:

演算法LeetCode演算法-Maximum Subarray

這是悅樂書的第154次更新,第156篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第13題(順位題號是53)。給定一個整數陣列nums,找出一個最大和,此和是由陣列中索引連續的元素組成,至少包含一個元素。例如: 輸入:[-2, 1, -

演算法LeetCode演算法-Count And Say

這是悅樂書的第153次更新,第155篇原創 01 看題和準備 今天介紹的是LeetCode演算法題中Easy級別的第12題(順位題號是38)。count-and-say序列是整數序列,前五個術語如下: 1 11 21 1211

leetcode72編輯距離

給定兩個單詞 word1 和 word2,計算出將 word1 轉換成 word2 所使用的最少運算元 。 你可以對一個單詞進行如下三種操作: 插入一個字元 刪除一個字元 替換一個字元 示例 1:

LeetCode207. 課程表 結報告 (C++)

原題地址:https://leetcode-cn.com/problems/course-schedule/description/ 題目描述: 現在你總共有 n 門課需要選,記為 0 到 n-1。 在選修某些課程之前需要一些先修課程。 例如,想要學習課程 0 ,你需要先完成課程 1 ,

sqlleetcode習題 (共 42 )

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } 【175】Combine Two Tables (2018年11月23日,開始集中review基礎) Table: Person +-------------+