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

对于每次开机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清除浮动_清除float浮——详解overflow:hidden 与clear:both属性

    最近刚好碰到这个问题,看完这个就明白了.写的很好,所以转载了! CSS清除浮动_清除float浮动 CSS清除浮动方法集合 一.浮动产生原因   -   TOP 一般浮动是什么情况呢?一般是一个盒子里 ...

  2. 【PL/SQL】匿名块、存储过程、函数、触发器

    名词解释 子程序:PL/SQL的过程和函数统称为子程序. 匿名块:以DECLARE或BEGIN开始,每次提交都被编译.匿名块因为没有名称,所以不能在数据库中存储并且不能直接从其他PL/SQL块中调用. ...

  3. linux 清空文件的几种方案

    之前要清理文件,都是简单粗暴的rm -rf log文件,最近,发现在某些环境下,是不能删除文件本省的,又必须要清理文件的内容信息,经过亲自实验,目测以下的几种方案是可行的,方案如下: 1.采用vi命令 ...

  4. SQL Server对数据进行修改

    SQL Server对数据进行修改,修改数据库中的数据. auto"> <tr style="background:red"> <td>编号 ...

  5. Linux 重要文件目录

    文件系统层次化标准(Filesystem Hierarchy Standard)[FHS] 树形结构 /boot 开机所需文件——内核开机菜单以及所需的配置文件等 /dev 以文件形式存放任何设备与接 ...

  6. python tips:dict的key顺序

    python3.6+版本中,dict的键值保持插入有序. t = list(range(10)) b = t[:] d = dict(zip(t, b)) print(list(d.items())) ...

  7. 使用JAVA写一个简单的日历

    JAVA写一个简单的日历import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateF ...

  8. js数组操作find查找特定值结合es6特性

    js数组操作find查找特定值结合es6特性

  9. Package和Activity

    Package Package 包.只是在我们的app中这个Package是唯一的,就像你身份证号码一样.在我们做app自动化时,我们就需要知道他的Package,我们知道了Package那么也就知道 ...

  10. BZOJ 2006 [NOI2010]超级钢琴 (堆+主席树)

    题面:BZOJ传送门 洛谷传送门 让你求前$K$大的子序列和,$n\leq 5*10^{5}$ 只想到了个$nlog^{2}n$的做法,似乎要被卡常就看题解了.. 好神奇的操作啊,我傻了 我们把序列和 ...