zoukankan      html  css  js  c++  java
  • 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:

    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):
     1 class Solution {
     2 public:
     3     bool detectCapitalUse(string word) {
     4         if(word.empty()){
     5             return false;
     6         }
     7         if(word.size() == 1){
     8             return true;
     9         }
    10         char* cstr = new char[word.size() + 1];
    11         strcpy(cstr, word.c_str());
    12         // first capital
    13         if(cstr[0] >= 'A' && cstr[0] <= 'Z'){
    14             // second is capital
    15             if(cstr[1] >= 'A' && cstr[1] <= 'Z'){
    16                 for(int i= 2; i < word.size(); i++){
    17                     if(cstr[i] < 'A' || cstr[i] > 'Z'){
    18                         return false;
    19                     }
    20                 }
    21                 return true;
    22             } else {
    23                 // second is not capital
    24                 for(int i= 2; i < word.size(); i++){
    25                     if(cstr[i] >= 'A' && cstr[i] <= 'Z'){
    26                         return false;
    27                     }
    28                 }
    29                 return true; 
    30             }
    31         } else {
    32             // first is not capital
    33             for(int i= 1; i < word.size(); i++){
    34                 if(cstr[i] >= 'A' && cstr[i] <= 'Z'){
    35                     return false;
    36                 }
    37             }
    38             return true; 
    39         }
    40     }
    41 };

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

     1 class Solution {
     2 public:
     3     bool detectCapitalUse(string word) {
     4         if(word.empty()){
     5             return false;
     6         }
     7         if(word.size() == 1){
     8             return true;
     9         }
    10         
    11         // first capital
    12         if(isupper(word[0])){
    13             // second is capital
    14             if(isupper(word[1])){
    15                 for(string::size_type ix= 2; ix < word.size(); ix++){
    16                     if(!isupper(word[ix])){
    17                         return false;
    18                     }
    19                 }
    20                 return true;
    21             } else {
    22                 // second is not capital
    23                 for(string::size_type ix= 2; ix < word.size(); ix++){
    24                     if(isupper(word[ix])){
    25                         return false;
    26                     }
    27                 }
    28                 return true; 
    29             }
    30         } else {
    31             // first is not capital
    32             for(string::size_type ix= 1; ix < word.size(); ix++){
    33                 if(isupper(word[ix])){
    34                     return false;
    35                 }
    36             }
    37             return true; 
    38         }
    39     }
    40 };

    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. 

     1 class Solution {
     2 public:
     3     int findMaxLength(vector<int>& nums) {
     4         if(nums.size() < 2){
     5             return 0;
     6         }
     7         int size = nums.size();
     8         vector<int> zero_cnt(size, 0);
     9         vector<int> one_cnt(size, 0);
    10         vector<int> max_len(size, 0);
    11         
    12         max_len[0] = 0;
    13         if(nums[0] == 0){
    14             zero_cnt[0]++;
    15         } else {
    16             one_cnt[0]++;
    17         }
    18         for(int i = 1; i < nums.size(); i++){
    19             // state update
    20             if(nums[i] == 0){
    21                 zero_cnt[i] = zero_cnt[i - 1] + 1;
    22                 one_cnt[i] = one_cnt[i - 1];
    23             } else {
    24                 zero_cnt[i] = zero_cnt[i - 1];
    25                 one_cnt[i] = one_cnt[i - 1] + 1;
    26             }
    27             
    28             //update max length for each element
    29             if(zero_cnt[i] == one_cnt[i]){
    30                 max_len[i] = i + 1;
    31             } else {
    32                 for(int j = 0; j < i; j++){
    33                     if((zero_cnt[i] - zero_cnt[j]) == (one_cnt[i] - one_cnt[j])){
    34                          max_len[i] = max(max_len[i], i - j);
    35                     }
    36                 }
    37             }
    38         }
    39         int max_sub_len = 0;
    40         for(int i = 0; i< max_len.size(); i++){
    41             max_sub_len = max(max_len[i], max_sub_len);
    42         }
    43         return max_sub_len;
    44     }
    45 };

    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
     1 class Solution {
     2 public:
     3     int findMaxLength(vector<int>& nums) {
     4         unordered_map<int, int> diff;
     5         int cur_sum = 0;
     6         int max_len = 0;
     7         for(int i = 0; i < nums.size(); i++){
     8             cur_sum += (nums[i] == 0 ? -1 : 1);
     9             if(cur_sum == 0){
    10                 max_len = i + 1;
    11             } else {
    12                 if(diff.find(cur_sum) != diff.end()){
    13                     max_len = max(max_len, i - diff[cur_sum]);
    14                 } else {
    15                     diff[cur_sum] = i;
    16                 }
    17             }
    18         }
    19         return max_len;
    20     }
    21 };
  • 相关阅读:
    免费报表工具 积木报表(JiMuReport)的安装
    jeecgboot积木报表(jimuReport)SQL Server切换
    Machine Learning目录
    Pytorch05_torch安装(GPU版)
    Pytorch04_RNN结构
    Pytorch03_张量变化
    Pytorch02_GPU加速
    Pytorch01_通用结构
    怎么将本地文件上传到远程git仓库
    SpringCloud-微服务架构编码构建
  • 原文地址:https://www.cnblogs.com/wdw828/p/6418021.html
Copyright © 2011-2022 走看看