1374.生成每种字符都是奇数个的字符串
链接:1374.生成每种字符都是奇数个的字符串
难度:Easy
标签:字符串
简介:给你一个整数 n,请你返回一个含 n 个字符的字符串,其中每种字符在该字符串中都恰好出现 奇数次 。
题解 1 - cpp
- 编辑时间:2022-08-01
- 内存消耗:6MB
- 编程语言:cpp
- 解法介绍:直接判断奇偶。
class Solution {
public:
string generateTheString(int n) {
string ans = "";
if ((n & 1) == 0) {
ans += 'b';
n--;
}
for (int i = 0; i < n; i++) ans += 'a';
return ans;
}
};
题解 2 - rust
- 编辑时间:2022-08-01
- 内存消耗:2MB
- 编程语言:rust
- 解法介绍:直接判断奇偶。
impl Solution {
pub fn generate_the_string(n: i32) -> String {
let mut n = n;
let mut ans = String::new();
if n & 1 == 0 {
ans.push('b');
n -= 1;
}
ans.push_str(&"a".repeat(n as usize));
ans
}
}