Q1: 520. Detect Capital

Given a word, you need to judge whether the usage of capitals in it is right or not.

We define the usage of capitals in a word to be right when one of the following cases holds:

  1. All letters in this word are capitals, like "USA".
  2. All letters in this word are not capitals, like "leetcode".
  3. Only the first letter in this word is capital if it has more than one letter, like "Google".

Otherwise, we define that this word doesn't use capitals in a right way.

Solution one, it is clear to follow when you are reading the code(AC):
 class Solution {
public:
bool detectCapitalUse(string word) {
if(word.empty()){
return false;
}
if(word.size() == ){
return true;
}
char* cstr = new char[word.size() + ];
strcpy(cstr, word.c_str());
// first capital
if(cstr[] >= 'A' && cstr[] <= 'Z'){
// second is capital
if(cstr[] >= 'A' && cstr[] <= 'Z'){
for(int i= ; i < word.size(); i++){
if(cstr[i] < 'A' || cstr[i] > 'Z'){
return false;
}
}
return true;
} else {
// second is not capital
for(int i= ; i < word.size(); i++){
if(cstr[i] >= 'A' && cstr[i] <= 'Z'){
return false;
}
}
return true;
}
} else {
// first is not capital
for(int i= ; i < word.size(); i++){
if(cstr[i] >= 'A' && cstr[i] <= 'Z'){
return false;
}
}
return true;
}
}
};

Another solution with std function is as follow, I prefer this solution as it is clear to understand.(AC)

 class Solution {
public:
bool detectCapitalUse(string word) {
if(word.empty()){
return false;
}
if(word.size() == ){
return true;
} // first capital
if(isupper(word[])){
// second is capital
if(isupper(word[])){
for(string::size_type ix= ; ix < word.size(); ix++){
if(!isupper(word[ix])){
return false;
}
}
return true;
} else {
// second is not capital
for(string::size_type ix= ; ix < word.size(); ix++){
if(isupper(word[ix])){
return false;
}
}
return true;
}
} else {
// first is not capital
for(string::size_type ix= ; ix < word.size(); ix++){
if(isupper(word[ix])){
return false;
}
}
return true;
}
}
};

Q2: 525. Contiguous Array

Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and 1.

Example 1:

Input: [0,1]
Output: 2
Explanation: [0, 1] is the longest contiguous subarray with equal number of 0 and 1.

Example 2:

Input: [0,1,0]
Output: 2
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.

Note: The length of the given binary array will not exceed 50,000.

Solution one: Dynamic programming(NOT AC, Time Limits Exceed)

I use dynamic programming to update the total number of 1 and 0 before current element(including itself). And loop from start position to current element to update the subarray, the time complexity is O(n*n), it is not smart solution since it takes much space and time.

But it is just part of the problem, the key idea of this problem is to use hash table to find the previous index with the same difference between 1 and 0, and update the maximum length. I will give this as solution two.

 class Solution {
public:
int findMaxLength(vector<int>& nums) {
if(nums.size() < ){
return ;
}
int size = nums.size();
vector<int> zero_cnt(size, );
vector<int> one_cnt(size, );
vector<int> max_len(size, ); max_len[] = ;
if(nums[] == ){
zero_cnt[]++;
} else {
one_cnt[]++;
}
for(int i = ; i < nums.size(); i++){
// state update
if(nums[i] == ){
zero_cnt[i] = zero_cnt[i - ] + ;
one_cnt[i] = one_cnt[i - ];
} else {
zero_cnt[i] = zero_cnt[i - ];
one_cnt[i] = one_cnt[i - ] + ;
} //update max length for each element
if(zero_cnt[i] == one_cnt[i]){
max_len[i] = i + ;
} else {
for(int j = ; j < i; j++){
if((zero_cnt[i] - zero_cnt[j]) == (one_cnt[i] - one_cnt[j])){
max_len[i] = max(max_len[i], i - j);
}
}
}
}
int max_sub_len = ;
for(int i = ; i< max_len.size(); i++){
max_sub_len = max(max_len[i], max_sub_len);
}
return max_sub_len;
}
};

Solution two: Hash table and a sum variable(AC).

This is a smart solution, change 0 to -1, and keep a variable to record the sum of all element by current element.

if sum is 0

  • update the max length is i + 1;

else: look up into the hash table

  • if we already push current sum to hash table, max length update to max(max_len, i - has_table[i]).
  • The reason is we have the same sum, that means all the number between these two indexs is zero, they contain the equal number of 1 and 0.
  • else, put current sum to hash table, the key is sum, the value is index.
  • return max length
 class Solution {
public:
int findMaxLength(vector<int>& nums) {
unordered_map<int, int> diff;
int cur_sum = ;
int max_len = ;
for(int i = ; i < nums.size(); i++){
cur_sum += (nums[i] == ? - : );
if(cur_sum == ){
max_len = i + ;
} else {
if(diff.find(cur_sum) != diff.end()){
max_len = max(max_len, i - diff[cur_sum]);
} else {
diff[cur_sum] = i;
}
}
}
return max_len;
}
};

leetcode contest 20的更多相关文章

  1. 【LeetCode算法-20】Valid Parentheses

    LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...

  2. LeetCode Contest 166

    LeetCode Contest 166 第一次知道LeetCode 也有比赛. 很久没有打过这种线上的比赛,很激动. 直接写题解吧 第一题 很弱智 class Solution { public: ...

  3. LeetCode Weekly Contest 20

    1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...

  4. [Leetcode][Python]20: Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...

  5. 【一天一道LeetCode】#20. Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  6. 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题

    我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...

  7. LeetCode:20. Valid Parentheses(Easy)

    1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')',  ...

  8. C# 写 LeetCode easy #20 Valid Parentheses

    20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...

  9. LeetCode题解(20)--Valid Parentheses

    https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...

随机推荐

  1. (23)IO之打印流 PrintStream & Printwriter

    PrintStream PrintStream可以接受文件和其他字节输出流,所以打印流是对普通字节输出流的增强,其中定义了很多的重载的print()和println(),方便输出各种类型的数据. Pr ...

  2. session的使用

    一.什么是session? Session:在计算机中,尤其是在网络应用中,称为“会话控制”.Session 对象存储特定用户会话所需的属性及配置信息.这样,当用户在应用程序的 Web 页之间跳转时, ...

  3. 2017年要学习的JavaScript的顶级框架和主题

    JavaScript的流行促进了一个非常活跃的由相关技术,框架和库组成的生态圈的发展.整个生态圈的多样性和活跃性越来越强,这让许多人变得越来越困惑. 你应该了解些什么技术呢?   我们应该将时间花费在 ...

  4. div内部元素居中

    要让div内部元素垂直居中,则给div加上此css样式: .div-vertical-middle{  height:200px;  width:304px;  line-height:50px;  ...

  5. POPTEST老李推荐:互联网时代100本必读书,来自100位业界大咖推荐 2

    ➤NO.30<移动的力量>[推荐人]刘九如:电子工业出版社副社长兼总编辑邬贺铨:中国工程院院士.原副院长汪力成:华立集团董事局主席➤NO.31<智慧社会>[推荐人]段永朝:财讯 ...

  6. 基于 Spring MVC 的开源测试用例管理系统以及开发自测的实践

    早前公司领导提出让开发自测,测试么也做做开发.当然了,为了保证自测质量,测试用例仍需测试提供,所以为了提高开发自测的效率和质量,我们开发了捉虫记.捉虫记是一个完整的Spring MVC项目,现已开源, ...

  7. 转接IC整理汇总 转接芯片大全

    转接口IC大全,信号转换大全EDP输出信号NCS8801 LVDS转EDP.RGB转EDP 封装QFN56 最大分辨率2560*1600用于手机.平板.转接板.液晶驱动板.广告机.可视门铃等等控制器到 ...

  8. Maven工程webinfo下面的JSP页面无法加载.js、.css文件的解决方案

    --下面是我的工程路径 --我jsp的写法 -----启动工程,访问js文件的路径是这样的, href="http://localhost:8080/activiti/css/public. ...

  9. MapReduce处理流程

    MapReduce是Hadoop2.x的一个计算框架,利用分治的思想,将一个计算量很大的作业分给很多个任务,每个任务完成其中的一小部分,然后再将结果合并到一起.将任务分开处理的过程为map阶段,将每个 ...

  10. jQuery / zepto ajax 全局默认设置

    jQuery / zepto 的 $.ajax 方法需要配置很多选项, 有些是很常用的每个 ajax 请求都要用到的, 可以全局设置, 避免每次都写. 注意: 此处用的 jQuery 版本是 1.8. ...