【Leetcode | 5】求和问题
一、1两数之和
二、15三数之和
C++ Soution 1:
class Solution {
public:
vector<vector<int>> threeSum(vector<int>& nums)
{
vector<vector<int>> res;
sort(nums.begin(), nums.end());
; k < nums.size(); ++k)
{
)
break;
&& nums[k] == nums[k - ])
continue;
- nums[k];
, j = nums.size() - ;
while (i < j)
{
if (nums[i] + nums[j] == target)
{
res.push_back({nums[k], nums[i], nums[j]});
])
++i;
])
--j;
++i;
--j;
}
else if (nums[i] + nums[j] < target)
++i;
else
--j;
}
}
return res;
}
};
三、16最接近的三数之和
C++ Soution 1:
class Solution {
public:
int threeSumClosest(vector<int>& nums, int target)
{
] + nums[] + nums[];
int diff = abs(closest - target);
sort(nums.begin(), nums.end());
; i < nums.size() - ; ++i)
{
, right = nums.size() - ;
while (left < right)
{
int sum = nums[i] + nums[left] + nums[right];
int newDiff = abs(sum - target);
if (diff > newDiff)
{
diff = newDiff;
closest = sum;
}
if (sum < target)
++left;
else
--right;
}
}
return closest;
}
};
四、18四数之和
C++ Soution 1:
class Solution {
public:
vector<vector<int>> fourSum(vector<int>& nums, int target)
{
vector<vector<int>> res;
sort(nums.begin(), nums.end());
if(nums.empty())
return {};
; k < nums.size(); ++k)
{
&& nums[k] == nums[k-])
continue;
; m < nums.size(); ++m)
{
int sum = target - nums[k] -nums[m];
&& nums[m] == nums[m -])
continue;
, j = nums.size() -;
while(i < j)
{
if(sum == nums[i] + nums[j])
{
res.push_back({nums[k], nums[m], nums[i], nums[j]});
])
++i;
])
--j;
++i;
--j;
}
else if(sum > nums[i] + nums[j])
++i;
else
--j;
}
}
}
return res;
}
};
五. 平方数之和
C++ Soution 1:双指针
class Solution {
public:
bool judgeSquareSum(int c)
{
;
long long high = sqrt(c);
while (low < high)
{
if (low*low + high * high == c)
return true;
else if (low*low + high * high > c)
high--;
else
low++;
}
return false;
}
};
367. 有效的完全平方数
C++ Soution 1:
class Solution {
public:
bool isPerfectSquare(int num)
{
) return true;
, right = num;//高低指针
long long mid;
while (left <= right)
{
mid = (left + right) / ;
long long target = mid * mid ; //防止超出int
if (target == num)
return true;
else if (target > num)
right = mid - ; //大了,就降低高指针
else
left = mid + ; //小了,升高低指针
}
return false;
}
};

50. Pow(x, n)
C++ Soution 1:
分析:迭代的解法,我们让i初始化为n,然后看i是否是2的倍数,是的话x乘以自己,否则res乘以x,i每次循环缩小一半,直到为0停止循环。最后看n的正负,如果为负,返回其倒数
class Solution {
public:
double myPow(double x, int n)
{
double res = 1.0;
; i /= )
{
!= )
res *= x;
x *= x;
}
? / res : res;
}
};

【Leetcode | 5】求和问题的更多相关文章
- leetcode 二进制求和 python
class Solution: def addBinary(self, a, b): """ :type a: str :type b: str :rtype: str ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- [LeetCode] Design Excel Sum Formula 设计Excel表格求和公式
Your task is to design the basic function of Excel and implement the function of sum formula. Specif ...
- (leetcode:选择不相邻元素,求和最大问题):打家劫舍(DP:198/213/337)
题型:从数组中选择不相邻元素,求和最大 (1)对于数组中的每个元素,都存在两种可能性:(1)选择(2)不选择,所以对于这类问题,暴力方法(递归思路)的时间复杂度为:O(2^n): (2)递归思路中往往 ...
- LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
题目链接:https://leetcode.com/problems/combination-sum-ii/?tab=Description 给定数组,数组中的元素均为正数,target也是正数. ...
- LeetCode 39 Combination Sum(满足求和等于target的所有组合)
题目链接: https://leetcode.com/problems/combination-sum/?tab=Description Problem: 给定数组并且给定一个target,求出所 ...
- LeetCode总结 -- 树的求和篇
树的求和属于树的题目中比較常见的,由于能够有几种变体,灵活度比較高,也能够考察到对于树的数据结构和递归的理解. 一般来说这些题目就不用考虑非递归的解法了(尽管事实上道理是跟LeetCode总结 -- ...
- LeetCode:二进制求和【67】
LeetCode:二进制求和[67] 题目描述 给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11" ...
- LeetCode:范围求和||【598】
LeetCode:范围求和||[598] 题目描述 给定一个初始元素全部为 0,大小为 m*n 的矩阵 M 以及在 M 上的一系列更新操作. 操作用二维数组表示,其中的每个操作用一个含有两个正整数 a ...
- LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题.该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sor ...
随机推荐
- Linux信号-信号集&信号屏蔽字&捕捉信号【转】
转自:https://blog.csdn.net/Lycorisradiata__/article/details/80096203 一. 阻塞信号 1. 信号的常见其他概念 实际执行信号的处理 ...
- python3+selenium框架设计08-进一步实现POM
之前都是只有一个页面,一个用例.这次两个页面.两个测试用例.其实界面自动化测试最大的难点在于driver的传递,需要保持唯一性.另外就是断言的难点. 修改之前的BaiduPage,新增部分代码 fro ...
- Content-Type的几种常用数据编码格式
Content-Type,内容类型,一般是指网页中存在的Content-Type,ContentType属性指定请求和响应的HTTP内容类型.如果未指定 ContentType,默认为text/htm ...
- 【转】C++标准转换运算符reinterpret_cast
reinterpret_cast<new_type> (expression) reinterpret_cast运算符是用来处理无关类型之间的转换:它会产生一个新的值,这个值会有与原始参数 ...
- 【转】C++标准转换运算符const_cast
const_cast转换符是用来移除变量的const或volatile限定符. 对于const变量,我们不能修改它的值,这是这个限定符最直接的表现.但是我们就是想违背它的限定希望修改其内容怎么办呢? ...
- window.opener和window.open的使用
window.opener和window.open的使用 window.opener是指调用window.open方法的窗口.window.opener 返回的是创建当前窗口的那个窗口的引用,比如点击 ...
- Android获取本机号码及运营商
import android.content.Context; import android.telephony.TelephonyManager; import android.util.Log; ...
- springboot 文件上传大小配置
转自:https://blog.csdn.net/shi0299/article/details/69525848 springboot上传文件大小的配置有两种,一种是设置在配置文件里只有两行代码,一 ...
- O(big oh) (big omega) (big theta)
big oh big omega big theta more
- 【原创】大数据基础之Logstash(2)应用之mysql-kafka
应用一:mysql数据增量同步到kafka 1 准备mysql测试表 mysql> create table test_sync(id int not null auto_increment, ...