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. ...
随机推荐
- Spring logger 配置
1. logback-spring.xml <?xml version="1.0" encoding="UTF-8"?> <configura ...
- (转)Arcgis for Js之GeometryService实现测量距离和面积
http://blog.csdn.net/gisshixisheng/article/details/40540601 距离和面积的测量时GIS常见的功能,在本节,讲述的是通过GeometryServ ...
- RSA PKCS1 填充方式
1)RSA_PKCS1_PADDING 填充模式,最常用的模式 要求:输入 必须 比 RSA 钥模长(modulus) 短至少11个字节, 也就是 RSA_size(rsa) – 11 如果输入 ...
- Cache-Control官方文档
https://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-25#page-21 5.2. Cache-Control The "Cach ...
- wx小程序开发 1:小程序代码构成
官网学习地址:https://developers.weixin.qq.com/miniprogram/dev/quickstart/basic/introduction.html 1: 2:待续.. ...
- 【转】【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之RAC 特殊问题和实战经验(五)
原文地址:http://www.cnblogs.com/baiboy/p/orc5.html 阅读目录 目录 共享存储 时间一致性 互联网络(或者私有网络.心跳线) 固件.驱动.升级包的一致性 共 ...
- RecyclerView 悬浮/粘性头部效果3种方式
但是以上两种方式onDrawOver()方法实现逻辑对初次查看该段代码要花时间理解.下面代码逻辑(原理一样,同样参考大神代码)相对清晰,易理解 public class StickyDecoratio ...
- PAT_A1154#Vertex Coloring
Source: PAT A 1154 Vertex Coloring (25 分) Description: A proper vertex coloring is a labeling of the ...
- 域名和ip、端口的关系
背景:新建一个项目,属于RPC服务,调用时需要ip+端口. 在工单系统里走流程,强制填写域名.之前也操作过,感觉域名不重要.我本来需要填写ip+端口,你给整个域名,那我端口往哪写?(一直以为域名=ip ...
- mysql 7 种 join
一. select * from A inner join B on A.key = B.key 二. select * from A left join B on A.key = B.key 三. ...