楼主: mameko
跳转到指定楼层
上一主题 下一主题
收起左侧

脸熟 新鲜面巾

🔗
zzgzzm 2017-8-5 02:39:04 | 只看该作者
全局:
mameko 发表于 2017-6-27 20:43
其实我才发现我的是简化版,不用输出所有删除以后合法串,输出一个就好了。用栈就能搞定...

Thanks for clarification. I think using a counter to maintain the count difference between '(' and ')' should be able to replace the stack. It's done in two passes (assuming the given string is only one char extra from valid string.)
  1. string RemoveInvalidParantheses(string s) {
  2.   int counter = 0;
  3.   for (auto i = s.begin(); i < s.end(); ++i)
  4.   {
  5.     if (*i == '(') ++counter;
  6.     else if (*i == ')' && --counter < 0)
  7.       return s.erase(i), s;
  8.   }

  9.   counter = 0;
  10.   for (auto i = s.end()-1; i >= s.begin(); --i)
  11.   {
  12.     if (*i == ')') ++counter;
  13.     else if (*i == '(' && --counter < 0)
  14.       return s.erase(i), s;
  15.   }
  16.   return s;
  17. }
复制代码
回复

使用道具 举报

🔗
zzgzzm 2017-8-5 04:46:20 | 只看该作者
全局:
f1371342385 发表于 2017-6-27 10:22
见过一个follow up,但是限制最大是两位,可以用map来存一下,解法还是一样的。但是如果你这个不知道多少位 ...

I think with unordered_map<int, char> code in general case and with maximum k digits in code, the dp equation dp[ i] will be

for (int len = 1; len <= k; ++len) {
   dp[ i] += dp[ i+len]*code.count(stoi(s.substr(i, len)));
}

and it needs k initial values to start loop.
回复

使用道具 举报

🔗
greencode 2018-3-24 07:49:02 | 只看该作者
全局:
Great,thanks
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表