https://leetcode.cn/problems/remove-vowels-from-a-string/description/
先用GO写了试试。一开始想用append直接加到string后面,但是发现不行。所以还是先新建一个字节数组[]byte,然后直接赋值,最后再string函数作个类型转换。
Go
func removeVowels(s string) string {
length:=len(s)
str:=make([]byte,length)
cur:=0
for i:=0;i<length;i++{
if s[i]!='a'&&s[i]!='e'&&s[i]!='i'&&s[i]!='o'&&s[i]!='u'{
str[cur]=s[i]
cur++
}
}
return string(str[:cur])
}想着再用C++写写试试,还是C++写起来顺手。用C的话也差不多,string改成char*即可。
C++
class Solution {
public:
string removeVowels(string s) {
string str;
for(int i=0;i<s.length();i++){
if(s[i]!='a'&&s[i]!='e'&&s[i]!='i'&&s[i]!='o'&&s[i]!='u'){
str+=s[i];
}
}
return str;
}
};