标签:toc print ring 字符替换 字符 public lin blog system
给定一个字符串,请你将字符串重新编码,将连续的字符替换成“连续出现的个数+字符”。比如字符串AAAABCCDAA会被编码成4A1B2C1D2A。
import java.util.Arrays; import java.util.Scanner; public class Test { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.nextLine()+ "*"; char[] c = str.toCharArray(); int count = 1; //System.out.println(Arrays.toString(c)); for (int i = 1; i < c.length; i++) { if (c[i] == c[i-1]) { count++; } else { System.out.print(count+""+c[i-1]); count = 1; } } } }
标签:toc print ring 字符替换 字符 public lin blog system
原文地址:http://www.cnblogs.com/zzsaf/p/6675248.html