簡體   English   中英

計算字符串中字符的連續重復出現

[英]Count continuous repeated occurrence of characters from String

這是我的代碼。

public static void countContinuosOccurence() {
    String first = "ABBCDDDEFGGH";
    StringBuffer result = new StringBuffer();
    int count = 1;
    for (int i = 1; i < first.length(); i++) {
        if (first.charAt(i) == (first.charAt(i - 1))) {
            count++;
        } else {
            if (count > 1) {

                result.append(String.valueOf(count) + first.charAt(i - 1));
            } else {
                result.append(first.charAt(i - 1));
            }
            count = 1;
        }
    }
    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}

結果是:

First String is:ABBCDDDEFGGH
Result is:A2BC3DEF2G

它缺少最后一個字符嗎? 有人可以幫我解決這個問題嗎?

在之后for循環結束,你需要追加和字符(縣)結果的最后一次運行的特點

public static void countContinuosOccurence() {
    String first = "ABBCDDDEFGGH";
    StringBuffer result = new StringBuffer();
    int count = 1;
    int i;
    for (i = 1; i < first.length(); i++) {
        if (first.charAt(i) == (first.charAt(i - 1))) {
            count++;
        } else {
            if (count > 1) {    
                result.append(String.valueOf(count) + first.charAt(i - 1));
            } else {
                result.append(first.charAt(i - 1));
            }
            count = 1;
        }
    }

    // ADD THIS - to take care of the last run.
    if (count > 1) {    
        result.append(String.valueOf(count) + first.charAt(i - 1));
    } else {
        result.append(first.charAt(i - 1));
    }

    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}

不是最出色的代碼,而是最簡單的代碼:

final String in = "ABBCDDDEFGGH" + '\u0000';
final StringBuilder b = new StringBuilder();
char prev = in.charAt(0);
int rpt = 0;
for (int i = 1; i < in.length(); i++) {
  final char curr = in.charAt(i);
  if (curr == prev) rpt++;
  else {
    b.append(rpt == 0? prev : "" + (rpt + 1) + prev);
    rpt = 0; prev = curr;
  }
}
System.out.println(b);
public static void countContinuosOccurence() {

    String[] input = "ABBCDDDEFGGH".split("");
        String out = "";
        for (int i = 0; i < input.length; i++) {
            int repeatedCharCount = 1;
            String currentChr = input[i];
            if (!(i == input.length - 1)) {
                while (input[i].equals(input[i + 1])) {
                    repeatedCharCount++;
                    i++;
                }
            }
            out = out + repeatedCharCount + currentChr;
        }

        System.out.println(out);

}

還有一個隱藏的問題,就是如果您要終止一個序列且出現多次,那么您將不會編寫任何內容。

解決此問題和檢測到的問題的最簡單方法是在for塊后添加最終檢查

[...]
    }
    int l = first.length();
    if (count > 1) {
        result.append(String.valueOf(count) + first.charAt(l - 1));
        } else {
            result.append(first.charAt(l - 1));
        }
    }
    System.out.println("First String is:"+ first);
    System.out.println("Result is:" + result);
}
import java.util.*;
public class HelloWorld{
public static void main(String []args){
    System.out.println("Hello World");
    String first = "ABBCDDDEFGGHhhhhh456456456{{{67}}}";
StringBuffer result = new StringBuffer();
result.append(first);
System.out.println(result);
    Map<Character,Integer> map = new HashMap<Character,Integer>();
for(int i = 0; i < first.length(); i++) {
char c = first.charAt(i);
if (map.containsKey(c)) {
int cnt = map.get(c);
map.put(c, ++cnt);
} else {
map.put(c, 1);
}

}
Set set = map.entrySet(); 
// Get an iterator 
Iterator itr = set.iterator(); 
// Display elements 
while(itr.hasNext()) { 
Map.Entry me = (Map.Entry)itr.next(); 
System.out.print(me.getKey() + ": "); 
System.out.println(me.getValue()); 
} 
System.out.println("Hello World1");
 }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM