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

1037555-20190309224001590-424016861.png

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

解题思路

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

遇到问题

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

Sample Submission

sample 0 ms submission

char* toLowerCase(char* str) {
    int c = 0;
    while (*(str+c) != '\0')
    {
        if (*(str+c) >= 'A' && *(str+c) <= 'Z')
        {
            *(str+c) = *(str+c) + 32;
        }
        c++;
   }    

    return str;
}
  • 指针操作可以加快运行速度?
  • *(str+c) = *(str+c) + 32换成*(str+c) += 32会增加运行时间?

转载于:https://www.cnblogs.com/huerxiong/p/10503558.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值