当前位置:网站首页>【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
【编程强训3】字符串中找出连续最长的数字串+数组中出现次数超过一半的数字
2022-07-01 07:07:00 【…狂奔的蜗牛~】
1.字符串中找出连续最长的数字串 ->链接

【解题思路】:
遍历字符串,使用cur去记录连续的数字串,如果遇到不是数字字符,则表示一个连续的数字串结束了,则将数字串跟之前的数字串比较,如果更长,则更新更长的数字串更新到res。
代码实现:
#include<iostream>
#include<string>
using namespace std;
int main()
{
string s, ret, cur;
cin >> s;
for (int i = 0; i <= s.length(); i++)
{
if (s[i] >= '0' && s[i] <= '9')
{
cur += s[i];
}
else {
// 找出更长的字符串,则更新字符串
if (ret.size() < cur.size())
{
ret = cur;
}
else
{
cur.clear();
}
}
}
cout << ret;
return 0;
}
2.数组中出现次数超过一半的数字 -> 链接

【解题思路1】:
思路一:数组排序后,如果符合条件的数存在,则一定是数组中间那个数。这种方法虽然容易理解,但由于涉及到快排sort,其时间复杂度为O(NlogN)并非最优.
代码实现:
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(numbers[i]==middle)
count++;
}
return (count>numbers.size()/2)? middle : 0;
}
};
【解题思路2】:
众数:就是出现次数超过数组长度一半的那个数字
如果两个数不相等,就消去这两个数,最坏情况下,每次消去一个众数和一个非众数,那么如果存在众数,最后留下的数肯定是众数。
代码实现:
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers)
{
if (numbers.empty()) return 0;
// 遍历每个元素,并记录次数;若与前一个元素相同,则次数加1,否则次数减1
int result = numbers[0];
int times = 1; // 次数
for (int i = 1; i < numbers.size(); ++i)
{
if (times != 0)
{
if (numbers[i] == result)
{
++times;
}
else
{
--times;
}
}
else
{
result = numbers[i];
times = 1;
}
}
// 判断result是否符合条件,即出现次数大于数组长度的一半
times = 0;
for (int i = 0; i < numbers.size(); ++i)
{
if (numbers[i] == result) ++times;
}
return(times > numbers.size() / 2) ? result : 0;
}
};
边栏推荐
- How to choose a product manager course when changing to a product manager?
- 运维管理系统,人性化操作体验
- Problem solving: officeexception: failed to start and connect (I)
- Ctfhub port scan (SSRF)
- 【LINGO】求无向图的最短路问题
- 在长城证券上做基金定投安全吗?
- Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
- Buildreoot override mechanism
- 運維管理系統,人性化操作體驗
- iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事
猜你喜欢

C# 读写自定义的Config文件

Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor

8 张图 | 剖析 Eureka 的首次同步注册表

DC-4 target

WiFi settings for raspberry Pie 4

Why did grayscale fall from the altar?

【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真

Solution to the problem that objects in unity2021 scene view cannot be directly selected

Why are so many people turning to product managers? What is the development prospect of product manager?

ctfshow-web352,353(SSRF)
随机推荐
Understand esp32 sleep mode and its power consumption
Principle of introducing modules into node
【图像处理】图像直方图均衡化系统含GUI界面
Rclone Chinese document: a collection of common commands
JSP - paging
【剑指offer&牛客101】中那些高频笔试,面试题——链表篇
【深圳IO】精确食品称(汇编语言的一些理解)
北漂程序员深夜emo发帖求助:女朋友走了我很孤独 ......
8 张图 | 剖析 Eureka 的首次同步注册表
【FPGA帧差】基于VmodCAM摄像头的帧差法目标跟踪FPGA实现
AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
Easynvs cloud management platform function reconfiguration: support adding users, modifying information, etc
Kdtree notes
Solve the problem that the class defined in meta-inf.services cannot be read
Product learning (III) - demand list
ESP32深度睡眠电流怎样低于10uA
Esp32 - ULP coprocessor reading Hall sensor in low power mode
女生适合学产品经理吗?有什么优势?
微软宣布开源 (GODEL) 语言模型聊天机器人
Are there any practical skills for operation and maintenance management