• 笔试强训Day3


    学了一坤时Linux,赶紧来俩道题目放松放松。

    T1:在字符串中找出连续最长的数字串

     链接:在字符串中找出连续最长的数字串__牛客网

    输入一个字符串,返回其最长的数字子串,以及其长度。若有多个最长的数字子串,则将它们全部输出(按原字符串的相对位置)

    本题含有多组样例输入。

    数据范围:字符串长度 1≤n≤200, 保证每组输入都至少含有一个数字

    这题复刻了一道经典dp【力扣53.最大子数组和】,下面是dp的代码

    1. #include
    2. #include
    3. #include
    4. using namespace std;
    5. string s;
    6. int main()
    7. {
    8. while(cin>>s){
    9. int ans=0;
    10. int n=s.size();
    11. int cnt=0;
    12. string temp;
    13. string res;
    14. vector<int>dp(n);
    15. if(s[0]>='0'&&s[0]<='9') {
    16. dp[0]=1;
    17. temp+=s[0];
    18. }
    19. for(int i=1;i
    20. if(s[i]>='0'&&s[i]<='9'){
    21. dp[i]=dp[i-1]+1;
    22. temp+=s[i];
    23. }
    24. else{
    25. dp[i]=0;
    26. temp="";
    27. }
    28. if(dp[i]>ans){
    29. res=temp;
    30. }
    31. else if(dp[i]==ans){
    32. res+=temp;
    33. }
    34. ans=max(ans,dp[i]);
    35. }
    36. cout<","<
    37. }
    38. return 0;
    39. }

    其实dp数组可以用一个变量代替,代码会更简洁。

    1. #include
    2. #include
    3. using namespace std;
    4. string s;
    5. int main()
    6. {
    7. while(cin>>s){
    8. int ans=0;
    9. int n=s.size();
    10. int cnt=0;
    11. string temp;
    12. string res;
    13. for(int i=0;i
    14. if(s[i]>='0'&&s[i]<='9'){
    15. cnt++;
    16. temp+=s[i];
    17. }
    18. else{
    19. cnt=0;
    20. temp="";
    21. }
    22. if(cnt>ans){
    23. res=temp;
    24. }
    25. else if(cnt==ans){
    26. res+=temp;
    27. }
    28. ans=max(ans,cnt);
    29. }
    30. cout<","<
    31. }
    32. return 0;
    33. }

     

    T2:数组中出现次数超过一半的数字

    链接:数组中出现次数超过一半的数字__牛客网

    给一个长度为 n 的数组,数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字。

    例如输入一个长度为9的数组[1,2,3,2,2,2,5,4,2]。由于数字2在数组中出现了5次,超过数组长度的一半,因此输出2。

    数据范围:n≤50000,数组中元素的值 0≤val≤100000

    要求:空间复杂度:O(1),时间复杂度 O(n)

    emmm,开始看道这题,容易想到map一遍,但这题的空间复杂度要求是O(1)。

    想了用位运算,不过那些是跟奇偶性有关。

    如何数组中存在众数,那众数的数量一定大于数组长度的一半。

    我们可以用一种消去的思想:比较相邻的俩个数,如果不相等就消去最坏的情况下,每次都消去一个众数和一个非众数,如果众数存在,那最后留下的一定就是众数

    1. class Solution {
    2. public:
    3. /**
    4. * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
    5. *
    6. *
    7. * @param numbers int整型vector
    8. * @return int整型
    9. */
    10. int MoreThanHalfNum_Solution(vector<int>& numbers) {
    11. // write code here
    12. int cnt=0;
    13. int ans=0;
    14. int n=numbers.size();
    15. for(auto x:numbers){
    16. if(!cnt){
    17. cnt=1;
    18. ans=x;
    19. }
    20. else{
    21. if(ans==x)cnt++;
    22. else cnt--;
    23. }
    24. }
    25. cnt=0;
    26. for(auto x:numbers){
    27. if(x==ans)cnt++;
    28. }
    29. if(cnt>n/2)return ans;
    30. return 0;
    31. }
    32. };

  • 相关阅读:
    数据分享|SAS数据挖掘EM贷款违约预测分析:逐步Logistic逻辑回归、决策树、随机森林...
    视频太大怎么压缩变小?超过1G的视频这样压缩
    Java8中的Stream的汇总和分组操作~它并不难的
    目标分割技术-语义分割总览
    端口探测详解
    基于SSH开发在线问卷调查系统
    牛顿法与拟牛顿法摘记
    API对接是一种在不同系统或应用程序之间共享数据和功能的方式
    嵌套合并如何操作,合并视频的同时设置新视频标题
    化工机械基础复习要点
  • 原文地址:https://blog.csdn.net/m0_64263546/article/details/133278644