709. To Lower Case

Algorithm

to-lower-case

https://leetcode.com/problems/to-lower-case/

1)problem

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.

Example 1:
    Input: "Hello"
    Output: "hello"
Example 2:
    Input: "here"
    Output: "here"
Example 3:
    Input: "LOVELY"
    Output: "lovely"

2)answer

声明一个新的字符串变量,对传入的字符串的字符逐个读取,如果是大写字母就取小写与大写之间的差值,得到大写字符对应的小写字母ASCII码存进字符串中。处理完后返回结果。

3)solution

Cpp:

#include <stdio.h>
#include <string>
using std::string;

class Solution {
public:
    string toLowerCase(string str) {
        string re_val = "";
        for (char str_val : str)
        {
            if (str_val >='A'&& str_val <='Z')
            {
                // 取小写与大写之间的差值,得到字符对应的小写ASCII码对应是什么存进字符串中
                re_val += (str_val + ('a' - 'A'));
            }
            else
            {
                // 如果是小写就不处理
                re_val += str_val;
            }

        }
        return re_val;

    }
};

int main()
{

    // 使用内容
    Solution nSolution;
    nSolution.toLowerCase("hello World?");
    return 0;
}

Python:

class Solution(object):
    def toLowerCase(self, str):
        """
        :type str: str
        :rtype: str
        """
        ret = ""
        for s in str:
            if s>='A' and s<='Z':
                ret += chr(ord(s)+(ord('a')- ord('A')))
            else:
                ret +=s

        return ret

原文地址:https://www.cnblogs.com/17bdw/p/10358164.html

时间: 2024-08-30 16:46:39

709. To Lower Case的相关文章

[LeetCode]709. To Lower Case

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. 将字符串的大写字母转成小写字母字符串. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Examp

LeetCode--To Lower Case &amp; Remove Outermost Parentheses (Easy)

709. To Lower Case(Easy) Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here"

2. To Lower Case

Title: Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Note: None Analysi

LeetCode算法题-To Lower Case(Java实现)

这是悦乐书的第301次更新,第320篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第169题(顺位题号是709).实现具有字符串参数str的函数ToLowerCase():以小写形式返回相同的字符串.例如: 输入:"Hello" 输出:"hello" 输入:"here" 输出:"here" 输入:"LOVELY" 输出:"lovely" 本次解题使用的开发工

[LeetCode] To Lower Case 转为小写

Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Example 3: Input: "L

(Easy) To Lower Case LeetCode

Description Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Example 3: In

arts-week6

Algorithm 830. Positions of Large Groups - LeetCode 75. Sort Colors - LeetCode 859. Buddy Strings - LeetCode 791. Custom Sort String - LeetCode 804. Unique Morse Code Words - LeetCode 877. Stone Game - LeetCode 890. Find and Replace Pattern - LeetCod

LintCode-Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Note It's not necessary to keep the original order of lower-case letters and upper case letters. Example For "abAcD", a reasonable answer is "acb

Sort Letters by Case

Given a string which contains only letters. Sort it by lower case first and upper case second. Example For "abAcD", a reasonable answer is "acbAD" 与将负数都放在前面,正数都放在后面的题目一样. 时间复杂度为O(n) 找到第一大写字母,记录其下标为i,则小写字母必定在i之后出现,在i之后找到第一个出现的小写字母j.交换i,