字符串的统计字符串(count and say类似题目)
来源:互联网 发布:zbrush mac keygen 编辑:程序博客网 时间:2024/05/22 10:58
题目一:
给定一个字符串str,返回其统计字符串,例如String s="aaabbadddffc";其统计字符串形式为”a_3_b_2_a_1_d_3_f_2_c_1“。
最后一组总是要额外添加,且在这里形式较之前的在最后少了一个”_“.
public String countAndSay(String s){if(s == null||s.length() == 0)return s;String result="";char pre=s.charAt(0);int count=1;for(int i=1;i<s.length();i++){if(s.charAt(i) == pre){count++;}else{result=result+pre+"_"+count+"_";pre=s.charAt(i);count=1;}}//最后一个总是加不上 result=result+pre+"_"+count; return result;}
题目二:
给定一个字符串的统计字符串,再给定一个整数,返回原始字符串上该整数上的字符。
统计字符串形式如”a_3_b_2_a_1_d_3_f_2_c_1“, 发现分割分为"_",利用split("_")函数划分后便容易处理。
public String function(String cstr,int k){String[] strs=cstr.split("_");//得到的矩阵为[a, 3, b, 2, a, 1, d, 3, f, 2, c, 1]int sum=0;for(int i=1;i<strs.length;i+=2)//index为奇数是的字符串代表个数。{sum+=Integer.valueOf(strs[i]);//通过Integer.valueOf()将字符串转化为整数if(sum > k){return strs[i-1];//返回所求字符}}return null;}
0 0
- 字符串的统计字符串(count and say类似题目)
- 【字符串】Count and Say
- 字符串---Count and Say
- leetcode---count-and-say---字符串
- LeetCode 38 Count and Say(字符串生成)
- [leetcode] 【字符串】 38. Count and Say
- 3.12—字符串—Count and Say
- Count and Say 数字统计
- LeetCode 38. Count and Say 字符串“阅读式”扩张
- Count and Say:循环记录字符串中数字个数
- 38.leetcode Count and Say(easy)[字符串处理 string与int的转换]
- leetcode_38. Count and Say 统计数字个数并说出来,组成新的字符串
- Count and Say 一道处理String的题目
- LeetCode | Count and Say(统计并输出)
- LeetCode题目9 Count and Say
- Easy-题目50:38. Count and Say
- (leetCode)Count and Say --- 统计读
- 【leetcode】第38题 Count and Say 题目+解析+代码
- codeforces 686D. Kay and Snowflake 树的重心应用
- python 点点滴滴
- Qt 5.6 编译加载ODBC
- 《linux学习》之root权限的使用及退出
- android软件盘弹出一系列问题
- 字符串的统计字符串(count and say类似题目)
- cf#336-B-Hamming Distance Sum 前缀和
- Java中Synchronized的用法
- ios app的在线调试,查看日志
- CodeForces 605E Intergalaxy Trips (概率)
- C#操作MSMQ向本地消息队列发送和接收消息
- Rotate Image
- Java中JDBC的PreparedStatement用法
- Andrew NG 《machine learning》week 7,class1 —Large Margin Classification