前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >LeetCode 709. 转换成小写字母

LeetCode 709. 转换成小写字母

作者头像
村雨遥
发布2020-04-10 16:53:52
4930
发布2020-04-10 16:53:52
举报
文章被收录于专栏:JavaParkJavaPark

题目

709. 转换成小写字母[1]

描述

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

示例 1:

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

示例 2:

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

示例** 3:**

输入: "LOVELY"输出: "lovely"

解题思路

  1. 遍历字符串中每个字符,通过 ASCII 表判断字符是否属于大写;
  2. 若字符是大写,则将其+32转换为小写,为小写则不作处理;
  3. 将转换后的字符连接成赋给最终结果resultStr
  4. 返回最终结果resultStr

实现

代码语言:javascript
复制
package string;

/**
 * Created with IntelliJ IDEA.
 * Version : 1.0
 * Author  : cunyu
 * Email   : cunyu1024@foxmail.com
 * Website : https://cunyu1943.github.io
 * Date    : 2020/4/3 17:45
 * Project : LeetCode
 * Package : string
 * Class   : SevenZeroNight
 * Desc    :
 */
public class SevenZeroNight {
	public static void main(String[] args) throws Exception {
		String str = "Hello World!";
		SevenZeroNight sevenZeroNight = new SevenZeroNight();
		System.out.println(sevenZeroNight.toLowerCase(str));
	}

	public String toLowerCase(String str) {
		String resultStr = "";
		for (int i = 0; i < str.length(); i++) {
			int ch = (int) str.charAt(i);
			if ((int) ch >= 65 && (int) ch <= 90) {
				ch += 32;
			}
			resultStr += "" + (char) ch;
		}
		return resultStr;
	}
}

参考资料

[1]

709. 转换成小写字母: https://leetcode-cn.com/problems/to-lower-case/

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-04-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 村雨遥 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题目
  • 描述
  • 解题思路
  • 实现
    • 参考资料
    领券
    问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档