2025年1月21日刷题记载

王柳  论坛元老 | 2025-1-22 14:13:31 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 1911|帖子 1911|积分 5733

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
1.leetcode1768标题
链接:1768. 瓜代合并字符串 - 力扣(LeetCode)
代码:
  1. class Solution {
  2. public:
  3.     string mergeAlternately(string word1, string word2) {
  4.         string word3;
  5.         int a = word1.size(), b = word2.size();
  6.         int i = 0, j = 0;
  7.         while(i<a || j<b){
  8.             if(i<a){
  9.                 word3+=word1[i];
  10.                 i++;
  11.             }
  12.             if(j<b){
  13.                 word3+=word2[j];
  14.                 j++;
  15.             }
  16.         }
  17.         return word3;
  18.     }
  19. };
复制代码
履历:


  • 原代码中直接使用 word3[k] = word1 和 word3[k] = word2[j],这种方式在 word3 未初始化大小时会导致未界说行为。应该使用word3+=word1.
2.力扣1071题
链接:1071. 字符串的最大公因子 - 力扣(LeetCode)

  1. class Solution {
  2. public:
  3.     string gcdOfStrings(string str1, string str2) {
  4.         int a=str1.size(),b=str2.size();
  5.         int gcd_len=gcd(a,b);
  6.         string gcd_string=str1.substr(0,gcd_len);
  7.         if(check(str1,gcd_string) && check(str2,gcd_string)){
  8.             return gcd_string;
  9.         }else{
  10.             return "";
  11.         }
  12.     }
  13. private:
  14.     bool check(string &a,string &b){
  15.         int len_a=a.size(),len_b=b.size();
  16.         int time=len_a/len_b;
  17.         string repeated_str=b;
  18.         for(int i=1;i<time;i++){
  19.             repeated_str+=b;
  20.         }
  21.         return repeated_str==a;
  22.     }
  23. };
复制代码
学习到:


  • string可以直接通过+=进行拼接。
  • gcd函数的使用
  • substr函数:str.substr(start,length)
3.力扣1431标题
1431. 拥有最多糖果的孩子 - 力扣(LeetCode)
  1. class Solution {
  2. public:
  3.     vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
  4.         int max = *max_element(candies.begin(),candies.end());
  5.         vector<bool> index;
  6.         for(int i=0;i<candies.size();i++){
  7.             if(candies[i]+extraCandies>=max){
  8.                 index.push_back(true);
  9.             }else{
  10.                 index.push_back(false);
  11.             }
  12.         }
  13.         return index;
  14.     }
  15. };
复制代码
 学习:


  • 新建vector:直接vector <type> name;
  • push_back()在vector的末了添加元素
  • 求解最大值:int max = *max_element(candies.begin(),candies.end());(加*是解迭代器)
4.力扣345题
  1. class Solution {
  2. public:
  3.     string reverseVowels(string s) {
  4.         int i = 0, j = s.size() - 1;  // 双指针
  5.         unordered_set<char> vowels = {'a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'};
  6.         while (i < j) {
  7.             // 移动左指针,直到找到元音
  8.             while (i < j && vowels.find(s[i]) == vowels.end()) {
  9.                 i++;
  10.             }
  11.             // 移动右指针,直到找到元音
  12.             while (i < j && vowels.find(s[j]) == vowels.end()) {
  13.                 j--;
  14.             }
  15.             // 交换两个元音字符
  16.             if (i < j) {
  17.                 swap(s[i], s[j]);
  18.                 i++;
  19.                 j--;
  20.             }
  21.         }
  22.         return s;
  23.     }
  24. };
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王柳

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表