一、字符串/数组

1.1768.交替合并字符串

class Solution {
public:
    string mergeAlternately(string word1, string word2) {
    	string res;
    	int i = 0;
    	int j = 0;
    	int m = word1.size();
    	int n = word2.size();
    	res.reserve(m + n);
    	while (i < m || j < n) {
			if (i < m) {
				res.push_back(word1[i]);
				i ++;
			}
			if (j < n) {
				res.push_back(word2[j]);
                j ++;
			}
		}
		return res;
}
};

2.1071.字符串的最大公因子

class Solution {
public:
	int gcd(int a,int b) {
		return b == 0 ? a : gcd(b,a % b);
	}
    string gcdOfStrings(string str1, string str2) {
    	if (str1 + str2 != str2 + str1) return "";
    	return str1.substr(0,gcd(str1.size(),str2.size()));
   }
};

3.1431. 拥有最多糖果的孩子

class Solution {
public:
    vector<bool> kidsWithCandies(vector<int>& candies, int extraCandies) {
        int minx = INT_MIN;
        for (auto can : candies) {
            if (can > minx) {
                minx = can;
            }
        }
        vector<bool> res(candies.size());
        for (int i = 0; i < candies.size(); i++) {
            res[i] = (candies[i] + extraCandies) >= minx;
        }
    return res;
    }
};

4.605. 种花问题

class Solution {
public:
	bool canPlaceFlowers(vector<int>& flowerbed, int n) {
		int length = flowerbed.size();
		for (int i = 0; i < length; i++) {
			if (flowerbed[i] == 0 && ((i == 0 || flowerbed[i - 1] == 0)) && ((i == length - 1) || flowerbed[i + 1] == 0))  {
                n --;
                flowerbed[i] = 1;
            }
		}
		return false;
	}

};

5.345. 反转字符串中的元音字母

class Solution {
public:
		string reverseVowels(string s) {
			int l = 0;
			int r = s.size() - 1;
			unordered_set <char> vowels = {'a','e','i','o','u','A','E','I','O','U'};
			while (l < r ) {
				if (!vowels.count(s[l]) l ++;
				else if (!vowels.count(s[r])) 
                r --;
            else swap(s[l++], s[r--]);
 			}
		}
		return s;
	}
};

相关推荐

  1. 字符串/数组

    2024-07-13 14:00:01       21 阅读
  2. 字符数组(字符串):单词计数

    2024-07-13 14:00:01       34 阅读
  3. php 生成段随机的 字符串 可以设置数字字符

    2024-07-13 14:00:01       35 阅读
  4. 字符串数组字符串指针

    2024-07-13 14:00:01       56 阅读
  5. 字符数组算法整理

    2024-07-13 14:00:01       31 阅读
  6. .net 框架基础() 字符字符串

    2024-07-13 14:00:01       30 阅读
  7. LeetCode每日题[c++]-找出字符串的可整除数组

    2024-07-13 14:00:01       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-13 14:00:01       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-13 14:00:01       72 阅读
  3. 在Django里面运行非项目文件

    2024-07-13 14:00:01       58 阅读
  4. Python语言-面向对象

    2024-07-13 14:00:01       69 阅读

热门阅读

  1. 2024年城市客运安全员考试题库及答案

    2024-07-13 14:00:01       17 阅读
  2. SwiftBrush算法与代码解读

    2024-07-13 14:00:01       20 阅读
  3. 005-基于Sklearn的机器学习入门:逻辑回归

    2024-07-13 14:00:01       28 阅读
  4. opencv—常用函数学习_“干货“_总

    2024-07-13 14:00:01       22 阅读
  5. Web组成架构

    2024-07-13 14:00:01       23 阅读
  6. Artificial intelligence machine learning DATA4800

    2024-07-13 14:00:01       24 阅读
  7. 自用的C++20协程学习资料

    2024-07-13 14:00:01       21 阅读
  8. 如何在uniapp中使用websocket?

    2024-07-13 14:00:01       16 阅读
  9. 【linux】预防rm误删文件的3种方法

    2024-07-13 14:00:01       23 阅读