求问:今晚完美世界编程第三题(口令那道)是用什么方法啊

用回溯做的,最后时间不够了,不知道对不对#完美世界#
全部评论
我用的栈。设置了一个全局变量cur=1。如果出现的是J,就压入cur++。如果出现了Z,就把栈中元素输出到vector里,然后根据这个Z后面有几个J,从小到大依次压cur++入栈(对应连续的J),最后再压一个cur++(对应当前的Z)。(出栈时候正好相反,会得到从高到低的结果,正好是递减的。) emmmm感觉没讲清楚,这是我的code。 vector<int> sort_number(string& s) {  if (s == "") return vector<int> {1};  int cur = 1;  stack<int> sta;  vector<int> res;  sta.push(cur++);  for (int i = 0; i < s.size(); i++) {   if (s[i] == 'J') {    sta.push(cur++);   }   else {    // 'Z'    while (!sta.empty()) {     res.push_back(sta.top());     sta.pop();    }    while (s[i + 1] == 'J') {     i++;     sta.push(cur++);    }    sta.push(cur++);   }  }  while (!sta.empty()) {   res.push_back(sta.top());   sta.pop();  }  return res; }
点赞 回复
分享
发布于 2020-03-17 21:17
第三题不是0-1背包吗
点赞 回复
分享
发布于 2020-03-17 21:18
饿了么
校招火热招聘中
官网直投
01背包  😕
点赞 回复
分享
发布于 2020-03-17 21:26

相关推荐

点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务