华为第二题 AC C++

/*
   算法思想:1.如果num[i]<=num[i+1],如果count[num[i]]>1,则删除num[i],否则不删除。
             2.数字的最大值总是保留第一个出现的位置,其他则删除。
     4 2 3 2 4
   首先删除的非最大值:
     ..... 得到:4 3 2 4
   然后处理最大数字值,保留第一个出现的:
     ..... 得到:4 3 2
*******************************************************************************************************
   答案有问题。。请看评论。。测试用例少,所以AC了。有同学,直接set得到不同的数字,然后大到小输出60%。
   谢谢各位的指正,给出的case。
*/
#include<iostream>
#include<climits>
#include<cstring>
#include<string>
using namespace std;

int main(){
   string str;
   while(getline(cin,str)){
    if(str.length()<=1) {cout<<str<<endl; continue;}
   

    /*num_count 计数数组 */
    int num_count[10]={0};
        /* 最大值位置 */
    int max_val=INT_MIN;int max_pos=-1;
    for(int i=0;i<str.length();i++){
        int tmp=str[i]-'0';
        num_count[tmp]++;
        if(tmp>max_val) {max_val=tmp;max_pos=i;}
    }

     /*删除的位置,标志位*/
    int *to_delete=new int[str.length()];
    memset(to_delete,0,sizeof(int)*str.length());


     for(int i=1;i<str.length();i++){
        int tmp=str[i-1]-'0';
        if(str[i]>=str[i-1]&&num_count[tmp]>1){
            num_count[tmp]-=1;
            to_delete[i-1]=-1;
        }
     }
     
     string result;
     for(int i=0;i<str.length();i++){
        if(i==max_pos) result=result+str[i];
        if(to_delete[i]!=-1&&(str[i]-'0'!=max_val)) result=result+str[i];
        
     }
    cout<<result<<endl;
   }
   return 0;
}



全部评论
#include <iostream> #include <string> using namespace std; bool search(string s, char a) { for (int i = 0; i < s.size(); i++) if (s[i] == a) return true; return false; } int main() { string s; while (cin>>s) { for (char i = '9'; i >= '0'; i--) { int j = 0; bool b = false; while (1) { if (s[j] == i) { if (b) { s.erase(j,1); j--; } else { if (s[j] < s[j + 1] && search(s.substr(j+1),i)) { s.erase(j,1); j--; } else b = true; } } j++; if (j >= (s.length() - 1)) { if (b&&s[j]==i) s.erase(j,1); break; } } } cout << s << endl; } }
点赞 回复 分享
发布于 2017-08-24 14:18
贪心去做 #include <bits/stdc++.h> using namespace std; typedef long long ll; int main() { ll n, tmp; vector<int> cnt(10); vector<int> s; vector<int> vis(10); cin >> n; tmp = n; while(tmp > 0) { cnt[tmp%10]++; s.push_back(tmp%10); tmp /= 10; } reverse(s.begin(), s.end()); stack<int> st; for (int i = 0; i < s.size(); ++i) { //cout << s[i] << endl; cnt[s[i]]--; if (st.empty()) { st.push(s[i]); vis[s[i]] = 1; } else { while(!st.empty() && st.top() < s[i] && cnt[st.top()] && !vis[s[i]]) { vis[st.top()] = 0; st.pop(); } if (!vis[s[i]]) st.push(s[i]), vis[s[i]] = 1; } } ll ans = 0, T = 1; while (!st.empty()) { ll x = st.top();st.pop(); ans += x*T; T *= 10; } cout << ans << endl; return 0; }
点赞 回复 分享
发布于 2017-08-24 08:07
这题测试用例不全,楼主你用1232153试试,你的方法是32153,而正确应为3215
点赞 回复 分享
发布于 2017-08-24 00:30
。。。虽然华为不让我笔试。。不过你的程序好像根本不对吧。。。。 你输入432143214321试试。。。。 #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; int main() { string str; while (cin >> str) { int len = str.length(); vector<int>count(10,0); for (int i = 0; i < len; i++) { count[str[i] - '0']++; } for (int i = 0; i < str.length()-1; i++) { if (str[i] <= str[i + 1] && count[str[i] - '0'] > 1) { count[str[i] - '0']--; str.erase(i, 1); i--; } } for (int i = str.length() - 1; i >=0; i--) { if (count[str[i] - '0'] > 1) { count[str[i] - '0']--; str.erase(i, 1); } } cout << str << endl; } return 0; } 这是我的。。。仅供参考。。。
点赞 回复 分享
发布于 2017-08-24 00:27
423234.保留第一个不是423么,结果不应该是432么
点赞 回复 分享
发布于 2017-08-23 23:29

相关推荐

刚在隔壁看到字节都有11面的了,有这么离谱吗
迷茫的大四🐶:11面?超级耐面王,或者说历史面试次数得分王
一起聊字节
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
09-11 10:08
点赞 评论 收藏
分享
搜索部&nbsp;首先说下timeline8.18,投递8.19,约一面8.21,晚上一面call约二面8.22,上午二面下午oc周末等待(8.23,8.24)8.25,offer一年前,我还是懵懵懂懂,高考完的暑假,只会提前学学高数,未来的画像是什么?我或许无法预测。开学后,自学Python,接单,无数个客户的ddl,偷偷摸摸一个人找自习的地方,这一步步竟然为后来的我,搭建工程能力的基础。大一上,我也要感谢我的第一位老板,让我接触到了实习,师兄带着我一步步入门,看他们写的飞书文档。大一下,导师带我参与企业项目,这让我渐渐发现,应该去实践,增长见识,而非局限当下,盯着自己的小新pro。不久后,第一波投递开始,结果当然是约面极少。盯着简历上的文字和ssob,我开始思考,确实很多可以去提升。带着些许不甘心,继续沉淀,慢慢的约面也越来越多,有的时候两天7场,准备完就接着下一个日程。这一次,也许是刚好到位吧,比较match,面试答的流利,关关难关关过,成为度孝子展望未来,依然是重重挑战,果然只有收到offer的那一刻是开心的。愿在百度星海拆解的每一段代码,都能成为丈量宇宙的诗行;此志终赴星河,而今迈步重铸天阶。屏幕前的你们,在无数个向星海奔赴的日夜,一定一定,会在未来化作群星回响的征程——请永远相信此刻埋首耕耘的自己!!!
一天三顿半:???百度提前批发 offer了?不是统一和正式批排序完再发吗我靠
百度求职进展汇总
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务