1. 程式人生 > 其它 >【Lintcode】1535. To Lower Case

【Lintcode】1535. To Lower Case

技術標籤:# 棧、佇列、串及其他資料結構字串leetcode演算法

題目地址:

https://www.lintcode.com/problem/to-lower-case/description

給定一個字串 s s s,將其所有字母變為小寫。返回新字串。

程式碼如下:

public class Solution {
    /**
     * @param str: the input string
     * @return: The lower case string
     */
    public String toLowerCase(String str) {
        // Write your code here
StringBuilder sb = new StringBuilder(); int diff = 'A' - 'a'; for (int i = 0; i < str.length(); i++) { char ch = str.charAt(i); if ('A' <= ch && ch <= 'Z') { sb.append((char) (ch - diff)); } else { sb.
append(ch); } } return sb.toString(); } }

時空複雜度 O ( l s ) O(l_s) O(ls)