【刷题-LeetCode】216. Combination Sum III
- Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers.
Note:
- All numbers will be positive integers.
- The solution set must not contain duplicate combinations.
Example 1:
Input: k = 3, n = 7
Output: [[1,2,4]]
Example 2:
Input: k = 3, n = 9
Output: [[1,2,6], [1,3,5], [2,3,4]]
解 定义函数f(l, r, n, target)
,表示在区间[l, r]之间和为target的n个数,则:
\]
当n=2时,直接采用双指针搜索
class Solution {
public:
vector<vector<int>> combinationSum3(int k, int n) {
vector<bool>nums(10, true);
return comb(1, 9, k, n);
}
vector<vector<int>> comb(int l, int r, int n, int target){
vector<vector<int>>ans;
if(n == 2){
int i = l, j = r;
while(i < j){
int tmp = i + j;
if(tmp == target){
ans.push_back({i,j});
i++;
j--;
}else if(tmp < target){
i++;
}else{
j--;
}
}
return ans;
}
for(int i = l; i <= r; ++i){
vector<vector<int>>tmp_ans = comb(i+1, r, n - 1, target - i);
if(tmp_ans.size() > 0){
for(auto &v : tmp_ans){
v.insert(v.begin(), i);
ans.push_back(v);
}
}
}
return ans;
}
};
【刷题-LeetCode】216. Combination Sum III的更多相关文章
- [LeetCode] 216. Combination Sum III 组合之和 III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Java for LeetCode 216 Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Leetcode 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- LeetCode 216. Combination Sum III (组合的和之三)
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- leetcode 39. Combination Sum 、40. Combination Sum II 、216. Combination Sum III
39. Combination Sum 依旧与subsets问题相似,每次选择这个数是否参加到求和中 因为是可以重复的,所以每次递归还是在i上,如果不能重复,就可以变成i+1 class Soluti ...
- 【LeetCode】216. Combination Sum III
Combination Sum III Find all possible combinations of k numbers that add up to a number n, given tha ...
- LC 216. Combination Sum III
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- 【leetcode刷题笔记】Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in ...
- 39. Combination Sum + 40. Combination Sum II + 216. Combination Sum III + 377. Combination Sum IV
▶ 给定一个数组 和一个目标值.从该数组中选出若干项(项数不定),使他们的和等于目标值. ▶ 36. 数组元素无重复 ● 代码,初版,19 ms .从底向上的动态规划,但是转移方程比较智障(将待求数分 ...
随机推荐
- socket网络编程基础模块
更多功能 sk = socket.socket(socket.AF_INET,socket.SOCK_STREAM,0) 参数一:地址簇 socket.AF_INET IPv4(默认) socket. ...
- 四、Uniapp+vue+腾讯IM+腾讯音视频开发仿微信的IM聊天APP,支持各类消息收发,音视频通话,附vue实现源码(已开源)-会话好友列表的实现
会话好友列表的实现 1.项目引言 2.腾讯云后台配置TXIM 3.配置项目并实现IM登录 4.会话好友列表的实现 5.聊天输入框的实现 6.聊天界面容器的实现 7.聊天消息项的实现 8.聊天输入框扩展 ...
- 使用proxy解决请求跨域问题
背景 在 react 项目里,前端请求接口时出现了跨域问题(类似图片中的提示) 这时最快捷的方法就是让后端同学设置请求允许跨域(如图配置响应头) 如果后端同学不配合,就需要靠我们自己来了! 实现 Re ...
- [COCI2018-2019#2] Sunčanje 题解
link 考虑简洁的 \(\text{cdq}\) 做法. 约定:第 \(i\) 个矩形左下角为 \((xl_i,yl_i)\),右上角为 \((xr_i,yr_i)\),所有坐标都已离散化. 如果 ...
- JAVA实现查询栏目、类别(菜单)返回树结构(递归)
其中Channel.java是栏目结构,ChannelDto是我自己封装的返回给前端的数据,可以根据自己的来 这个的逻辑就是双重循环遍历每个类别,然后判断如果当前类别是其他类别的父级,那么就把其他类别 ...
- IDEA控制台中文显示乱码处理
加入 -Dfile.encoding=UTF-8 以及
- 【LeetCode】1200. Minimum Absolute Difference 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcode ...
- 【LeetCode】883. Projection Area of 3D Shapes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 数学计算 日期 题目地址:https://leetc ...
- 《Head First设计模式》读书笔记
前言:本文是记录我在阅读<Head First设计模式>这本书时,做得相关笔记,相关示例代码地址:design-patterns.由于本书不是将设计原则和设计模式分开讲述的,而是在讲一个设 ...
- SuperPixel
目录 SLIC Superpixel algorithm 距离函数的选择 代码 Gonzalez R. C. and Woods R. E. Digital Image Processing (For ...