【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 ...
随机推荐
- LwIP Application Developers Manual11---Initializing lwIP
1.前言 2.Initialization for simple lwIP 查看doc/rawapi.txt来获得更多官方信息 #if NO_SYS /* Network interface vari ...
- LaTeX Error: Something's wrong--perhaps a missing \item
使用Latex 引用参考文献,.bib文件是个很好的助手,创建后 1.第一步点击Latex编译,可以获得*.aux文件.*.dvi文件.*.log文件以及*.gz文件: 2.第二步点击Bibtex编译 ...
- CFileFind
1.CFileFind类的声明文件保存在afx.h头文件中.2.该类的实现的功能:执行本地文件的查找(查找某个具体的文件,查找某类文件x*.x*,查找所有文件*.*)3.CFileFind类是CGop ...
- VS2017中用C#调试DLL
1.首先将DLL工程导入到包含C#应用程序工程的解决方案中 2.将DLL和C#工程都改为[Debug]模式 3.设置DLL工程属性,右键点击DLL工程,选择[属性],选择[常规],将[输出目录]改为C ...
- TechnoSoftware OPCDA client(1.2.1) Error Codes
OPCDA.NET Client Interface WrapperSummary of OPC Error Codes We have attempted to minimize the numbe ...
- web-font 个人学习小总结
个人觉得 web-font 分为两种: 第一种就是真正文本字体,客户端没有安装的字体通过远程加载字体来实现特殊字体提高用户体验: icodon.com : http://icodon.com/goo ...
- nginx https配置记录
一.证书生成: 要有两个文件,一个私钥,一个证书. 私钥:-----BEGIN PRIVATE KEY----- 开始 证书:-----BEGIN CERTIFICATE----- 开始 二.Ngin ...
- Spring initializr使用
Spring initializr 是Spring 官方提供的一个很好的工具,用来初始化一个Spring boot 的项目. 有两种方式可以使用Spring initializr来创建一个项目: ht ...
- 前端----css 选择器
css 为了修饰页面作用, 让页面好看 ⑴ css的引入方式1,行内样式body里面2,内接样式在html里面的 style 里面3,外接样式两种:①链接式: <link rel=" ...
- 20)django-session使用
一:目录 1)session原理 2)cookie与session对比 3)session配置 4)session使用 5)示例 二:session原理 Django的Session机制会向请求的浏览 ...