【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 ...
随机推荐
- python答题辅助
最近直播答题app很热门,由于之前看过跳一跳的python脚本(非常棒),于是也想写一个答题的脚本. https://github.com/huanmsf/cai 思路: 1.截图 2.文字识别,提取 ...
- Des加密解密算法java实现
package tech.fullink.eaglehorn.utils; import javax.crypto.Cipher; import javax.crypto.SecretKey; imp ...
- C++写文件
头文件 ofstream -- 向文件写内容 实现代码 #include <vector> #include <string> #include <fstream> ...
- requests库入门12-文件上传和下载
因为找不到可以演示上传接口,所以只能纯代码了 文件上传 上传文件是在请求中使用files参数,files需要指向一个dict,然后dict里面的键是接口中对应文件名的字段,而值就是打开这个文件读取到内 ...
- ffmpeg 版本升级到 4.0 增加 libaom 库 [AOMedia 的 AV1 视频编码格式]
win10 中交叉编译 libaom 时 注意事项 libaom 源代码 下载 git -c "http.proxy=ip:port" clone https://aomedia. ...
- golang esl api
通过ESL 调取FS的状态,比如show calls : 用golang eventsocket 实现 conn, err := eventsocket.Dial("192.168.5.3 ...
- Docker入门 - 006 Docker 多种数据库的安装
Docker 安装 MySQL 查找Docker Hub上的mysql镜像 root@VM_16_14_centos ~# docker search mysql INDEX NAME DESCRIP ...
- appium常用方法
1.输入中文 在capabilities中增加两项设置: capabilities.setCapability("unicodeKeyboard", "True" ...
- 用layui前端框架弹出form表单以及提交
第一步:引用两个文件 第二步:点击删除按钮弹出提示框 /*删除开始*/ $(".del").click(function () { var id = $(this).attr(&q ...
- input错误提示,点击提交,提示有未填项,屏幕滑到input未填项的位置
function errorInfo(parm) { //获取文本框值 var $val = parm.val(); if ($val==""||undefined||null){ ...