A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

本帖最后由 杜正冬 于 2012-11-25 07:53 编辑

public class Test1 {
public static void main(String[] args) {
  // 取出一个字符串中字母出现的次数。如:"abcdekka27qoq" a(2)b(1)k(2)...
  stringTime("abcaac");
}
static void stringTime(String s) {
  int[] count = new int[s.length()];
  for (int x = 0; x < s.length(); x++) {
   for (int y = x; y < s.length(); y++) {
    if (s.charAt(x) == s.charAt(y))
     System.out.println("x="+x+"---"+"y="+y);;
    count[x]++;
   }
   System.out.println(s.charAt(x) + "" + count[x]);
  }
}
}

未命名.jpg (209.66 KB, 下载次数: 31)

未命名.jpg

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

8 个回复

倒序浏览
有问题.这类的功能要实现你要考虑到2个因素:第一 抓取的方式 charAt?subString?index? 这得需要你自己测试以后才可以下定论 ;第二 不得有重复的录入 比如 aabb 重复了就成a(2)a(2)b(2)b(2). 嘛 差不多就这样.
回复 使用道具 举报
求解决00000
回复 使用道具 举报
我好像发现问题了,不知道能不能帮到你,如下面所示,把红色的两行代码调换一下顺序
public class Test1 {
public static void main(String[] args) {
  // 取出一个字符串中字母出现的次数。如:"abcdekka27qoq" a(2)b(1)k(2)...
  stringTime("abcaac");
}
static void stringTime(String s) {
  int[] count = new int[s.length()];
  for (int x = 0; x < s.length(); x++) {
   for (int y = x; y < s.length(); y++) {
    if (s.charAt(x) == s.charAt(y))
     System.out.println("x="+x+"---"+"y="+y);;    //这两句话调换一下顺序
     count[x]++;
   }
   System.out.println(s.charAt(x) + "" + count[x]);
  }
}
}

评分

参与人数 1技术分 +1 收起 理由
古银平 + 1 神马都是浮云

查看全部评分

回复 使用道具 举报
你的算法逻辑我看起来有问题,不过也或许是我没看明白你的真实意思
如果能当面交流的话很容易找出问题所在
帖子交流的话我只能揣摩你的意思后根据我自己的想法给出一个可行的逻辑算法,你可以参考一下
思路如图:
编码如下:
  1. package notrue.study.temp;

  2. public class Test1 {
  3. public static void main(String[] args) {
  4. // 取出一个字符串中字母出现的次数。如:"abcdekka27qoq" a(2)b(1)k(2)...
  5. stringTime2("abcaac");
  6. }

  7. static void stringTime2(String s) {
  8. char[] zimu = new char[s.length()];
  9. int[] cishu = new int[s.length()];
  10. int k = 0;

  11. zimu[0] = s.charAt(0);
  12. cishu[0] = 1;
  13. boolean find = false;

  14. for (int y = 1; y < s.length(); y++) {

  15. for (int x = 0; x < k + 1; x++) {

  16. if (s.charAt(y) == zimu[x]) {
  17. cishu[x] = cishu[x] + 1;
  18. find = true;
  19. }

  20. }
  21. if (find == false) {
  22. k++;
  23. zimu[k] = s.charAt(y);
  24. cishu[k] = 1;

  25. }

  26. }
  27. for (int x = 0; x < k+1; x++)
  28. if ((Character) zimu[x] != null)
  29. System.out.print(zimu[x] + "(" + cishu[x] + ") ");
  30. else
  31. break;
  32. }

  33. }
复制代码
运行结果


未命名.JPG (67.15 KB, 下载次数: 28)

逻辑思路

逻辑思路

未命名2.JPG (10.43 KB, 下载次数: 27)

未命名2.JPG
回复 使用道具 举报

以解决 谢谢

public class Test1 {
public static void main(String[] args) {
  // 取出一个字符串中字母出现的次数。如:"abcdekka27qoq" a(2)b(1)k(2)...
  stringTime("abcaaasdadc");
}
static void stringTime(String s) {

  
  String newstr = "";
  
  while(s.length()>0){
   char ch=s.charAt(0);
   int count1=1;
   newstr="";
   for(int j=1;j<s.length();j++){
    if(s.charAt(j)==ch){
     count1++;
    }else{
     newstr+=s.charAt(j);
    }
   }
   s=newstr;
   System.out.println(ch+"出现了----("+count1+")");
  }
}
}

未命11名.jpg (199.14 KB, 下载次数: 33)

未命11名.jpg
回复 使用道具 举报
获取一个子串在字符串中出现的次数。(面试题)
                 *                 "nbawernbatyunbaidfnbaghjnba" nba出现了几次。
                 *
                 * 思路:
                 *         1,需要对nba在整串中进行查找。
                 *  2,如果找到了,记录nba出现的位置,
                 *  3,再从这个位置+nba长度的位置开始继续查找剩下的字符串中是否还有nba。
                 *  4,通过计数器来记录每次查找的次数
                 *  
                 *  步骤:
                 *  1,先有计数器。
                 *  2,定义变量记录每次出现的位置。
                 *  3,可以通过String类中的indexOf方法来完成对子字符串的索引。
                 *  4,指定的子串有可能有很多,需要不断的查找(循环 ,条件,找不到了就结束。).
                 *  
                 */
Public static void main(String[] args){
                String s1 = "nbawernbatyunbaidfnbaghjnba";
                String key = "nba";
                int count = getSubCount(s1,key);
                System.out.println(key+",count="+count);
                }
/**
         * 获取一个子串在字符串中出现的次数。
         * @param str 给定的字符串。
         * @param key 要找的子串
         * @return 返回key出现在str中的次数。
         */
       
        public static  int getSubCount(String str,String key){
               
                //1,定义变量,一个是计数器,一个是记录位置。
                int count = 0;
                int index = 0;
               
                //2,调用indexOf方法获取key出现的位置。
                while((index = str.indexOf(key,index))!=-1){
                        index = index + key.length();
                        count++;
                }
                return count;
        }


希望对楼主有帮助
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马