字符串问题之 字符串的统计字符串
问题1、给定一个字符串str, 返回str的统计字符串,
例如: “aaabbadddffc”的统计字符串为“a_3_b_2_a_1_d_3_f_2_c_1”
进阶题目:
给定一个统计字符串cstr, 在给定一个整数inde 返回代表的字符
问题2、例如: "a_1_b_100" index=50的字符是’b‘
问题1思路:
str为空,则统计字符串不存在
str不为空,首先生成String类型res,表示统计字符串,num表示数量(初始位置:str[0], nim=1)
不停的遍历 就是看看 str[i]跟str[i-1]的关系 等于num++ 不等于则 res=res+"_"+num+"_"+str[i](把当前的加入哈) 然后num=1 继续遍历~~~~~
写入num的条件是发现新字符时候,此时一定要注意!!! 当遍历结束时候!
package TT; public class Test3 { public static String getCountString(String str){ if(str==null || str.equals("")){ return ""; } char[] chs = str.toCharArray(); String res= String.valueOf(chs[0]); int num=1; for(int i =1; i<chs.length; i++){ if(chs[i]!=chs[i-1]){ res = concat(res, String.valueOf(num), String.valueOf(chs[i])); num=1; }else{ num++; } } return concat(res, String.valueOf(num),""); } public static String concat(String s1, String s2, String s3){ return s1+"_"+s2+(s3.equals("")?s3:"_"+s3); } public static void main(String[] args) { String str = "aaabbadddffc"; String s = getCountString(str); System.out.println(s); } }
结果:
可以快速手写:
public class t8 { public static String statistics(String str){ char[] chars = str.toCharArray(); int count = 1; String result =String.valueOf(chars[0]); for (int i=1; i< chars.length; i++){ if (chars[i] == chars[i-1]){ count++; }else { result = result+"-"+count; count = 1; result = result+"-"+chars[i]; } } //最后一个的判断 result = result + "-"+count; return result; } public static void main(String[] args){ String str = "aaabbadddffc"; String statisticsStr = statistics(str); System.out.println(statisticsStr); } }
问题2
解题过程
1 boolean stage 控制进入哪个状态 true代表字符阶段 false代表遇到连续字符统计阶段 初始时候 stage=true cur=0 num=0 sum=0
比较sum跟index的关系
上代码:
package TT; public class Test3 { public static char getCharAt(String cstr, int index){ if(cstr ==null || cstr.equals("")){ return 0; } char[] chs=cstr.toCharArray(); boolean stage = true; char cur = 0; int num = 0; int sum=0; for(int i =0; i!=chs.length; i++){ if(chs[i]=='_'){ stage =!stage; }else if(stage){ sum +=num; if(sum > index){ return cur; } num =0; cur = chs[i]; }else{ num = num*10+chs[i]-'0'; } } return sum+num>index?cur:0; } public static void main(String[] args){ String cstr ="a_1_b_100"; int index = 50; char a = getCharAt(cstr, index); System.out.println(a); } }
结果:
可以这么玩儿:
public class Test6 { public static String getCountString(String str) { String start = str.charAt(0) + " "; String res = ""; int count = 1; for (int i = 1; i < str.length(); i++) { int last = str.length() - 1; if (str.charAt(i) == str.charAt(i - 1) && i != last) { count++; } else if (i == last) { if (str.charAt(i) == str.charAt(i - 1)) { count++; res = res + str.charAt(i) + "-" + count; } else { res = res + str.charAt(i) + "-" + 1; } } else { res = res + start + "-" + count + "-"; start = str.charAt(i) + ""; count = 1; } } return res; } public static void main(String[] args) { String str = "aaabbadddffc"; System.out.println(getCountString(str)); } }
解决问题方式有很多的:
public class t9 { public static String getIndexStr(String str, int index) { int num = 0; int sum = 0; char[] chars = str.toCharArray(); String result = " "; for (int i = 0; i < chars.length; i++){ int now = chars[i] - '0'; if (now >= 0 && now <= 9){ num = num * 10 + now; }else { //不是数字的情况 if (index > sum){ sum= sum+num; } if (index <= sum){ result = getStr(chars, index); } } } //最后一个单独拿出来 if (index <= num){ result = getStr(chars, str.length() - 1); } return result; } public static String getStr(char[] chars, int indexNow){ int j = indexNow; while (j > 0 && (chars[j] != '_')){ j -- ; } return String.valueOf(chars[j-1]); } public static void main(String[] args) { String str = "a_1_b_100"; String indexStr = getIndexStr(str, 50); System.out.println(indexStr); } }