当前位置:网站首页>[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
[programming compulsory training 3] find the longest consecutive number string in the string + the number that appears more than half of the times in the array
2022-07-01 07:15:00 【... running snail ~】
1. Find the longest consecutive number string in the string -> link

【 Their thinking 】:
Traversal string , Use cur To record a continuous string of numbers , If you encounter non numeric characters , It means that a continuous number string ends , Then compare the number string with the previous number string , If it's longer , Then update the longer number string to res.
Code implementation :
#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 {
// Find a longer string , Then update the string
if (ret.size() < cur.size())
{
ret = cur;
}
else
{
cur.clear();
}
}
}
cout << ret;
return 0;
}
2. A number that appears more than half the times in an array -> link

【 Their thinking 1】:
Train of thought : After array sorting , If the qualified number exists , Then it must be the number in the middle of the array . Although this method is easy to understand , But because it involves fast platoon sort, Its time complexity is O(NlogN) Not optimal .
Code implementation :
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers) {
if(numbers.empty()) // Sentenced to empty
return 0;
sort(numbers.begin(),numbers.end());// Sort
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;
}
};
【 Their thinking 2】:
The number of : The number that occurs more than half the length of the array
If two numbers are not equal , Just eliminate these two numbers , In the worst case , Eliminate one mode and one non mode at a time , So if there are modes , The last number left must be the mode .
Code implementation :
class Solution {
public:
int MoreThanHalfNum_Solution(vector<int> numbers)
{
if (numbers.empty()) return 0;
// Traverse each element , And record the number of times ; If it's the same as the previous element , Then the number of times plus 1, Otherwise, the times will be reduced 1
int result = numbers[0];
int times = 1; // frequency
for (int i = 1; i < numbers.size(); ++i)
{
if (times != 0)
{
if (numbers[i] == result)
{
++times;
}
else
{
--times;
}
}
else
{
result = numbers[i];
times = 1;
}
}
// Judge result Whether the conditions are met , That is, the number of occurrences is greater than half of the length of the array
times = 0;
for (int i = 0; i < numbers.size(); ++i)
{
if (numbers[i] == result) ++times;
}
return(times > numbers.size() / 2) ? result : 0;
}
};
边栏推荐
- 灰度何以跌下神坛?
- 未来互联网人才还稀缺吗?哪些技术方向热门?
- Image style migration cyclegan principle
- STM32F1与STM32CubeIDE编程实例-NEC协议红外接收与解码
- JSP - paging
- Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
- rclone常用子命令中文解释
- 【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
- 【LINGO】求七个城市最小连线图,使天然气管道价格最低
- Product learning (II) - competitive product analysis
猜你喜欢

【编程强训2】排序子序列+倒置字符串
![Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list](/img/9a/44976b5df5567a7aff315e63569f6a.png)
Those high-frequency written tests and interview questions in [Jianzhi offer & Niuke 101] - linked list

运维面临挑战?智能运维管理系统来帮您

K8s set up redis cluster

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

【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
![[lingo] find the shortest path problem of undirected graph](/img/14/1ccae0f33f5857b546d7fd0aa74c35.png)
[lingo] find the shortest path problem of undirected graph

How to create an exclusive vs Code theme

【计网】(一) 集线器、网桥、交换机、路由器等概念

Challenges faced by operation and maintenance? Intelligent operation and maintenance management system to help you
随机推荐
The game is real! China software cup releases a new industrial innovation competition, and schools and enterprises can participate in it jointly
Problem solving: officeexception: failed to start and connect (I)
Docker installation and deployment redis
Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
K8s set up redis cluster
Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
C# 读写自定义的Config文件
Solve the problem that the class defined in meta-inf.services cannot be read
代码实战——从零开始搭建自己的Diffusion models/Score-based generative models
Introduction to spark (one article is enough)
手机开户选哪个证券公司比较好,哪个更安全
未来互联网人才还稀缺吗?哪些技术方向热门?
关于“2022年度网络安全教育线上培训”相关问题的复盘和说明
【编程强训】删除公共字符(哈希映射)+组队竞赛(贪心)
Product learning (I) - structure diagram
图像风格迁移 CycleGAN原理
Solution to the problem that objects in unity2021 scene view cannot be directly selected
[matlab] solve nonlinear programming
8 figures | analyze Eureka's first synchronization registry
电脑有网络,但所有浏览器网页都打不开,是怎么回事?