IT评测·应用市场-qidao123.com技术社区

标题: 2025年1月21日刷题记载 [打印本页]

作者: 王柳    时间: 2025-1-22 14:13
标题: 2025年1月21日刷题记载
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. };
复制代码
履历:

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. };
复制代码
学习到:

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. };
复制代码
 学习:

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企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 IT评测·应用市场-qidao123.com技术社区 (https://dis.qidao123.com/) Powered by Discuz! X3.4