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. ...
随机推荐
- Gradle sync failed: Could not find method android() for arguments 错误的解决办法
这个问题本质上是Android-gradle的一个使用限制. 对应的英文文档android_tool文档 如果你的App包含了多个Android模块, 应该尽量避免给每个模块手动指定编译SDK版本. ...
- RadioButtonList绑定后台的数据。
在前台,放置一个 <td style="width: 650px;"><asp:RadioButtonList ID="RadioButtonList2 ...
- Hibernate_01_初体验
hibernate开发的基本步骤: 编写配置文档hibernate.cfg.xml: 编写实体类: 生成对应实体类的映射文件并添加到配置文档中: 调用hibernate API进行测试. Hibern ...
- 解决:惠普HP LaserJet Pro M126a MFP 驱动 安装失败,及其它同类打印机失败问题
注意:如果在 Windows XP 系统下安装出错,请先安装WindowsXP KB971276-v3补丁后再安装装驱动. 下载地址:http://www.dyjqd.com/soft/KB97127 ...
- Python之global
1 Global The global statement and its nonlocal cousin are the only things that are remotely like dec ...
- SLAM: Ubuntu14.04_Kylin安装ROS-Indigo
参考连接:ROS-Indigo版在Ubuntu上的安装第一步: 软件源配置 1. 增加下载源(增加ubuntu版的ros数据仓库,即下载源)(通用指令适合任何版本的ros) sudo sh -c 'e ...
- Django逻辑关系
title: Django学习笔记 subtitle: 1. Django逻辑关系 date: 2018-12-14 10:17:28 --- Django逻辑关系 本文档主要基于Django2.2官 ...
- python学习之小小爬虫
学习python一段时间了,写了一个图片的小小爬虫,分享下,不喜勿喷! #coding=utf-8 ''' Created on 2015-5-22 @author: 悦文 ''' import re ...
- es6-set-map数据结构
Set的用法 set的key一定是字符串 { let list=new Set(); list.add(5);//向set中增加值要用add() list.add(6); console.log('s ...
- PAT_A1137#Final Grading
Source: PAT A1137 Final Grading (25 分) Description: For a student taking the online course "Dat ...