[Algo] 175. Decompress String II
Given a string in compressed form, decompress it to the original string. The adjacent repeated characters in the original string are compressed to have the character followed by the number of repeated occurrences.
Assumptions
- The string is not null 
- The characters used in the original string are guaranteed to be ‘a’ - ‘z’ 
- There are no adjacent repeated characters with length > 9 
Examples
- “a1c0b2c4” → “abbcccc” 
public class Solution {
  public String decompress(String input) {
    // Write your solution here
    if (input.length() == 0) {
      return input;
    }
    StringBuilder sb = new StringBuilder();
    int i = 0;
    char[] charArr = input.toCharArray();
    char prevChar = input.charAt(0);
    while (i < charArr.length) {
      char cur = charArr[i];
      if (Character.isLetter(cur)) {
        prevChar = cur;
        i += 1;
      } else if (Character.isDigit(cur)) {
        int num = cur - '0';
        if (num == 0) {
          i += 1;
        } else {
          while (i + 1 < charArr.length && Character.isDigit(charArr[i + 1])) {
            num = 10 * num + (charArr[i + 1] - '0');
            i += 1;
          }
          for (int j = 0; j < num; j++) {
            sb.append(prevChar);
          }
          i += 1;
        }
      }
    }
    return sb.toString();
  }
}
public class Solution {
  public String decompress(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 num = charArr[i] - '0';
      for (int j = 0; j < num; j++) {
        sb.append(cur);
      }
    }
    return sb.toString();
  }
}
[Algo] 175. Decompress String II的更多相关文章
- [Algo] 611. Compress String II
		Given a string, replace adjacent, repeated characters with the character followed by the number of r ... 
- [LeetCode] 344 Reverse String && 541 Reverse String II
		原题地址: 344 Reverse String: https://leetcode.com/problems/reverse-string/description/ 541 Reverse Stri ... 
- leetcode 344. Reverse String 、541. Reverse String II  、796. Rotate String
		344. Reverse String 最基础的旋转字符串 class Solution { public: void reverseString(vector<char>& s) ... 
- Reverse Words in a String I & Reverse Words in a String II
		Reverse Words in a String I Given an input string, reverse the string word by word. For example,Give ... 
- leadcode 541. Reverse String II
		package leadcode; /** * 541. Reverse String II * Easy * 199 * 575 * * * Given a string and an intege ... 
- LeetCode 541. 反转字符串 II(Reverse String II)
		541. 反转字符串 II 541. Reverse String II 
- 【leetcode_easy】541. Reverse String II
		problem 541. Reverse String II 题意: 给定一个字符串,每隔k个字符翻转这k个字符,剩余的小于k个则全部翻转,否则还是只翻转剩余的前k个字符. solution1: cl ... 
- Compress and decompress string
		You are given a string with lower case letters only. Compress it by putting the count of the letter ... 
- 186. Reverse Words in a String II
		题目: Given an input string, reverse the string word by word. A word is defined as a sequence of non-s ... 
随机推荐
- Windows 10任务调度器曝出新零日漏洞
			新浪科技讯,北京时间 5 月 23 日早间消息,据美国科技媒体 BleepingComputer 报道,在微软每月安全更新周期刚刚过去一周后,漏洞开发者 SandboxEscaper 悄悄发布了 Wi ... 
- 【LeetCode】电话号码的字母组合
			[问题]给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合. 给出数字到字母的映射如下(与电话按键相同).注意 1 不对应任何字母. 示例: 输入:" 输出:["ad ... 
- C++基础--引用的一点补充
			这一篇是对引用的一点补充,内容基本上是来自<C++ primer plus>一书第八章的内容. 前面一篇介绍了引用的一点特点,这里补充一个,将引用用于类对象的时候,有一个体现继承的特征,就 ... 
- 目标检测算法的总结(R-CNN、Fast R-CNN、Faster R-CNN、YOLO、SSD、FNP、ALEXnet、RetianNet、VGG Net-16)
			目标检测解决的是计算机视觉任务的基本问题:即What objects are where?图像中有什么目标,在哪里?这意味着,我们不仅要用算法判断图片中是不是要检测的目标, 还要在图片中标记出它的位置 ... 
- bzoj 4300绝世好题
			呵呵呵呵 #include<bits/stdc++.h> #define INF 0x7fffffff #define LL long long #define N 100005 usin ... 
- 面试题(6)之 leetcode-001
			1. 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这 ... 
- vue移动端点击一个元素缩小,松手的时候元素恢复正常
			active伪类解决 HTML代码 <div class='box'> </div> CSS代码 .box { width: 100px; height: 100px; bac ... 
- mysql经典查询语句-笔记
			笔记来源公开课,谢谢! 1.创建student和score表 CREATE TABLE student ( id INT(10) NOT NULL UNIQUE PRIMARY KEY , name ... 
- zabbix监控tcp/nginx/memcache连接数自定义监控shell
			#!/bin/bashtcp_status_fun(){ TCP_STAT=$1 #netstat -n | awk '/^tcp/ {++state[$NF]} END {for(key in st ... 
- C#调用C++系列二:传结构体
			这一篇记录下C#调用C++的结构体的方式来使用OpenCV的数据格式,这里会有两种方式,第一种是C#传一个结构体和图像的路径给C++,然后C++将图像加载进来,再把传进来的结构体填满即可,第二种是C# ... 
