牛客编程巅峰赛S1赛季第1场 - 青铜&白银局(重现赛) A 移动字母 字符串签到

链接:https://ac.nowcoder.com/acm/contest/6448/A
来源:牛客网

题目描述
给定一个只包含小写字母的字符串s,牛牛想将这个字符串中的所有’a’字母全部移动到字符串的末尾,而且保证其它字符的相对顺序不变。其中字符串s的长度<=1e6。

示例1
输入
复制

“abcavv”

输出
复制

“bcvvaa”

#define debug
#ifdef debug
#include 
#include "/home/majiao/mb.h"
#endif


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#define MAXN ((int)1e5+7)
#define ll long long int
#define QAQ (0)

using namespace std;

#define num(x) (x-'0')

#define show(x...)                             \
    do {                                       \
        cout << "\033[31;1m " << #x << " -> "; \
        err(x);                                \
    } while (0)

void err() { cout << "\033[39;0m" << endl; }
template<typename T, typename... A>
void err(T a, A... x) { cout << a << ' '; err(x...); }

class Solution {
public:
    
    string change(string s) {
#if 0
		string ret;
		int cnt = 0;
		for(int i=0, k=0; i<int(s.length()); i++) {
			if(s[i] == 'a') cnt ++;
			else ret.push_back(s[i]);
		}
		while(cnt--) ret.push_back('a');
		return ret;
#endif
		int i, k; //双指针, 遇到'a'时跳过交换
		for(i=0, k=0; i<int(s.length()); i++) {
			if(s[i] == 'a') continue ;
			s[k++] = s[i];
		}
		for( ; s[k]; k++) s[k] = 'a'; //最后别忘了补齐所有'a'
		return s;
	}
};

#ifdef debug
signed main() {
	freopen("test", "r", stdin);
	clock_t stime = clock();

	Solution s;
	cout << s.change("abcavv") << endl;






	clock_t etime = clock();
	printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
	return 0;
}
#endif




java代码


import java.io.*;
import java.util.*;
import java.math.*;

public class Solution {

	public String change (String str) {
		StringBuilder s = new StringBuilder(str);
		int i, k;
		for(i=0, k=0; i<str.length(); i++) {
			if(s.charAt(i) == 'a') continue ;
			s.setCharAt(k, s.charAt(i));
			k ++;
		}
		while(k < str.length()) s.setCharAt(k++, 'a');
		return s.toString();
	}

	public static void main(String[] args) throws Exception {

		Solution s = new Solution();
		System.out.println(s.change("abcavv"));
	}

	static final void printf(String str, Object... obj) {
		System.out.printf(str, obj);
	}

	static final void show(Object... obj) {
		for (int i = 0; i < obj.length; i++)
			System.out.println(obj[i]);
	}


}

class Read { //自定义快读 Read

	public BufferedReader reader;
	public StringTokenizer tokenizer;

	public Read(InputStream stream) {
		reader = new BufferedReader(new InputStreamReader(stream), 32768);
		tokenizer = null;
	}

	public String next() {
		while (tokenizer == null || !tokenizer.hasMoreTokens()) {
			try {
				tokenizer = new StringTokenizer(reader.readLine());
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		}
		return tokenizer.nextToken();
	}

	public String nextLine() {
		String str = null;
		try {
			str = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public long nextLong() {
		return Long.parseLong(next());
	}

	public Double nextDouble() {
		return Double.parseDouble(next());
	}

	public BigInteger nextBigInteger() {
		return new BigInteger(next());
	}

}

你可能感兴趣的:(字符串,签到,双指针)