---恢复内容开始---

对于每次开机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的更多相关文章

  1. python最全学习资料:python基础进阶+人工智能+机器学习+神经网络(包括黑马程序员2017年12月python视频(百度云链接))

    首先用数据说话,看看资料大小,达到675G 承诺:真实资料.不加密,获取资料请加QQ:122317653 包含内容:1.python基础+进阶+应用项目实战 2.神经网络算法+python应用 3.人 ...

  2. 2017/11/22 Leetcode 日记

    2017/11/22 Leetcode 日记 136. Single Number Given an array of integers, every element appears twice ex ...

  3. 2017/11/21 Leetcode 日记

    2017/11/21 Leetcode 日记 496. Next Greater Element I You are given two arrays (without duplicates) num ...

  4. 2017/11/13 Leetcode 日记

    2017/11/13 Leetcode 日记 463. Island Perimeter You are given a map in form of a two-dimensional intege ...

  5. 2017/11/20 Leetcode 日记

    2017/11/14 Leetcode 日记 442. Find All Duplicates in an Array Given an array of integers, 1 ≤ a[i] ≤ n ...

  6. 2017/11/9 Leetcode 日记

    2017/11/9 Leetcode 日记 566. Reshape the Matrix In MATLAB, there is a very useful function called 'res ...

  7. 2017/11/7 Leetcode 日记

    2017/11/7 Leetcode 日记 669. Trim a Binary Search Tree Given a binary search tree and the lowest and h ...

  8. 2017/11/6 Leetcode 日记

    2017/11/6 Leetcode 日记 344. Reverse String Write a function that takes a string as input and returns ...

  9. 2017/11/5 Leetcode 日记

    2017/11/5 Leetcode 日记 476. Number Complement Given a positive integer, output its complement number. ...

随机推荐

  1. CSS元素水平垂直居中的方法

    1.  元素水平居中 1.1  设置父元素的属性 text-align: center; 说明:此属性只针对父元素的子元素为内联元素时有效,比如:img,input,select,button等(行内 ...

  2. 省市区县的sql语句——省

    /*SQLyog v10.2 MySQL - 5.5.48 : Database - 省市县****************************************************** ...

  3. Clustered Index Scan 与 Clustered Index Seek

    Clustered Index Scan 与 Clustered Index Seek 在利用 SQL Server 查询分析器的执行计划中,会有许多扫描方式,其中就有 Clustered Index ...

  4. jQuery顺序加载图片(终版)

    这一篇是对上一篇(jQuery顺序加载图片(初版)--http://www.cnblogs.com/newbie-cc/p/3707504.html)的改进. function loadImage(i ...

  5. MVC控制器返回值

    public ActionResult Index(string id)//主页 //参数string searchString 访问方式为index?searchString=xxxx .参数str ...

  6. MessageFormat.format()用法

    1.java.text.Format的继承结构如下   2.MessageFormat模式 FormatElement { ArgumentIndex }:是从0开始的入参位置索引 { Argumen ...

  7. PAT_A1140#Look-and-say Sequence

    Source: PAT A1140 Look-and-say Sequence (20 分) Description: Look-and-say sequence is a sequence of i ...

  8. Appium的ios配置

    automationName  text        XCUITest platformName             text          iOS platformVersion      ...

  9. 7.5 pragma 指令

    第1个pragma禁用了我们感兴趣的特别警告,第2个恢复了它.禁用尽可能小的一段代码的警告是一个良好的做法,以便你不会错过任何真正应该修复的错误.如果你想在单独一行上禁用或恢复多个警告,那么只需用逗号 ...

  10. 【*2000】【2018-2019 ICPC, NEERC, Southern Subregional Contest C 】Cloud Computing

    [链接] 我是链接,点我呀:) [题意] [题解] 我们可以很容易知道区间的每个位置有哪些安排可以用. 显然 我们优先用那些花费的钱比较少的租用cpu方案. 但一个方案可供租用的cpu有限. 我们可以 ...