笔试强训之每日一题(三)

x33g5p2x  于2022-02-07 转载在 其他  
字(2.5k)|赞(0)|评价(0)|浏览(286)

笔试强训每日一题(三)

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

题目链接

题目描述

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

输入描述

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

输出描述

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

解题思路一

使用三个string对象,str对象用来读取输入的字符串,cur对象用来保存当前遍历位置的连续数字串,ret用来保存当前遍历位置最长的连续数字串,遍历str时,当遇到数字字符时,就将数字字符放入cur中,直到数字字符结束,此时需要判断cur的大小和ret的大小,如果cur的大,就更新ret,如果cur的小,就将cur清空,继续遍历。这里需要注意边界,当连续数字串在结尾时,i在等于size时也需要进去判断一次。

解题代码一

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. string str;
  7. getline(cin,str);
  8. string cur;
  9. string ret;
  10. for(int i = 0;i <= str.size();i++)
  11. {
  12. if(str[i]>='0' && str[i]<='9')
  13. {
  14. cur += str[i];
  15. }
  16. else
  17. {
  18. if(ret.size()<cur.size())
  19. {
  20. ret = cur;
  21. }
  22. else
  23. {
  24. cur.clear();
  25. }
  26. }
  27. }
  28. cout<<ret<<endl;
  29. return 0;
  30. }

解题思路二

通过遍历,找数字字符(>=‘0’<=‘9’),找到起始下标i,用j来保存,然后判断连续的个数:while(str[i + 1] >= ‘0’&& str[i + 1] <= ‘9’),_count++。循环结束,如果比当前最大连续的个数大更新count,更新起始下标,_count置0

解题代码二

  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4. int main()
  5. {
  6. //输入一个字符串str,输出str中连续最长的数字串
  7. string str;
  8. getline(cin, str);
  9. //count记录最大连续的个数
  10. //遍历?找数字字符 -- >='0'<='9',找到起始下标i,判断连续?while(str[i + 1] >= '0'&& str[i + 1] <= '9'),_count++
  11. //循环结束,如果比当前最大连续的个数大更新count,更新起始下标,最后_count置0
  12. int count = 0;
  13. int _count = 0;//辅助的count
  14. int beginPos = 0;
  15. for (size_t i = 0; i < str.size(); i++)
  16. {
  17. if (str[i] >= '0' && str[i] <= '9')
  18. {
  19. //找到起始数字字符
  20. _count = 1;
  21. int j = i;//保存起始下标
  22. while (str[i + 1] >= '0' && str[i + 1] <= '9')
  23. {
  24. _count++;
  25. i++;
  26. }
  27. //走到这里,连续的数字字符遍历结束,判断是否更新count
  28. if (_count > count)
  29. {
  30. count = _count;
  31. beginPos = j;
  32. }
  33. _count = 0;
  34. }
  35. }
  36. for (int i = beginPos; i < beginPos + count; i++)
  37. {
  38. cout << str[i];
  39. }
  40. cout << endl;
  41. return 0;
  42. }

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

题目链接

题目描述

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

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

示例1

输入:

  1. [1,2,3,2,2,2,5,4,2]

返回值:

  1. 2

示例2

输入:

  1. [3,3,3,3,2,2,2]

返回值:

  1. 3

解题思路一:排序

  1. 首先对数组进行排序
  2. 找到中间的数字
  3. 再次遍历这个数组,计算这个数字出现了多少次

解题代码一

  1. class Solution {
  2. public:
  3. int MoreThanHalfNum_Solution(vector<int> numbers)
  4. {
  5. if(numbers.empty())
  6. {
  7. return 0;
  8. }
  9. sort(numbers.begin(),numbers.end());
  10. int midNum = numbers[numbers.size()/2];
  11. int count = 0;
  12. for(int i =0;i<numbers.size();i++)
  13. {
  14. if(midNum == numbers[i])
  15. {
  16. count++;
  17. }
  18. }
  19. if(count > numbers.size()/2)
  20. {
  21. return midNum;
  22. }
  23. return 0;
  24. }
  25. };

解题思路二:抵消

如果两个数不相等,就消去这两个数,最坏情况下,每次消去一个众数和一个非众数,那么如果存在众数,最后留下的数肯定是众数

解题代码二

  1. class Solution {
  2. public:
  3. int MoreThanHalfNum_Solution(vector<int> numbers)
  4. {
  5. if(numbers.empty())
  6. {
  7. return 0;
  8. }
  9. int result = numbers[0];
  10. int count = 1;
  11. for(int i = 1;i<numbers.size();i++)
  12. {
  13. if(count != 0)
  14. {
  15. //抵消
  16. if(numbers[i] != result)
  17. {
  18. count--;
  19. }
  20. else
  21. {
  22. count++;
  23. }
  24. }
  25. else
  26. {
  27. result = numbers[i];
  28. count = 1;
  29. }
  30. }
  31. count = 0;
  32. for(int i = 0;i<numbers.size();i++)
  33. {
  34. if(result == numbers[i])
  35. {
  36. count++;
  37. }
  38. }
  39. return count > numbers.size()/2? result:0;
  40. }
  41. };

相关文章