128. Longest Consecutive Sequence

hashmap, int up = nums[i], int down, int max

注:访问过的要erase

152. Maximum Product Subarray

Maximum Subarray那题的变种。由于正负得负,负负得正的关系。以A[i]结尾的max product subarray同时取决于以A[i-1]结尾的max / min product subarray以及A[i]本身。因此,对每个i,需要记录min/max product两个状态

159. Longest Substring with At Most Two Distinct Characters

Longest Substring with At Most K Distinct Characters

 int lengthOfLongestSubstringKDistinct(string s, int k) {
// write your code here
int start = 0, cnt = 0;
int char_set[256] = {0};
int ans = 0;
for (int i = 0; i < s.size(); i++) {
if (char_set[s[i]]++ == 0) cnt++;
while (cnt > k) {
char_set[s[start]]--;
if (char_set[s[start++]] == 0) cnt--;
}
ans = max(i - start + 1, ans);
}
return ans;
}
187. Repeated DNA Sequences
解析:
http://blog.csdn.net/coderhuhy/article/details/43647731

构造unordered_set<string> repeated, 遍历输入的原串, 对s[i]到s[i+9]的序列构成的子串, 如未出现在repeated中, 则存入repeated;如出现在repeated中, 则说明该子串曾出现过, 符合题意要求, 将其存入答案vector<string> answer

Input: "AAAAAAAAAAAA"
Output: ["AAAAAAAAAA","AAAAAAAAAA"]
Expected: ["AAAAAAAAAA"]

-- 对于如何去重, 其一可以先收集所有答案, 再sort, unique去重, 当然这样很慢也很麻烦; 其二, 可以再构造一个unordered_set<int> check, 用于存储已经存入answer中的重复子串对应的hint值;

sort(ans.begin(), ans.end());
vector<string>::iterator end_unique = unique(ans.begin(), ans.end());
ans.erase(end_unique, ans.end());

209. Minimum Size Subarray Sum

这道题给定了我们一个数字,让我们求子数组之和大于等于给定值的最小长度,跟之前那道Maximum Subarray 最大子数组有些类似,并且题目中要求我们实现O(n)和O(nlgn)两种解法,那么我们先来看O(n)的解法,我们需要定义两个指针left和right,分别记录子数组的左右的边界位置,然后我们让right向右移,直到子数组和大于等于给定值或者right达到数组末尾,此时我们更新最短距离,并且将left像右移一位,然后再sum中减去移去的值,然后重复上面的步骤,直到right到达末尾,且left到达临界位置,即要么到达边界,要么再往右移动,和就会小于给定值。

/////不是很懂nlog(n)

下面我们再来看看O(nlgn)的解法,这个解法要用到二分查找法,思路是,我们建立一个比原数组长一位的sums数组,其中sums[i]表示nums数组中[0, i - 1]的和,然后我们对于sums中每一个值sums[i],用二分查找法找到子数组的右边界位置,使该子数组之和大于sums[i] + s,然后我们更新最短长度的距离即可。

 

subsequence/subsets/subarray/substring problems的更多相关文章

  1. Here is a 10-line template that can solve most 'substring' problems子字符串问题的模板

    转载自leetcode评论区:https://discuss.leetcode.com/topic/30941/here-is-a-10-line-template-that-can-solve-mo ...

  2. leetcode bugfree note

    463. Island Perimeterhttps://leetcode.com/problems/island-perimeter/就是逐一遍历所有的cell,用分离的cell总的的边数减去重叠的 ...

  3. [LeetCode] Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  4. [LeetCode] Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  5. [LeetCode] 730. Count Different Palindromic Subsequences 计数不同的回文子序列的个数

    Given a string S, find the number of different non-empty palindromic subsequences in S, and return t ...

  6. [LeetCode] 647. Palindromic Substrings 回文子字符串

    Given a string, your task is to count how many palindromic substrings in this string. The substrings ...

  7. D2. Kirk and a Binary String (hard version) D1 Kirk and a Binary String (easy version) Codeforces Round #581 (Div. 2) (实现,构造)

    D2. Kirk and a Binary String (hard version) time limit per test1 second memory limit per test256 meg ...

  8. 6专题总结-动态规划dynamic programming

    专题6--动态规划 1.动态规划基础知识 什么情况下可能是动态规划?满足下面三个条件之一:1. Maximum/Minimum -- 最大最小,最长,最短:写程序一般有max/min.2. Yes/N ...

  9. StringBuffer 的 各种方法

    StringBuffer 其实提供了很多有用的方法, 以前用的多是 append, 其实还有: append(double) delete(int, int) deleteCharAt(int) re ...

随机推荐

  1. keepalive学习

    keepalive学习之软件设计 软件架构如下图所示: Keepalived 完全使用标准的ANSI/ISO C写出. 该软件主要围绕一个中央I/O复用分发器而设计,这个I/O复用分发器提供网络实时功 ...

  2. PLAN: step one

    1. 熟悉Unix/Linux Shell和常见的命令行  (start : 7.9 - end: 7.12) 1.文件系统结构和基本操作 ls/chmod/chown/rm/find/ln/cat/ ...

  3. s​s​h​配​置​公​钥​和​私​钥​登​陆​S​e​c​u​r​e​C​R​T

    在用windows时管理linux服务器时,常会用到SecureCRT.Xshell以及开源的putty.在我工作环境大多都是采用密码认证的方式进行登录.今天对学习了些SecureCRT的密钥登录方式 ...

  4. General Structure of Quartz.NET and How To Implement It

    General Structure of Quartz.NET and How To Implement It   General Structure of Quartz.NET and How To ...

  5. 在MVC中添加异常增加日志

    MVC的结构非常棒,基本你能想到注入的地方都可以找到地方,譬如IActionFilter,IResultFilter,IAuthorizationFilter以及IExceptionFilter 以下 ...

  6. 线性回归,logistic回归和一般回归

    1 摘要 本报告是在学习斯坦福大学机器学习课程前四节加上配套的讲义后的总结与认识.前四节主要讲述了回归问题,回归属于有监督学习中的一种方法.该方法的核心思想是从连续型统计数据中得到数学模型,然后将该数 ...

  7. tomcat创建一个windows服务

    具体步骤如下: 1.把JDK解压到C:\Program Files\Java下,Tomcat解压到D:\tomcat下 2.配置环境变量 JAVA_HOME:C:\Program Files\Java ...

  8. C语言实现,队列可伸缩

    两个栈实现一个队列,C语言实现,队列可伸缩,容纳任意数目的元素. 一.思路:1.创建两个空栈A和B:2.A栈作为队列的入口,B栈作为队列的出口:3.入队列操作:即是入栈A:4.出队列操作:若栈B为空, ...

  9. jquery.post用法补充(type设置问题)

    jquery.post用法 http://blog.csdn.net/itmyhome1990/article/details/12578275 当使用ajax获取data数据的时候,直接data.f ...

  10. 【最新】让快捷方式 实现相对路径——非.bat方式实现

    快捷方式一般是使用绝对路径的,但并不是不能使用相对路径,系统为普通用户考虑一般默认使用绝对路径,这样的结果是:只要目标文件(不论是文件夹.程序.文档,也包括快捷方式)绝对位置不变,我们创建的快捷方式基 ...