您的位置:首页 > 编程语言 > Go语言

[Google电面] String decompression

2016-06-19 19:26 459 查看
Problem Description

    input              output

2[abc]3[a]c => abcabcabcaaac;     2[ab3[d]]2[cc] => abdddabdddcccc

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:                                      // using divide and conquer,  recursion
string decompression(string src) {      // 2[ab3[d]]2[cc] => abdddabdddcccc
if(src=="") return "";
if(src[0]>='a'&&src[0]<='z'||src[0]>='A'&&src[0]<='Z') {
for(int i=0;i<src.size();i++) {
if(src[i]>='0'&&src[i]<='9') {
return src.substr(0,i)+decompression(src.substr(i));
}
}
return src;
}
else {
int start=-1,end,ans=0;
for(int i=0;i<src.size();i++) {
if(src[i]=='[') {
ans++;
if(start==-1) {
start=i;
}
}
else if(src[i]==']') {
ans--;
if(ans==0) {
end=i;break;
}
}
}
string numStr=src.substr(0,start);
stringstream trans;
trans<<numStr;
int num;
trans>>num;
string res="";
string nex=decompression(src.substr(start+1,end-start-1));
for(int i=0;i<num;i++) {
res+=nex;
}
if(end==src.size()-1) return res;
else return res+decompression(src.substr(end+1));
}
}

char findKthChAfterDecompression(string src,int k) {    //follow up Question (presume k<=src.size())
if(src[0]>='a'&&src[0]<='z'||src[0]>='A'&&src[0]<='Z') {
for(int i=0;i<src.size();i++) {
if(src[i]>='0'&&src[i]<='9') {
if(i>=k) return src[k-1];
else return findKthChAfterDecompression(src.substr(i),k-i);
}
}
return src[k-1];
}
else {
int start=-1,end,ans=0;
for(int i=0;i<src.size();i++) {
if(src[i]=='[') {
ans++;
if(start==-1) {
start=i;
}
}
else if(src[i]==']') {
ans--;
if(ans==0) {
end=i;break;
}
}
}
string numStr=src.substr(0,start);
stringstream trans;
trans<<numStr;
int num;
trans>>num;
string res="";
string nex=decompression(src.substr(start+1,end-start-1));
int curlen=nex.size()*num;
if(curlen>=k) {
return nex[k%nex.size()];
}
else return findKthChAfterDecompression(src.substr(end+1),k-curlen);

}
}

};
int main()
{
string test="2[ab3[d]]2[cc]";
string test1="2[abc]3[a]c";
Solution t;
cout<<t.decompression(test1)<<endl;
cout<<t.findKthChAfterDecompression(test1,10)<<endl;
return 0;
}


也可以用栈实现,附上别人的代码

public class Solution {
public String decompression(String s) {
if (s == null || s.length() == 0) {
return s;
}
LinkedList<Character> stack = new LinkedList<Character>();
for(int i = 0; i < s.length(); i++) {
if (s.charAt(i) != ']') {
stack.push(s.charAt(i));
} else if (s.charAt(i) == ']') {
StringBuilder sb = new StringBuilder();
char curC = stack.pop();
while(cur != '[') {
sb.append(curC);
curC = stack.pop();
}
int num = getRepeatTimes(stack);
String str = sb.toString();
for(int i = 0; i < num; i++) {
for(int j = str.length() - 1; j >= 0; j--) {
stack.push(str.charAt(j));
}
}
}
}

StringBuilder ret = new StringBuilder();
while(!stack.isEmpty()) {
ret.append(stack.pop());
}
return ret.reverse().toString();
}

int getRepeatTimes(LinkedList<Character> stack) {
int ret = 0;
int base = 1;
while(stack.peek() >= '0' && stack.peek() <= '9') {
ret += Integer.parseInt(stack.pop()) * base;
base *= 10;
}
}
}


     
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Google 面经