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 ' ...
随机推荐
- ubuntu下python flask环境搭建
ubuntu下python flask环境搭建 1. 安装pip sudo apt-get install python-dev pyhton-pip 2. 安装virtualenv sudo apt ...
- 浩哥解析MyBatis源码(一)——执行流程
原创作品,可以转载,但是请标注出处地址: 一.MyBatis简介 MyBatis框架是一种轻量级的ORM框架,当下十分流行,配合Spring+Spring MVC组成SSM框架,能够胜任几乎所有的项目 ...
- 求一个int型整数的两种递减数之和(java)--2015华为机试题
题目描述: 给出一个整数(负数使用其绝对值),输出这个整数中的两种递减数(1.最大递减数:2.递减数中各位数之和最大的数)之和. 递减数:一个数字的递减数是指相邻的数位从大到小排列的数字,不包含相邻的 ...
- 开发Nagios监控passwd文件插件
第一步:开发脚本 [root@Admin libexec]# pwd /usr/local/nagios/libexec [root@Admin libexec]# cat check_passwd ...
- jmeter配置、安装
一. 工具描述 apache jmeter是100%的java桌面应用程序,它被设计用来加载被测试软件功能特性.度量被测试软件的性能.设计jmeter的初衷是测试web应用,后来又扩充了其它的功能.j ...
- stl1
#include<iostream> #include<map> #include<string> using namespace std; map<st ...
- Java面试步步走
一.Java 基础 1.Java 集合原理 2.Java 多线程.同步集合.并发集合.阻塞队列.线程池.各种锁等 3.JVM 垃圾回收机制.JVM 引用类型.JVM 内存模型 二.应用技术基础 1)数 ...
- Win10专业版下图片拖到PS无法打开的解决技巧
PS这个软件是用户最常用的软件之一,其强大的图形处理能力毋庸置疑.有用户表示在Win10专业版系统中使用PS发现图片不能直接拖动到PS中打开,这个问题本身不是特别大的问题,但这一小小的毛病会打破用户习 ...
- Cesium之球心坐标与本地坐标
1球心坐标(ECEF)与本地坐标(NEU) 假如你来到一个陌生城市,你很可能需要问路.通常会告诉你向北走100米,右转,向东走100米,理解起来很直观.你给儿子买了一个地球仪,你从北京(39,115) ...
- QQ_SingleTalkClient
package test_teacher;import java.io.*;import java.net.*;public class SingleTalkClient{ public sta ...