2017-3-12 leetcode 167 209 216
---恢复内容开始---
对于每次开机avast喊出的“已经检测到危害”实在忍无可忍了(它只能检测到不能根除很气。。)于是重装了系统,回到了win10感觉不赖。
================================================================================
leetcode167Two Sum II
leetcode209Minimum Size Subarray Sum
leetcode216Combination Sum III
================================================================================
169讲的是
给你n个数字(非降序,可能重复),和一个数字target,在这n个数字中一定存在且只存在一组数字,相加等于target,输出他们的下标+1
我的思路
这道题是leetcode1的升级版,但是感觉难度是下降了啊。。。。和1的解法类似,只是不需要排序了,直接看代码吧。。
class Solution {
public:
vector<int> twoSum(vector<int>& numbers, int target) {
int n=numbers.size();
vector<int> &mynum=numbers;
int myend=n-,mybegin=;
vector<int> aim;
while(){
while(mynum[mybegin]+mynum[myend]>target)
myend--;
while(mynum[mybegin]+mynum[myend]<target)
mybegin++;
if(mynum[mybegin]+mynum[myend]==target){
aim.push_back(mybegin+);
aim.push_back(myend+);
break;
}
}
return aim;
}
};
169
==================================================================================
209讲的是
给你n个正整数,和一个数字s,问你数组中满足条件的最短连续子序列的长度是多少?条件是子序列累加和大于等于s。
我的思路
两个指针扫,前面的负责加,后面的负责减,判断。O(n)的。
class Solution {
public:
int minSubArrayLen(int s, vector<int>& nums) {
int n=nums.size(),aim=n,eptr,sum=,sptr=-;
if(n==)return ;
for(eptr=;eptr<n&&sum<s;eptr++){
sum+=nums[eptr];
}
while(sum-nums[sptr+]>=s){
sum-=nums[++sptr];
}
aim=sum>=s?(eptr-)-sptr:;
for(;eptr<n;eptr++){
sum+=nums[eptr];
while(sum-nums[sptr+]>=s){
sum-=nums[++sptr];
}
aim=min(aim,eptr-sptr);
}
return aim;
}
};
209
===================================================================================
216讲的是
给你两个数字n,k表示你需要用n个不同数字的和使其等于k,数字只能选1--9,输出所有的情况。
我的思路
深搜,枚举所有情况就行了。。。
class Solution {
public:
vector<int> aim;
vector<vector<int> > ans;
void dfs(int s,int f,int sum,int n,int target){
if(sum>target)return;
if(f==n+&&sum==target){
this->ans.push_back(aim);
return;
}
if(f==n+)return;
for(int i=s;i<;i++){
this->aim.push_back(i);
dfs(i+,f+,sum+i,n,target);
this->aim.pop_back();
}
}
vector<vector<int>> combinationSum3(int k, int n) {
dfs(,,,k,n);
return this->ans;
}
};
216
2017-3-12 leetcode 167 209 216的更多相关文章
- python最全学习资料:python基础进阶+人工智能+机器学习+神经网络(包括黑马程序员2017年12月python视频(百度云链接))
首先用数据说话,看看资料大小,达到675G 承诺:真实资料.不加密,获取资料请加QQ:122317653 包含内容:1.python基础+进阶+应用项目实战 2.神经网络算法+python应用 3.人 ...
- 2017/11/22 Leetcode 日记
2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...
- 2017/11/21 Leetcode 日记
2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...
- 2017/11/13 Leetcode 日记
2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...
- 2017/11/20 Leetcode 日记
2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...
- 2017/11/9 Leetcode 日记
2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...
- 2017/11/7 Leetcode 日记
2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...
- 2017/11/6 Leetcode 日记
2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...
- 2017/11/5 Leetcode 日记
2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...
随机推荐
- Entity Framework -- 添加,删除,修改,关系依附,关系摘除,验证操作
数据库模型 这个基本上是浓缩 Jerry Tom博客的内容,作为参考http://www.cnblogs.com/mbailing/archive/2012/07/31/2616779.html 说明 ...
- Activity生命周期(待整理)
1. 定义 有一些方法共同定义生命周期,如下图示:(图片来自于官网文档) 2. onStart()——在Activity即将对用户可见之前调用 (1)Activity启动动画.二维动画在onStart ...
- Android fragment的切换(解决REPLACE的低效)
在项目中切换Fragment,一直都是用replace()方法来替换Fragment.但是这样做有一个问题,每次切换的时候Fragment都会重新实列化,重新加载一次数据,这样做会非常消耗性能用用户的 ...
- 使用Visio—UML画类图
在一个VS工程中,由于类的个数较多,而参数描述不是特别清晰.若此工程的生命周期较长,则有必要对工程进行完整分析,给出完整的文档.需要画出类图,并对每个成员进行详细描述. 一.画出类图 在VIsio中, ...
- eclipse快捷键:
打开快捷键提示: ctrl + shift + L; 自动补全代码: Alt + /; 快速修复: ctrl + 1; 导包: ctrl + shift + o; 格式化代码: ctrl + shif ...
- 【sqli-labs】 less10 GET - Blind - Time based. - Double quotes (基于时间的双引号盲注)
这个和less9一样,单引号改完双引号就行了 http://localhost/sqli/Less-10/?id=1" and sleep(5)%23 5s后页面完成刷新 http://lo ...
- js 验证文件格式和大小
<script> $('#btnSearch').click(function(){ // alert("000");// fileElem = document.ge ...
- js消息框
<script> function del(obj, id) { layer.confirm('是否要删除信息!', { btn: ['确定', '取消'] }, function (in ...
- 2019-04-16 sql tran and try catch :
begin try begin tran tran_addresource -- 标记事务的开始 delete rp insert into Cube.ResourcePool(ResourceTyp ...
- 《奋斗吧!菜鸟》 第八次作业:Alpha冲刺 Scrum meeting 4
项目 内容 这个作业属于哪个课程 任课教师链接 作业要求 https://www.cnblogs.com/nwnu-daizh/p/11012922.html 团队名称 奋斗吧!菜鸟 作业学习目标 A ...