[日常刷题]leetcode D32

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

409. Longest Palindrome

Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.

This is case sensitive, for example "Aa"is not considered a palindrome here.

Note:
Assume the length of given string will not exceed 1,010.

Example:

Input:
"abccccdd"

Output:
7

Explanation:
One longest palindrome that can be built is "dccaccd", whose length is 7.

Solution in C++:

关键点:

  • 最长回文串构造规则

思路:

  • 最长回文串的构造规则是取所有字母的最大偶数个,然后再加上一个字母,只要有剩余奇数单词的就可以,在最后的结果上加1;这里我还是通过map去计数,也可以通过数组去计数。
int longestPalindrome(string s) {
        
        size_t size = s.size();
        if (size == 0)
            return 0;
        
        map<char,int> characters;
        for(auto ch : s){
            map<char,int>::iterator it = characters.find(ch);
            if (it != characters.end()){
                ++characters[ch];
            } else{
                characters[ch] = 1;
            }
        }
        
        int result = 0;
        int ones = 0;
        map<char,int>::iterator it = characters.begin();
        while(it != characters.end()){
            int tmp = it->second % 2;
            result += it->second - tmp;
            if (tmp == 1)
                ++ones;
            ++it;
        }
        
        if (ones >= 1)
            ++result;
        
        return result;
    }

412. Fizz Buzz

Write a program that outputs the string representation of numbers from 1 to n.

But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”.

Example:

n = 15,

Return:
[
    "1",
    "2",
    "Fizz",
    "4",
    "Buzz",
    "Fizz",
    "7",
    "8",
    "Fizz",
    "Buzz",
    "11",
    "Fizz",
    "13",
    "14",
    "FizzBuzz"
]

Solution in C++:

关键点:

思路:

  • 因为3的倍数输出"Fizz" 5的倍数输出"Buzz" 两个的倍数输出"FizzBuzz"所以顺序判断3、5倍数,添加字符串即可,如果两个都不是就转换当前数字为字符串即可。
  • 看到解析,里面的学习点主要是对代码的复用性的提升,采用map的结构去存储maps的方式来达到在条件修改的情况下,尽量少的修改代码的效果。

方法一:暴力

vector<string> fizzBuzz(int n) {
        vector<string> result;
        for(int i = 1; i <= n; ++i){
            string tmp = "";
    
            if (i % 3 == 0)
                tmp += "Fizz"; 
               
            if (i % 5 == 0)
                tmp += "Buzz";
                
            if (tmp == "")
                tmp = to_string(i);
            result.push_back(tmp);
        }
        
        return result;
    }

方法二:map存储maps

vector<string> fizzBuzz(int n) {
        vector<string> result;
        map<int,string> fizzbuzzDict = {
            {3,"Fizz"},
            {5,"Buzz"}
        };
        
        for(int i = 1; i <= n; ++i){
            string tmp;
            map<int,string>::iterator it = fizzbuzzDict.begin();
            while(it != fizzbuzzDict.end()){
                if (i % it->first == 0)
                    tmp += it->second;
                ++it;
            }
            if (tmp == "")
                tmp = to_string(i);
            result.push_back(tmp);
        }
        
        return result;
    }

小结

今天又有成就感又受挫。这两题A的还挺快的,差不多两题20min吧包括看解析。但是第三题做的时候就很慢,现在还没调试出来,越到后面脑壳越糊,所以决定明日再战。

知识点

  • map遍历 && 代码拓展性

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/83021177
今日推荐