LeetCode | 709.转换成小写字母

这道题可以用api也可以自己实现,都不难,大小字母之前相差了32,检查到大写字母时加上32即可

python 复制代码
class Solution(object):
    def toLowerCase(self, s):
        """
        :type s: str
        :rtype: str
        """
        return s.lower()

class Solution {
public:
    string toLowerCase(string s) {
        for (char &c: s) {
            if (c >= 'A' && c <= 'Z') c = c - 'A' + 'a';
        }
        return s;
    }
};

题解的方法总让我大开眼界,值得学习!

cpp 复制代码
class Solution {
public:
    string toLowerCase(string s) {
        for (char& ch: s) {
            if (ch >= 65 && ch <= 90) {
                ch |= 32;
            }
        }
        return s;
    }
};
相关推荐
lisw057 小时前
稀疏算法概述!
算法·机器人·无人机
YLCHUP8 小时前
【括号匹配+dp优化】题解:双端括号(brackets)_动态规划_状态优化_数学建模_贪心_性质挖掘_STLmap_C++算法竞赛
数据结构·c++·笔记·算法·数学建模·贪心算法·动态规划
郝学胜-神的一滴9 小时前
Effective STL 条款09:慎重选择删除元素的方法
开发语言·网络·数据结构·c++·算法·stl
geovindu13 小时前
python:Logistic Regression
开发语言·后端·python·算法
geovindu15 小时前
python: Random Forest
开发语言·后端·python·算法·随机森林
凌冰_16 小时前
数据结构 - 线性结构(三)
java·数据结构·算法·软件工程
拂拉氏16 小时前
【知识讲解】 数据结构与算法-树状数组的认识
数据结构·算法·树状数组
饮水桶_T18 小时前
C++ STL00 | 六大核心组件协作框架 笔记
开发语言·算法·百度·facebook·新浪微博
饮水桶_T20 小时前
Java调用大语言模型、多轮对话 笔记
笔记·算法·设计模式·电脑·ai编程
AAA修煤气灶刘哥21 小时前
QQ 音乐怎么在 25 毫秒内,决定一句话该交给谁?
人工智能·算法·llm