zoukankan      html  css  js  c++  java
  • LeetCode 709.To Lower Case

    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:
    Input: "LOVELY"
    Output: "lovely"

    My Submission

    char* toLowerCase(char* str) {
        int i = 0;
        
        while(str[i] != '')
        {
            if(str[i] >= 'A' && str[i] <= 'Z')
                str[i] = str[i] + 32;
            i++;
        }
        return str;
    }
    

    解题思路

    转化大小写,首先想到ASCII码字母大小写之间相差32,字符串又是以''做为结束标识符的,所以循环当字符串没到末尾时,如果有大写字母就加上32并赋给当前字符,最后循环结束后返回指针。

    遇到问题

    刚开始不记得是小写字母大以及大小写相差多少,小写字母 = 大写字母 + 32;开始WA了一发是因为未判断字符等于A和Z的情况,写成了小于和大于。

    Sample Submission

    sample 0 ms submission

    char* toLowerCase(char* str) {
        int c = 0;
        while (*(str+c) != '')
        {
            if (*(str+c) >= 'A' && *(str+c) <= 'Z')
            {
                *(str+c) = *(str+c) + 32;
            }
            c++;
       }    
    
        return str;
    }
    
    • 指针操作可以加快运行速度?
    • *(str+c) = *(str+c) + 32换成*(str+c) += 32会增加运行时间?
  • 相关阅读:
    eclipse 10个常用 快捷键
    struts2 s:set标签
    web项目的路径问题
    linux系统中用户切换
    java的多线程(一)
    Mongodb 与 Mongoose 的使用
    escape()、encodeURI()、encodeURIComponent()区别详解
    JSON WEB TOKEN
    关于使用REST API
    mac下设置mongodb开机启动方法
  • 原文地址:https://www.cnblogs.com/huerxiong/p/10503558.html
Copyright © 2011-2022 走看看