LeetCode每日一题: 转换成小写字母(No.709)

栏目: 编程工具 · 发布时间: 6年前

实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串。
复制代码

示例:

输入: "Hello"
输出: "hello"

输入: "here"
输出: "here"

输入: "LOVELY"
输出: "lovely"
复制代码

思考:

这题比较简单,循环判断字符是大写,就加上32即可。
复制代码

实现:

class Solution {
    public String toLowerCase(String str) {
        int  num = 'a' - 'A';
        char[] array = str.toCharArray();
        for (int count = 0; count <  str.length(); count++){
            if (array[count] >= 'A' && array[count] <= 'Z'){
                array[count] += num;
            }
        }
        return new String(array);
    }
}复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Programming PHP

Programming PHP

Rasmus Lerdorf、Kevin Tatroe、Peter MacIntyre / O'Reilly Media / 2006-5-5 / USD 39.99

Programming PHP, 2nd Edition, is the authoritative guide to PHP 5 and is filled with the unique knowledge of the creator of PHP (Rasmus Lerdorf) and other PHP experts. When it comes to creating websit......一起来看看 《Programming PHP》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换