leetcode contest 20
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:
- All letters in this word are capitals, like "USA".
- All letters in this word are not capitals, like "leetcode".
- 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.
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的更多相关文章
- 【LeetCode算法-20】Valid Parentheses
LeetCode第20题 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determin ...
- LeetCode Contest 166
LeetCode Contest 166 第一次知道LeetCode 也有比赛. 很久没有打过这种线上的比赛,很激动. 直接写题解吧 第一题 很弱智 class Solution { public: ...
- LeetCode Weekly Contest 20
1. 520. Detect Capital 题目描述的很清楚,直接写,注意:字符串长度为1的时候,大写和小写都是满足要求的,剩下的情况单独判断.还有:我感觉自己写的代码很丑,判断条件比较多,需要改进 ...
- [Leetcode][Python]20: Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 20: Valid Parentheseshttps://oj.leetcod ...
- 【一天一道LeetCode】#20. Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- 《LeetBook》leetcode题解(20):Valid Parentheses[E]——栈解决括号匹配问题
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- LeetCode:20. Valid Parentheses(Easy)
1. 原题链接 https://leetcode.com/problems/valid-parentheses/description/ 2. 题目要求 给定一个字符串s,s只包含'(', ')', ...
- C# 写 LeetCode easy #20 Valid Parentheses
20.Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']', ...
- LeetCode题解(20)--Valid Parentheses
https://leetcode.com/problems/valid-parentheses/ 原题: Given a string containing just the characters ' ...
随机推荐
- 手动的写一个structs
为了更好的学习框架的运行机制,这里开始学习框架之前,介绍一个简单的自定义的框架. 需求: 登录:id:aaa,pwd:888登录成功之后,跳转到,index.jsp页面并显示,欢迎你,aaa 注册,页 ...
- Spark2.1.0分布式集群安装
一.依赖文件安装 1.1 JDK 参见博文:http://www.cnblogs.com/liugh/p/6623530.html 1.2 Hadoop 参见博文:http://www.cnblogs ...
- React文档翻译系列(二)Hello World
这是React文档翻译系列的第二篇,前一篇介绍了如何安装react,本篇主要介绍react的知识体系,掌握了基本的知识体系,才能更好的学习React. Hello World 开始React最简单的方 ...
- 【转】Flash字体嵌入方法
原文链接:http://www.cnblogs.com/ddw1997/archive/2011/11/13/2247546.html 一.如果使用flash cs5.5 1.先新建一个字体fla文件 ...
- 6.Java集合总结系列:常见集合类的使用(List/Set/Map)
MARK 总结: Set.List 和 Map 可以看做集合的三大类. List集合是有序集合,集合中的元素可以重复,访问集合中的元素可以根据元素的索引来访问. Set集合是无序集合,集合中的元素不可 ...
- [编织消息框架][JAVA核心技术]动态代理应用5-javassist
基础部份: 修改class我们用到javassist,在pom.xml添加 <properties> <javassist.version>3.18.2-GA</java ...
- python作业设计:输入用户名密码,认证成功后显示欢迎信息,输错三次后锁定
作业需求: 1.输入用户名密码 2.认证成功后显示欢迎信息 3.输错三次后锁定实现思路: 1.判断用户是否在黑名单,如果在黑名单提示账号锁定. 2.判断用户是否存在,如果不存在提示账号不存在. 3.判 ...
- MySQL读写分离技术
1.简介 当今MySQL使用相当广泛,随着用户的增多以及数据量的增大,高并发随之而来.然而我们有很多办法可以缓解数据库的压力.分布式数据库.负载均衡.读写分离.增加缓存服务器等等.这里我们将采用读写分 ...
- Android M以上运行时权限(Google官方出品)
转载请注明出处:http://www.cnblogs.com/cnwutianhao/p/6690152.html 网上运行时权限的例子.Demo无计其数,但是和Google官方出品的比起来,都显得很 ...
- C++命名空间的解释 【转】
使用命名空间的目的是对标识符的名称进行本地化,以避免命名冲突.在C++中,变量.函数和类都是大量存在的.如果没有命名空间,这些变量.函数.类的名称将都存在于全局命名空间中,会导致很多冲突.比如,如果我 ...