[日常刷题]leetcode D38

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/83120254

482. License Key Formatting

You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.

Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.

Given a non-empty string S and a number K, format the string according to the rules described above.
Example 1:

Input: S = "5F3Z-2e-9-w", K = 4

Output: "5F3Z-2E9W"

Explanation: The string S has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.

Example 2:

Input: S = "2-5g-3-J", K = 2

Output: "2-5G-3J"

Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.

Note:

  1. The length of string S will not exceed 12,000, and K is a positive integer.
  2. String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
  3. String S is non-empty.

Solution in C++:

关键点:

思路:

  • 不知道为什么我开始写的代码跑的时候老是报MLE,没找到原因,暂时代码先贴下面mark一下。我的主要想法是,先去除S中的’-’,然后计算第一组所分的个数,然后循环即可。
  • 解析里面就是很简单的暴力,就是这里注意一下字符串的顺序以及多余’-'的问题即可。

正答 : 暴力

string licenseKeyFormatting(string S, int K) {
        string result;
        int count = 0;
        for(int i = S.size() - 1; i >= 0; --i){
            if (S[i] == '-')
                continue;
            
            if (count == K){
                result = '-' + result;
                count = 0;
            }
           
            char ch = S[i];
            if (isalpha(S[i]))
                ch = toupper(S[i]);
            result = ch + result;
            ++count;
            
        }
        
        if (result[0] == '-')
            result.erase(0, 1);
        
        return result;
    }

错答 待找错

string licenseKeyFormatting(string S, int K) {
        string result;
        S.erase(remove(S.begin(), S.end(), '-'), S.end());  
        
        for(auto &ch : S){
            if (isalpha(ch))
                ch = toupper(ch);
        }
        
        int num = S.size();
        
        if (num == 0)
            return result;
        
        int first = num % K;
        
        if (first != 0)
            result = S.substr(0, first);
        else
            result = S.substr(0, K);
        
        for(int i = result.size(); i < num; i += K){
            result = result + '-' + S.substr(i, K);
        }
        
        return result;
    }

485. Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array.

Example 1:

Input: [1,1,0,1,1,1]
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s.
    The maximum number of consecutive 1s is 3.

Note:

  • The input array will only contain 0and 1.
  • The length of input array is a positive integer and will not exceed 10,000

Solution in C++:

易错点:

  • 遍历完之后忘记再更新一下max

思路:

  • 就是单纯的遍历,然后数每个连续的1,碰到0后更新max。
int findMaxConsecutiveOnes(vector<int>& nums) {
        int max = 0;
        int count = 0;
        for(int i = 0; i < nums.size(); ++i){
            if (nums[i] == 1)
                ++count;
            else{
                if (count > max)
                    max = count;
                count = 0;
            }
        }
        
        if (count > max)
            max = count;
        
        return max;
    }

小结

今天做的题没什么难度,就是调试花了我不少时间,特别是第一题,现在还不知道原因,可能也是这几天太急躁了吧,不过刷题还是得坚持下去感觉,毕竟我这个人是这样的,要是一旦断了,有点很难再捡起来的感觉。
今天没什么知识点就不总结了。

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83120254