时间限制:1秒
空间限制:32768K
小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
你能帮帮小Q吗?
输入描述:
输入数据有多组,每组包含一个字符串s,且保证:1<=s.length<=1000.
输出描述:
对于每组数据,输出移位后的字符串。
输入例子1:
AkleBiCeilD
输出例子1:
kleieilABCD
思路:
- 从头到尾遍历,遇到大写字母后跟小写字母的情况,互换两者位置,这样每遍历一次,可将大写字母统一后移一位(除了已满足要求的大写字母);
- 按照每个字符串字母组合的特点进行多次循环遍历,直到满足题意。
package Problems;
import java.util.Scanner;
public class Move {
public int firstBig(String s) {
int len = s.length();
int index = 0;
for (int i = 0; i < len; i++) {
if (s.charAt(i) >= 'A' && s.charAt(i) <= 'Z') {
index = i;
break;
}
}
return index;
}
public String moveIt(String s) {
char[] charArray = s.toCharArray();
int count = s.length() - firstBig(s);
while (count > 0) {
count--;
for (int i = 0; i < charArray.length - 1; i++) {
if (charArray[i] >= 'A' && charArray[i] <= 'Z' && charArray[i + 1] > 'Z') {
char temp = charArray[i];
charArray[i] = charArray[i + 1];
charArray[i + 1] = temp;
}
}
}
String res = new String(charArray);
return res;
}
public static void main(String[] args){
Move move = new Move();
Scanner input = new Scanner(System.in);
String str = input.next();
System.out.println(move.moveIt(str));
}
}
几个测试用例
public class MoveTest {
Move move = new Move();
String result = "";
@Test
public void all_chars_are_small() {
result = move.moveIt("abcde");
assertEquals("abcde", result);
}
@Test
public void chars_with_one_big() {
result = move.moveIt("aBcde");
assertEquals("acdeB", result);
}
@Test
public void chars_with_four_bigs() {
result = move.moveIt("AkleBiCeilD");
assertEquals("kleieilABCD", result);
}
@Test
public void chars_with_many_bigs() {
result = move.moveIt("hxKLAGLLzPyTxsFsrUnnSKQBHdQQrOyaEYJ" +
"RgiJbHIDXFcQkFmIhPNKIBfHxXDBdKAvgZiBLVwnlxJAHmttsSJ" +
"kZhSmQneNVoKoIYZRjPqsrFFaaqZbyNyeRjVKVFrCGdfycidTqb" +
"yQcpAtdRGzzBAaKoqybWMOyhrCQdwcRwQQpQavTnAbjriVwxJOr" +
"TYJVGYSWzKYeNAGqBzkJLucabNYvyVFxAGKLfqHXNttaqZfncEd" +
"TroGMzZnDbvZBBaRbJvuYIvlWrKaaGrvtyxrsCUOqxdwCrmVEeD" +
"rLKZKFJVRmrLsmbmOGUJyfdZIrFhuSwJQGRTYMLxKQNMaCavatl" +
"QIRZmFQvyWgQTVENxUcPKQCaUQbjyfaNuwoNdTBNldgrtPUcQod" +
"qsuJOdDpUczJWCZaasDdEYJkvituMHrCmZQSlRjIefVisatIUtf" +
"xBeKnHPyvWUKzRliFsYWgeXogiEgXDbfxAybwFuqFyEvjfIHEPD" +
"PKqEiGUtZhdDIDBGKpvBFyqHeEEhAToAbqHEpIdIhIGBtWjGHiQ" +
"RctZxQQYkfFoWUbqZyIcjRPQBilHrnqNBzFmoRUYCSrGkawJCcO" +
"rMceegISpIpSGVjbngWVMTYtGoAlQFPFyOFAxndJZNfKDTwFIx" +
"isKTjyjchidXpYgLfoBOLriuIAHmAbQwoHBgbdUYBHlDQGZJAS" +
"sHszOEPthLVnYbNqWegmONexfdsTVYHgtDmlyugefOBsqmgNDB" +
"oxkkhVHfvrYooVOyxDJQJLjYSngksbTopoPJFsKQzHePLukXyYTYCeW");
assertEquals("hxzyxssrnndryagibckmhfxdvgiwnlxm" +
"ttskhmneoojqsraaqbyyejrdfycidqbycptdzzaoqybyhrdw" +
"cwpavnbjriwxrzeqzkucabvyxfqttaqfncdroznbvabvuvlra" +
"arvtyxrsqxdwrmermrsmbmyfdrhuwxaavatlmvygxcabjyfauw" +
"odldgrtcodqsudpczaasdkviturmljefisattfxenyvzlisgeog" +
"igbfxybwuqyvjfqithdpvyqehobqpdhtjictxkfobqycjilrnqzm" +
"orkawcrceegppjbngtolyxndfwxisjyjchidpgforiumbwogbdlss" +
"zthnbqegmexfdsgtmlyugefsqmgoxkkhfvrooyxjngksboposzeuk" +
"yeKLAGLLPTFUSKQBHQQOEYJRJHIDXFQFIPNKIBHXDBKAZBLVJAHSJ" +
"ZSQNVKIYZRPFFZNRVKVFCGTQARGBAKWMOCQRQQQTAVJOTYJVGYSWK" +
"YNAGBJLNYVFAGKLHXNZETGMZDZBBRJYIWKGCUOCVEDLKZKFJVRLOG" +
"UJZIFSJQGRTYMLKQNMCQIRZFQWQTVENUPKQCUQNNTBNPUQJODUJWC" +
"ZDEYJMHCZQSRIVIUBKHPWUKRFYWXEXDAFFEIHEPDPKEGUZDIDBGKB" +
"FHEEATAHEIIIGBWGHQRZQQYFWUZIRPQBHNBFRUYCSGJCOMISISGVW" +
"VMTYGAQFPFOFAJZNKDTFIKTXYLBOLIAHAQHBUYBHDQGZJASHOEPLV" +
"YNWONTVYHDOBNDBVHYVODJQJLYSTPJFKQHPLXYTYCW", result);
}
@Test
public void chars_with_one_big_in_first() {
result = move.moveIt("FIuNrJGoGqnPTnuMuUQCpSXwCWFdWljPPcAteMPbkxoCBiPcabpssTedsrfgycuiBuWXZTakaJqKEjxJNzUTIRbhgvFVKpwJjoYREJfBlLTgO ");
assertEquals("uroqnnuupwdljctebkxoicabpssedsrfgycuiuakaqjxzbhgvpwjoflgFINJGGPTMUQCSXCWFWPPAMPCBPTBWXZTJKEJNUTIRFVKJYREJBLTO ", result);
}
}
网友评论