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 ' ...
随机推荐
- macaca环境搭建(web 和 android)
一.安装配置JDK 1.1下载JDK地址http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.h ...
- Http相关
1.http请求 http请求分为三部分:请求行,请求头,请求正文 1. 请求行 请求方式 GET POST 请求资源路径 协议版本 GET与POST请求区别? get只能传递1kb以下数据,P ...
- linux服务器证书安装指引
下面提供了3类服务器证书安装方法的示例: 1. Apache 2.x 证书部署 1.1 获取证书 Apache文件夹内获得证书文件 1_root_bundle.crt,2_www.domain.com ...
- iOS开发之instancetype
instancetype和id使用方法类似,但他们还有不同点: (1)instancetype在类型表示上,跟id一样,可以表示任何对象类型 (2)instancetype只能用在返回值类型上,不能像 ...
- shell学习指南-阅读笔记
shell学习指南真不是刚开始学习shell应该看得书,虽然其中讲了简单的linux命令,shell语法等,但是每章也有些深入和生僻地方,我想如果我刚学shell看到这样的地方一定会头疼的要死.或许也 ...
- IE6 margin 双倍边距解决方案
一.什么是双边距Bug? 先来看图: 我们要让绿色盒模型在蓝色盒模型之内向左浮动,并且距蓝色盒模型左侧100像素.这个例子很常见,比如在网页布局中,侧边栏靠左侧内容栏浮动,并且要留出内容栏的宽度.要实 ...
- web之Respone
服务器处理请求的流程: 服务器每次收到请求时,都会为这个请求开辟一个新的线程. 服务器会把客户端的请求数据封装到request对象中,request就是请求数据的载体!(袋子) 服务器还会创建r ...
- SQL条件循环语句以及异常知识整理
create or replace procedure pr_test1 is begin > then dbms_output.put_line('条件成立'); elsif > the ...
- 将一个对象push到数组之中的几点问题
在项目开发中我们需要向意数组中添加对象:首先想到的是利用数组的api,----push demo: var ar = [1,2,3] var ar2 = [11,22,33] var obj = { ...
- android开发用无线网络进行Android开发中的调试
1.手机具有root权限 2.安装adbWireless1.5.4.apk (下面有下载地址) 3.敲入命令:adb connect 192.168.1.127 后面是手机的IP地址 打开eclip ...