3SumJan 18 '128685 / 33750
Given an array S of n integers, are there elements a, b, c 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)
class Solution { public: vector<vector<int> > threeSum(vector<int> &num) { sort(num.begin(), num.end()); vector<vector<int> > res; int len = num.size(); for (int i = 0; i < len - 2; i++) { if (i > 0 && num[i] == num[i-1]) continue; int a = i+1, b = len -1; int t = -num[i]; while (a < b) { if (num[a] + num[b] == t) { int tmp[3] = {-t, num[a], num[b]}; vector<int> tmp2(tmp, tmp+3); res.push_back(std::move(tmp2)); while (a < b && num[a] == num[a+1]) a++; a++; while (a < b && num[b] == num[b-1]) b--; b--; } else if (num[a] + num[b] > t) { b--; } else a++; } } return res; } };
3Sum ClosestJan 18 '125336 / 13935
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1. The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).» Solve this problem
class Solution { public: int threeSumClosest(vector<int> &num, int target) { sort(num.begin(), num.end()); int len = num.size(); int mindelta = 1 << 30; int res; for (int i = 0; i < len - 2; i++) { if (i > 0 && num[i] == num[i-1]) continue; int a = i+1, b = len -1; int t = target - num[i]; while (a < b) { int delta = t - num[a] - num[b]; if (delta == 0) return target; else if (delta > 0) { if (delta < mindelta) mindelta = delta, res = target - delta; a++; } else { if (-delta < mindelta) mindelta = -delta, res = target- delta; b--; } } } return res; } };
相关推荐
16 | [3 Sum Closest](https://leetcode.com/problems/3sum-closest/) | [C++](./C++/3sum-closest.cpp) [Python](./Python/3sum-closest.py) | _O(n^2)_ | _O(1)_ | Medium || Two Pointers 18| [4 Sum]...
leetcode 答案LeetCode / LintCode(Java) 在 LeetCode 和 LintCode 中记录问题的答案和解释。 如果您遇到有关答案的任何问题并需要进一步解释,请随时与我联系。 + Data Structure + Algorithm 进步 403/573 阵列 94...
双指针算法,python数组双指针算法求和问题LeetCode2sum3sum4sum含代码
文档python数组双指针算法求和问题LeetCode2sum3sum4sum含代码提取方式是百度网盘分享地址
leetcode中国 InterView_Problem English LeetCode / NowCoder / Timus ... Problems code. (Mainly Java implementation) Chinese 刷题代码,主要是java实现,可能会有其他语言代码 代码来自LeetCode / NowCoder ...
leetcode 答案leet-code 对 LeetCode 问题的回答,参考关于这些问题的讨论。
这是来自LeetCode的已解决任务的存储库使用Java语言解决任务 CoinChange.java - //leetcode.com/problems/coin-change/ ProductOfArrayExceptSelf.java - //leetcode.com/problems/product-of-array-except-self/ ...
vscode提交leetcode Leetcode刷题存档 准备 vscode 安装 leetcode 插件 添加leetcode到bin ln -s ~/.vscode/extensions/leetcode.vscode-leetcode-0.17.0/node_modules/vsc-leetcode-cli/bin/leetcode /usr/local/...
Leetcode two sum java 解法
收集面试中提出的一些重要问题。...数组https://leetcode.com/problems/3sum/ [解决方案] https://leetcode.com/problems/trapping-rain-water/ [解决方案] https://leetcode.com/problems/move-zeroes/ ...
https://leetcode.com/problems/two-sum/ Two Sum 10 https://leetcode.com/problems/regular-expression-matching/ Regular Expression Matching 100 https://leetcode.com/problems/same-tree/ Same Tree 101 ...
leetcode 和 oj OJ_Solution Solutions ...3, java/dungenegame https://leetcode.com/problems/dungeon-game/ 4, java/findminrotaed 5, java/houserobber https://leetcode.com/problems/house-robber/
leetcode中文版[目录] 醉水 # @filename : leetcode # @author : Copyright (C) drunkwater # @date : Tue Jan 16 12:59:51 HKT 2018 # @function : # @see : # @require : /* # @filename : leetcode # @author : ...
leetcode寻找最近的 其他 ...https://github.com/yangyu2010/leetcode/blob/master/leetcode/1两数之和.py 整数反转 https://github.com/yangyu2010/leetcode/blob/master/leetcode/7整数反转.py 加一 ...
leetcode 答案SQL-练习 详细问题请访问 Leetcode:此存储库仅包含我的想法和问题的答案。
lru缓存leetcode Leetcode练习题极客时间算法训练营题目练习 基础算法 简单的二和 EasyValid 括号 中解码字符串https://leetcode.com/problems/decode-string/ HardLRU 缓存...
这是我准备面试时建议的个人问题清单。 图表 数组 散列 链表 https://leetcode.com/problems/reverse-linked-list/ ...https://leetcode.com/problems/positions-of-large-g
Python3 编写。 所有这些都是在时间压力下完成的,而且斗志昂扬,无人爱戴。 我强烈推荐 Leetcode() 和付费订阅功能,因为很多解决方案都提供了清晰的算法分析和干净的代码示例。 然而,许多问题没有解决方案,有些...
leetcode 2 sum c JS 算法与数据结构(LEETCODE) 环境安装 安装依赖 npm install --save-dev jest //单元测试 npm install babel-jest babel-core@^7.0.0-bridge.0 @babel/core regenerator-runtime babel-preset-env...
leetcode 答案leetcode 回答