使用java语言,将字符串中连续重复出现的字母变成“出现次数“+字母的格式

天堂鸟有点卡 2015-09-17 07:05:54
今天笔试题遇到了。

将一个字符串例如 hello ,最后输出变成he2lo

例如wwwaacbbd 变成 3w2ac2bd的字符串输出

能力有限,回来弄了半天,还是做不出来
...全文
945 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪衣染尘 2019-08-10
  • 打赏
  • 举报
回复
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String string=sc.nextLine(); getNumFromString(string); } private static void getNumFromString(String string) { // TODO Auto-generated method stub StringBuilder sb= new StringBuilder(); char c= string.charAt(0); int count=0; for (int i = 1; i < string.length(); i++) { char s= string.charAt(i); if (s==c) { count++; c=s; } else if (count>=1) { sb.append(count); sb.append(c); c=s; count=0; }else{ sb.append(c); c=s; } } if(count>=1){ sb.append(count); sb.append(c); //最后字符也可能会重复所以多加一个 }else sb.append(c); System.out.println(sb.toString()); } }
  • 打赏
  • 举报
回复
引用 6 楼 a52740449 的回复:
[quote=引用 5 楼 doujinlong1 的回复:]
import java.util.Scanner;


public class Robot {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String string=sc.nextLine();
		getNumFromString(string);
	}

	private static void getNumFromString(String string) {
		// TODO Auto-generated method stub
		StringBuilder sb= new StringBuilder();
		char  c= string.charAt(0);
		int count=1;
		for (int i = 1; i < string.length(); i++) {
			char s= string.charAt(i);
			if (s==c) {
				count++;
			}
			else {
				
				if (count>1) {
				sb.append(count);
				sb.append(c);
				count=1;
				}else{
				sb.append(c);
				}
			}
			c=s;
		}
		sb.append(c);
		System.out.println(sb.toString());
		
	}
}
2楼的代码我没看,3楼的代码是正确的,我看了好一会才理解。 你的代码有个小问题,就是最后的字母如果是重复的话没有打印出现的次数,最后没有判断count是否>1, 你写的代码要好理解些。。 至少对于我是这样。 非常感谢,我自己再去写一遍。[/quote] 在最后一个append前面加一个判断就好了
  if (count>1) {
			sb.append(count);
		}
        sb.append(c);
        System.out.println(sb.toString());
确实是没考虑到那个问题
tony4geek 2015-09-18
  • 打赏
  • 举报
回复 1
正则替换
	String str = "wwwaacbbd";
		String s="";
		Matcher m=Pattern.compile("(\\w)\\1*").matcher(str);
		while(m.find()){
			s+=m.group().length()+m.group().subSequence(0, 1).toString();
 		} 
		System.out.println(s.replaceAll("1", ""));
--------3w2ac2bd

天堂鸟有点卡 2015-09-17
  • 打赏
  • 举报
回复
引用 5 楼 doujinlong1 的回复:
import java.util.Scanner;


public class Robot {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String string=sc.nextLine();
		getNumFromString(string);
	}

	private static void getNumFromString(String string) {
		// TODO Auto-generated method stub
		StringBuilder sb= new StringBuilder();
		char  c= string.charAt(0);
		int count=1;
		for (int i = 1; i < string.length(); i++) {
			char s= string.charAt(i);
			if (s==c) {
				count++;
			}
			else {
				
				if (count>1) {
				sb.append(count);
				sb.append(c);
				count=1;
				}else{
				sb.append(c);
				}
			}
			c=s;
		}
		sb.append(c);
		System.out.println(sb.toString());
		
	}
}
2楼的代码我没看,3楼的代码是正确的,我看了好一会才理解。 你的代码有个小问题,就是最后的字母如果是重复的话没有打印出现的次数,最后没有判断count是否>1, 你写的代码要好理解些。。 至少对于我是这样。 非常感谢,我自己再去写一遍。
  • 打赏
  • 举报
回复
import java.util.Scanner;


public class Robot {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String string=sc.nextLine();
		getNumFromString(string);
	}

	private static void getNumFromString(String string) {
		// TODO Auto-generated method stub
		StringBuilder sb= new StringBuilder();
		char  c= string.charAt(0);
		int count=1;
		for (int i = 1; i < string.length(); i++) {
			char s= string.charAt(i);
			if (s==c) {
				count++;
			}
			else {
				
				if (count>1) {
				sb.append(count);
				sb.append(c);
				count=1;
				}else{
				sb.append(c);
				}
			}
			c=s;
		}
		sb.append(c);
		System.out.println(sb.toString());
		
	}
}
  • 打赏
  • 举报
回复
我觉得楼上的答案不对,写了一个,你参考一下
oO临时工Oo 2015-09-17
  • 打赏
  • 举报
回复
上面的代码有点问题
public static void main(String[] args) {
		String str = "awww.tttbbb.commm";
		StringBuffer sb = new StringBuffer();
		
		char lastChar = 0;            //上次遇到的字符
		int lastCharCount = 0;        //上次遇到的字符 的数量
		for(int i = 0; i < str.length(); i ++){
			char c = str.charAt(i);
			
			boolean isFirst = i == 0;
			boolean isLast = i == str.length() - 1;
			boolean change = false;
			if(isFirst){
				//第一个字符
				lastChar = c;
			}else{
				//非第一个字条
				change =  c != lastChar;
			}
			
			if(change){
				//字符切换
				if(lastCharCount > 1){
					sb.append(lastCharCount);
				}
				sb.append(lastChar);
				
				lastChar = c;
				lastCharCount = 1;
				
			}else{
				lastCharCount ++ ;
			}
			
			if(isLast){
				//最后一个字符
				if(change){
					sb.append(c);
				}else{
					if(lastCharCount > 1){
						sb.append(lastCharCount);
					}
					sb.append(lastChar);
				}
			}
		}
		
		System.out.println(sb.toString());
		
	}
oO临时工Oo 2015-09-17
  • 打赏
  • 举报
回复

public static void main(String[] args) {
		String str = "www.tttbbb.com";
		StringBuffer sb = new StringBuffer();
		
		boolean isFirstChar = true;   //是否是第一个字符
		char lastChar = 0;            //上次遇到的字符
		int lastCharCount = 0;        //上次遇到的字符 的数量
		for(int i = 0; i < str.length(); i ++){
			char c = str.charAt(i);
			if(isFirstChar){
				//第一个字符
				lastChar = c;
				lastCharCount ++ ;
				isFirstChar = false;
			}else{
				//非第一个字条
				if( c == lastChar){
					//字符重复
					lastCharCount ++ ;
				}else{
					if(lastCharCount > 1){
						//上一个字符数量大于1
						sb.append(lastCharCount);
					}
					sb.append(lastChar);
					lastCharCount = 0;
					lastChar = c;
				}
			}
		}
		
		System.out.println(sb.toString());
		
	}
oO临时工Oo 2015-09-17
  • 打赏
  • 举报
回复
这是类似于字符串压缩的一个问题。 最简单的方法是遍历符串中的每个字符

67,550

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧