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;
    }
};
相关推荐
你撅嘴真丑14 小时前
第九章-数字三角形
算法
uesowys14 小时前
Apache Spark算法开发指导-One-vs-Rest classifier
人工智能·算法·spark
ValhallaCoder14 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
董董灿是个攻城狮14 小时前
AI 视觉连载1:像素
算法
智驱力人工智能14 小时前
小区高空抛物AI实时预警方案 筑牢社区头顶安全的实践 高空抛物检测 高空抛物监控安装教程 高空抛物误报率优化方案 高空抛物监控案例分享
人工智能·深度学习·opencv·算法·安全·yolo·边缘计算
孞㐑¥15 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
月挽清风15 小时前
代码随想录第十五天
数据结构·算法·leetcode
XX風15 小时前
8.1 PFH&&FPFH
图像处理·算法
NEXT0616 小时前
前端算法:从 O(n²) 到 O(n),列表转树的极致优化
前端·数据结构·算法
代码游侠16 小时前
学习笔记——设备树基础
linux·运维·开发语言·单片机·算法