[日常编程训练]Day3:字符串中找连续最长的数字串与找出数组中出现次数超过一半的数字

目录

一.日常训练题目

1.1 字符串中找连续最长的数字串

1.1.1 题目描述

1.1.2 思路剖析

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

2.1.1 题目描述

2.1.2 思路剖析

二.总结


现在更新变成早上来进行更新了,谢谢大家,笔者会继续坚持下去的.

一.日常训练题目

1.1 字符串中找连续最长的数字串

1.1.1 题目描述

链接:字符串中找出连续最长的数字串_牛客题霸_牛客网

来源:牛客网

读入一个字符串str,输出字符串str中的连续最长的数字串.

输入描述:

测试输入包含1个测试用例,一个字符串str,长度不超过255。

输出描述: 

在一行内输出str中里连续最长的数字串。

示例:

输入:

abcd12345ed125ss123456789

输出:

123456789

1.1.2 思路剖析

 对应的代码如下

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string s1;
    getline(cin,s1);
    //记录的输出字符串
    string s2;

    int Maxcount=0;
    int count=0;
    int size=s1.size();
    int i=0;
    while(i<size)
    {
        if(s1[i]>='a'&&s1[i]<='z')
        {
            //说明这里就遍历到字母了
            i++;

        }
        else if(s1[i]>='1'&&s1[i]<='9')
        {
            //说明这里就遍历到数字了
            //先查一下遍历到数字的个数
            int j=i;
            while(s1[j]>='1'&&s1[j]<='9')
            {
                j++;
                count++;
            }
            //在进行比较
            if(count>Maxcount)
            {
                //输出字符串加上对应的字符串
                s2="";
                for(;i<=(j-1);++i)
                {
                   s2+=s1[i];
                }
                Maxcount=count;
                count=0;
                i=j;
            }
            else
            {
                count=0;
                i=j;
            }
          
        }
        else{
            i++;
        }
    }
    cout<<s2<<endl;

    return 0;
}

 优秀的解题思路:

int main()
{
    string str, res, cur;
    //从键盘上面输入的数据放到str里面
    cin >> str;

    //利用for循环对字符串进行遍历
    for (int i = 0; i < str.length(); i++)
    {
        //1.遍历到了数字字符串
        if (str[i] >= '0' && str[i] <= '9')
        {
            cur += str[i];
        }
        else
        {
            //观察对应的数字字符串
            if (res.size() < cur.size())
            {
                res = cur;
            }
            else
            {
                //清零cur字符串继续往后面进行遍历
                cur = "";
            }
        }
    }
    cout << res << endl;

    return 0;
}

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

2.1.1 题目描述

链接:数组中出现次数超过一半的数字_牛客题霸_牛客网 (nowcoder.com)

来源:牛客网

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

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

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

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

输入描述:

保证数组输入非空,且保证有解

输出描述:

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

示例:

输入:[1,2,3,2,2,2,5,4,2]

返回值:2

2.1.2 思路剖析

第一种方法的思路:

 代码剖析:

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) {
    
    //先对这个数组进行排序
    //如果有数字超过数组的一半的话,数组中出现次数超过一半的数字肯定在最中间
    if(numbers.empty())
    {
        //这块是肯定不行的
        return 0;
    }
    sort(numbers.begin(),numbers.end());
    //最中间的这个数肯定
    int middle=numbers[numbers.size()/2];

    //记录一下整个数字出现的次数
    int count=0;
    for(int i=0;i<numbers.size();i++)
    {
        //进行数字比较
        if(middle==numbers[i])
        {
            count++;
        }
    }

    if(count>=(numbers.size()/2))
    {
        return middle;
    }
    else
    {
        return 0;
    }

    }
};

第二种方法的思路: 

 代码剖析:

class Solution {
public:
    int MoreThanHalfNum_Solution(vector<int> numbers) 
    {

        //先判断一下数组是否为空
        if(numbers.empty())
        {
            return 0;
        }
        //记录数据中众数
        int count=1;
        int result=numbers[0];
        for(int i=1;i<numbers.size();++i)
        {
            if(count!=0)
            {
                if(numbers[i]==result)
                {
                    ++count;
                }
                else
                {
                    --count;
                }
            }
            else
            {
                //说明此时count==0
                result=numbers[i];
                count=1;
            }
        }
        //跳出循环之后就进行判断
        count=0;
        for(int i=0;i<numbers.size();++i)
        {
            if(result==numbers[i])
            {
                count++;
            }
        }

        if(count>=numbers.size()/2)
        {
            return result;
        }
        else
        {
            return 0;
        }
    
    }
};

二.总结

个人代码和笔记已经上传到自己的gittee上面,有需要的小伙伴请自取.

Gittee:阮志鹏 (ruan-zhipeng) - Gitee.com

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

胡须不排序H

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值