LintCode 大小写转换

题目

将字符串中的小写字母转换为大写字母

题解

直接利用C++中的tolower(大写转小写)函数。

class Solution {
    
    
public:
    /**
     * @param str: the input string
     * @return: The lower case string
     */
    string toLowerCase(string &str) {
    
    
        // Write your code here
        for( int i=0; i<str.length(); i++ ){
    
    
            str[i] = tolower(str[i]);
        }
        return str;
    }
};

上述代码运行时间为43ms,空间消耗3.62MB。

class Solution {
    
    
public:
    /**
     * @param str: the input string
     * @return: The lower case string
     */
    string toLowerCase(string &str) {
    
    
        // Write your code here
        for(int i=0;i<str.size();i++)
        {
    
    
            if(str[i]>='A'&&str[i]<='Z')
            {
    
    
                str[i]+=32;
            }
        }
        return str;
    }
};

时间消耗 :20ms
空间消耗 :4.16MB

猜你喜欢

转载自blog.csdn.net/m0_46161051/article/details/119947137
今日推荐