LeetCode Algorithm 1374. 生成每种字符都是奇数个的字符串

题目链接:1374. 生成每种字符都是奇数个的字符串

Ideas

算法:分类讨论
数据结构:无
思路:直接看代码,非常好懂。

Code

C++

class Solution {
    
    
public:
    string generateTheString(int n) {
    
    
        string res;
        if (n % 2 == 0) {
    
    
            int num = n - 1;
            while (num--) {
    
    
                res += 'a';
            }
            res += 'b';
        } else {
    
    
            while (n--) {
    
    
                res += 'a';
            }
        }
        return res;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_43336281/article/details/126107713