当前位置: 代码迷 >> 综合 >> 微软面试题字符串解码以及Leetcode394 字符串解码
  详细解决方案

微软面试题字符串解码以及Leetcode394 字符串解码

热度:74   发布时间:2024-02-22 02:38:47.0

第一题:
encode algo: abcde -> c, abde -> cb, ade -> cbd, ae -> cbda, e-> cbdae,相当于每次取剩余字符串的中间字符append
give encode string like cbdae , return raw data string like abcde.
给了deque的解法,问能不能不用dq,然后又换了个方法做、

abcde -> cbdae

规律是这样 先c放中间然后b放左边,d放右边,a放左边,e放右边,如果偶数长度是反过来的。

这题第一次见还是很难想。



第二题:. check 1point3acres for more.
compressed string, 2[abc] -> abcabc, 2[a2[b2[c]]] -> 2[a2[bcc]]-> 2[abccbcc] -> abcbccabcbcc
输入 2[a2[b2[c]]],输出 abcbccabcbcc,lc应该有原题

Leetcode 394题

class Solution {
public:string decodeString(string s) {int index = 0;return helper(s,index);}string helper(string s, int& i){string res;int num = 0;while(i<s.size()){if(s[i]>='0'&&s[i]<='9'){num = num*10+(s[i]-'0');}else if(s[i]=='['){string str = helper(s, ++i);for(int i=0;i<num;i++){res+=str;}num = 0;}else if(s[i]==']'){return res;}else{res+=s[i];}i++;}return res;}
};

 

 

  相关解决方案