算法基础-字符移位
来源:互联网 发布:centos vsftpd 编辑:程序博客网 时间:2024/06/04 19:34
腾讯2017暑期实习生编程题
小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
小Q最近遇到了一个难题:把一个字符串的大写字母放到字符串的后面,各个字符的相对位置不变,且不能申请额外的空间。
你能帮帮小Q吗?
输入描述:
输入数据有多组,每组包含一个字符串s,且保证:1<=s.length<=1000.
输出描述:
对于每组数据,输出移位后的字符串。
输入例子1:
AkleBiCeilD
输出例子1:
kleieilABCD
import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner input = new Scanner(System.in);while(input.hasNext()){String str = input.nextLine();System.out.println(cal(str));}}private static String cal(String str) {char[] c = str.toCharArray();int cnt = c.length;for (int i = 0; i < cnt; i++) {if(String.valueOf(c[i]).matches("[A-Z]")){char stmp = c[i];for (int j = i+1 ; j < c.length; j++) {c[j-1] = c[j];}c[c.length-1] = stmp;cnt--;i--;}}return String.valueOf(c);}}
还有一种方法是从某位大神那看到的。
public class Main{ public static void main(String[] args){ Scanner scan = new Scanner(System.in); while(scan.hasNext()){ String str = scan.nextLine(); System.out.println(getResult(str)); } } public static String getResult(String str){ return str.replaceAll("[A-Z]","")+str.replaceAll("[a-z]",""); }}
阅读全文
0 0
- 算法基础-字符移位
- 算法基础-字符移位
- 算法基础-字符移位
- 算法基础-字符移位
- 算法基础-字符移位
- 算法基础-字符移位
- 腾讯 算法基础-字符移位
- 算法基础-字符移位(逻辑)
- 腾讯:算法基础-字符移位
- 【PHP】算法基础-字符移位
- [编程题] 算法基础-字符移位
- [腾讯校招] 算法基础-字符移位
- [编程题] 算法基础-字符移位
- [编程题] 算法基础-字符移位
- [编程题] 算法基础-字符移位
- 【算法题】字符移位
- 算法基础-字符移位----腾讯2017暑期实习生编程题
- 腾讯2017实习生编程之算法基础-字符移位
- Python打包分发工具setuptools简介
- Intervals (差分约束)
- How Many Tables(HDU
- HDU 1671 Phone List(字典树)
- 友盟 分享
- 算法基础-字符移位
- LocationManager的使用
- Java中的static关键字解析
- mysql基本使用
- 如何解决斐波那契数列递归方法的性能问题
- 淘宝框架atlas集成
- 为什么你该开始学习编程了?
- 理解HTTPS
- Python之道(3)---Python生成器