[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
也可以用栈实现,附上别人的代码
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; } } }
相关文章推荐
- android Google Map获取地理位置信息的方法
- 谷歌 Project Zero 团队宣布新政策,漏洞披露前将有完整的 90 天缓冲期
- Android使用Google Map浅谈
- 专家解读:开源软件项目是否会被限制出口?
- 专家解读:开源软件项目是否会被限制出口?
- Google排名算法改变 不在以外部链为主
- Google Adsense 支票托收信息收集 (1-30-2007)
- Google Adsense常用技巧总结
- Google推广的十二点技巧
- google优化圣经做网站必备第1/3页
- Google AdSense英文高价关键词排行列表
- Google 统计图表(Flash)小插件
- 2月份停止Google Adsense下线推介 将在中国地区停止投放
- 使用Google工具栏追踪 Blog 留言
- Google官方支持的NodeJS访问API,提供后台登录授权
- jquery repeater 模仿 Google 展开页面预览子视图
- 据说是Google首页的网页模块拖动代码
- jquery与google map api结合使用 控件,监听器
- php获取Google机器人访问足迹的方法