给定一个只包含小写字母的字符串s,牛牛想将这个字符串中的所有'a'字母全部移动到字符串的末尾,而且保证其它字符的相对顺序不变。其中字符串s的长度<=1e6。
public String change (String s) { // write code here int i = 0; int j = 0; char[] buffer = s.toCharArray(); for(;i<s.length();){ if(buffer[i] != 'a'){ char a = buffer[i]; buffer[i] = buffer[j]; buffer[j] = a; j++; } i++; } return new String(buffer); }
# # # @param s string字符串 # @return string字符串 # class Solution: def change(self , s ): # write code here num=0 l2=[] for n in s: if 'a' ==n: num+=1 else: l2.append(n) str2=''.join(l2)+'a'*num return str2
找不到写golang的
func change(s string) string { b := []byte(s) nb := []byte{} count := 0 for i := 0; i < len(b); i++ { if b[i] == 97 { count++ } else { nb = append(nb, b[i]) } } for j := 0; j < count; j++ { nb = append(nb, 97) } sb := string(nb) return sb }
string change(string s) { // write code here for(int i=0;i<s.size();i++) { if(s[i]=='a') continue; else { int temp=i;//记录下交换前的i while(i-1>=0 && s[i-1]=='a') //若前面为'a'那么一直交换 { s[i-1]=s[i]; s[i]='a'; i--; } i=temp; } } return s; }
# # # @param s string字符串 # @return string字符串 # class Solution: def change(self , s ): # write code here n = s.count('a') return s.replace('a','')+'a'*n
class Solution11 { public: /** * * @param s string字符串 * @return string字符串 */ string change(string s) { // write code here int count = 0; string str; int len = s.size(); for (int i = 0; i < s.size(); i++) { if (s.at(i) == 'a') { count++; } else { str += s.at(i); } } str.append(count, 'a'); return str; } };
class Solution: ''' 借助一个stack将'a'临时存入 ''' def change(self , s ): # write code here if not s: return [] stack, res = [], [] for c in s: if c == 'a': stack.append(c) else: res.append(c) return ''.join(res+stack) class Solution: ''' 统计'a'的数量 ''' def change(self , s ): # write code here if not s: return [] res = [] count = 0 for c in s: if c == 'a': count += 1 else: res.append(c) s = ''.join(res) + ''.join(['a']*count) # s = ''.join(res) # for _ in range(count): # s += 'a' #数据量很大时,不要用 + ,占用大量内存 return s
string change(string s) { // write code here int nums = 0, i = 0; nums = s.size(); string p(nums, ' '); --nums; for (char ch: s) { if (ch == 'a') p[nums--] = 'a'; else p[i++] = ch; } return p; }
import java.util.*; public class Solution { public String change (String s) { // write code here int len1=s.length(); s=s.replace("a",""); int len2=s.length(); while(len2<len1) { s+="a"; len2++; } return s; } }
class Solution: def change(self, s): ls = [*s] i, j, length = 0, 0, len(ls) for i in range(length): if ls[i] != 'a': ls[j] = ls[i] j += 1 while j < length: ls[j] = 'a' j += 1 return ''.join(ls)
class Solution { public: /** * * @param s string字符串 * @return string字符串 */ string change(string s) { // write code here int len=s.length(); int j=0; string res=""; for(int i=0;i<len;i++) { if(s[i]!='a') { s[j]=s[i]; j++; } } res=s.substr(0,j); for(int i=0;i<len-j;i++) { res+='a'; } return res; } };根据移除数字发散到字符,一个类型的题目,要多多注意发散思维
class Solution: def change(self , s ): # write code here ret1 = [] ret2 = [] for i in range(len(s)): if s[i] == 'a': ret1.append(s[i]) else: ret2.append(s[i]) res1 = "".join(ret2) res2 = "".join(ret1) res = res1 + res2 return res
import java.util.*; public class Solution { public static void main(String [] args){ Scanner scan = new Scanner(System.in); String s = scan.next(); System.out.print(change(s)); } /** * * @param s string字符串 * @return string字符串 */ public static String change (String s) { int len = s.length(); s = s.replace("a", ""); int num = len - s.length(); for(int i=0; i < num; i++){ s+="a"; } return s; } }提示为 :运行超时:您的程序未能在规定时间内运行结束,请检查是否循环有错或算法复杂度过大。