其他分享
首页 > 其他分享> > [LC] 611. Compress String II

[LC] 611. Compress String II

作者:互联网

Given a string, replace adjacent, repeated characters with the character followed by the number of repeated occurrences.

Assumptions

Examples

public class Solution {
  public String compress(String input) {
    // Write your solution here
    char[] charArr = input.toCharArray();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < charArr.length; i++) {
      char cur = charArr[i];
      int count = 1;
      while (i + 1 < charArr.length && charArr[i + 1] == charArr[i]) {
        count += 1;
        i += 1;
      }
      sb.append(cur).append(count);
    }
    return sb.toString();
  }
}

 

标签:count,String,int,611,Compress,II,sb,charArr,string
来源: https://www.cnblogs.com/xuanlu/p/12340765.html