[日常刷题]leetcode D32

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遍历 && 代码拓展性
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值