1. 程式人生 > >Decode string----字串解碼問題

Decode string----字串解碼問題

問題描述

Given an encoded string, return it’s decoded string.

The encoding rule is: k[encoded_string], where the encoded_string inside the square brackets is being repeated exactly k times. Note that k is guaranteed to be a positive integer.

You may assume that the input string is always valid; No extra white spaces, square brackets are well-formed, etc.

Furthermore, you may assume that the original data does not contain any digits and that digits are only for those repeat numbers, k. For example, there won’t be input like 3a or 2[4].

自己寫的程式:

#include<iostream>
#include<string>
#include<unordered_map>
using namespace std;
class
Solution { public: string decodeString(string s) { string s2; int k1=0; int k2=0; int k3=0; int i=0; int num; while (i < s.size()) { num = 0; int j = i; while (s[j]!='['&&j<s.size()) { num = num * 10
+ s[j] - '0'; j++; } k2 = j+1; while(s[j]!=']'&&j<s.size()) { j++; } k3 = j-1; while(num>0) { for (int i = k2;i <=k3;i++) s2.push_back(s[i]); num--; } i = j+1; } return s2; } }; void main() { Solution s; string a = "3[a]2[b]"; cout << s.decodeString(a) << endl; }

注:只針對沒有巢狀的字串序列,在寫程式的時候j=i++,和j=i+1有很大的不一樣

參考的程式
class Solution {
public:
string decodeString(string s, int& i) {
string res;

    while (i < s.length() && s[i] != ']') {
        if (!isdigit(s[i]))
            res += s[i++];
        else {
            int n = 0;
            while (i < s.length() && isdigit(s[i]))
                n = n * 10 + s[i++] - '0';

            i++; // '['
            string t = decodeString(s, i);
            i++; // ']'

            while (n-- > 0)
                res += t;
        }
    }

    return res;
}

string decodeString(string s) {
    int i = 0;
    return decodeString(s, i);
}

};

相關推薦

Decode string----字串解碼問題

問題描述 Given an encoded string, return it’s decoded string. The encoding rule is: k[encoded_string], where the encoded_string insid

394. Decode String解碼字串

Given an encoded string, return it’s decoded string. The encoding rule is: k[encoded_string], where the encoded_strin

394. Decode String 解碼icc字符串3[i2[c]]

extra ted ike 為什麽 tee put style 一句話 inside [抄題]: Given an encoded string, return it‘s decoded string. The encoding rule is: k[encoded_str

[LeetCode] Encode and Decode Strings 加碼解碼字串

Design an algorithm to encode a list of strings to a string. The encoded string is then sent over the network and is decoded back to the original list of

Decode Ways 把字串解碼成數字組合@LeetCode

做這道題還是有很多收穫的:1.為了避免麻煩,開陣列時可以開大一些,如int[] ways = new int[len+10];2. 這道題實際上是和計算要幾步走完臺階的那道題本質是一樣的。同樣要用動態規劃3. 在拆分時,之前我一直在糾結如何處理0的為題,特別是對於輸入是01,

Leetcode -- 394. Decode String

inside rul ted script form you 給定 rac 會有 Given an encoded string, return it‘s decoded string. The encoding rule is: k[encoded_string], w

String字串的認識

 1. 簡單認識String字串 通過原始碼我們可以看出String字串被final進行修飾,那麼也就說明了String字串不能被繼承和重寫。 String的特點:不能繼承和重寫 關於字串與記憶體關係: String str1 = new String("abc");

java String字串

概念題 1.String和StringBuffer有什麼區別。 String是不可變類,即一旦一個String類物件被建立以後,包含在這個物件中的字元序列是不可改變的 StringBuffer類是可變類 2."=="和equals()有什麼區別。  == 是對引用

【Java筆記】String字串拼接應該使用哪種方式?

我們所知道的字串String的拼接有: “+” 、 concat () 方式實現,或者使用StringBuilder、StringBuffer類實現。這幾種方式效能的從低到高進行排序,則順序為:“+”  <  concat ()  < S

String 字串擷取

private String DateTransfer(String validEndTime){ try { if(validEndTime.length() == 8){ validEndTime = validEnd

394. 字串解碼

給定一個經過編碼的字串,返回它解碼後的字串。 編碼規則為: k[encoded_string],表示其中方括號內部的 encoded_string 正好重複 k 次。注意 k 保證為正整數。 你可以認為輸入字串總是有效的;輸入字串中沒有額外的空格,且輸入的方括號總是符合格式要求的

關於String字串的比較

1. String s1 = new String("Hello"); String s2 = new String("Hello"); System.out.println(s1 == s2); false 原因: new String() 物件會在堆上開闢一個空間,s1

Retrofit2.0 處理返回的加密String字串

Retrofit2.0 處理返回的加密String字串 由於專案的需要,上傳與返回的資料都要加密,並用到的返回的報文頭欄位進行解密。若用Retrofit2.0 應該怎樣處理呢,下面我給大家解讀一下。 一、配置Retrofit2.0 與 RxJava 二、專案上加解密的處

JAVA擷取後String字串六位字元

public static void main(String[] args){ String cellphone="15585458544"; String pwd = cellphone.substring(cellphone.length() - 6);

java中String字串轉化成list<Integer>格式

最近開發中遇到問題,同事在傳給我ids時拼接為String字串格式,轉化成List,網上的轉化大致為: String[] strs = {"1","3","12","33"}; List<String> sList = Arrays.asList(strs); 而我要的是轉化後

演算法44----字串解碼【棧】

一、題目:字串解碼 給定一個經過編碼的字串,返回它解碼後的字串。 編碼規則為: k[encoded_string],表示其中方括號內部的 encoded_string 正好重複 k 次。注意 k 保證為正整數。 你可以認為輸入字串總是有效的;輸入字串中沒有額外的空格,且輸入的方括號總是符合格式要求的。

string字串拼接應使用哪種方式比較好

我們所知道的字串String的拼接有: “+” 、 concat () 方式實現,或者使用StringBuilder、StringBuffer類實現。這幾種方式效能的從低到高進行排序,則順序為:“+”  <  concat ()  < S

string字串的案例

 1. 案例:計算各種型別字元在字串中出現的次數 function fnCount(str){             //charAt  返回某個

佇列&棧//字串解碼

給定一個經過編碼的字串,返回它解碼後的字串。 編碼規則為: k[encoded_string],表示其中方括號內部的 encoded_string 正好重複 k 次。注意 k 保證為正整數。 你可以認為輸入字串總是有效的;

使用URLDecoder.decode解析字串含有"+","+"號這個特殊字元會自動轉換成空格

1.問題:       在使用url的時候,一般從網路或是伺服器得到的一個url地址一般都是經過encode編碼過(一般是使用URLEncoder.encode),或者是我們要給服務端傳遞url引數,直接使用的話,到服務端去解析的時候(應該服務端就是URLEnc