Algorithm

to-lower-case

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

1)problem

  1. Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.
  2. Example 1:
  3. Input: "Hello"
  4. Output: "hello"
  5. Example 2:
  6. Input: "here"
  7. Output: "here"
  8. Example 3:
  9. Input: "LOVELY"
  10. Output: "lovely"

2)answer

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

3)solution

Cpp:

  1. #include <stdio.h>
  2. #include <string>
  3. using std::string;
  4. class Solution {
  5. public:
  6. string toLowerCase(string str) {
  7. string re_val = "";
  8. for (char str_val : str)
  9. {
  10. if (str_val >='A'&& str_val <='Z')
  11. {
  12. // 取小写与大写之间的差值,得到字符对应的小写ASCII码对应是什么存进字符串中
  13. re_val += (str_val + ('a' - 'A'));
  14. }
  15. else
  16. {
  17. // 如果是小写就不处理
  18. re_val += str_val;
  19. }
  20. }
  21. return re_val;
  22. }
  23. };
  24. int main()
  25. {
  26. // 使用内容
  27. Solution nSolution;
  28. nSolution.toLowerCase("hello World?");
  29. return 0;
  30. }

Python:

  1. class Solution(object):
  2. def toLowerCase(self, str):
  3. """
  4. :type str: str
  5. :rtype: str
  6. """
  7. ret = ""
  8. for s in str:
  9. if s>='A' and s<='Z':
  10. ret += chr(ord(s)+(ord('a')- ord('A')))
  11. else:
  12. ret +=s
  13. return ret

709. To Lower Case的更多相关文章

  1. Leetcode#709. To Lower Case(转换成小写字母)

    题目描述 实现函数 ToLowerCase(),该函数接收一个字符串参数 str,并将该字符串中的大写字母转换成小写字母,之后返回新的字符串. 示例 1: 输入: "Hello" ...

  2. 【Leetcode_easy】709. To Lower Case

    problem 709. To Lower Case solution1: class Solution { public: string toLowerCase(string str) { stri ...

  3. 709. To Lower Case - LeetCode

    Question 709. To Lower Case Sollution 题目大意:字符串大写转小写 思路: 直接调用Java API函数 字符串转char数组,遍历数组,判断如果大写就转小写 Ja ...

  4. 【Leetcode】709. To Lower Case

    To Lower Case Description Implement function ToLowerCase() that has a string parameter str, and retu ...

  5. LeetCode 709 To Lower Case 解题报告

    题目要求 Implement function ToLowerCase() that has a string parameter str, and returns the same string i ...

  6. 【LeetCode】709. To Lower Case 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述: 题目大意 解题方法 ASIIC码操作 日期 题目地址:https:// ...

  7. LeetCode 709.To Lower Case

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

  8. LeetCode 709. To Lower Case (转换成小写字母)

    题目标签:String 题目让我们把大写字母转换成小写,只要遇到的是大写字母,把它 + 32 变成 小写就可以了. Java Solution: Runtime beats 100.00% 完成日期: ...

  9. LeetCode--To Lower Case && Remove Outermost Parentheses (Easy)

    709. To Lower Case(Easy)# Implement function ToLowerCase() that has a string parameter str, and retu ...

随机推荐

  1. 从Paxos到Zookeeper分布式一致性原理与实践 读书笔记之(一) 分布式架构

    1.1 从集中式到分布式 1 集中式特点 结构简单,无需考虑对多个节点的部署和节点之间的协作. 2  分布式特点 分不性:在时间可空间上随意分布,机器的分布情况随时变动 对等性:计算机之间没有主从之分 ...

  2. Hadoop记录-Linux Service

    [Unit] Description=Datanode After=syslog.target network.target auditd.service sshd.service datanode_ ...

  3. 学习go语言编程系列之helloworld

    1. 下载https://golang.org/dl/ # Go语言官网地址,在国内下载太慢,甚至都无法访问.通过如下地址下载:https://golangtc.com/download. 2. 安装 ...

  4. 利用 yEd 软件做元数据管理

    利用 yEd 软件做元数据管理 yEd Diagram editor 是我常用的 flow chart 制图工具, 另外我也用它画 ER 和 use case 图. 总结一下我喜欢 yEd 的原因:1 ...

  5. None.js 第二步 REPL(交互式解析器)

    简单的表达式 $ node 1 + 4 // 5 5 / 2 // 2.5 3 * 5 // 15 使用变量 $ node x = 5 // 5 var y = 10 // undefined con ...

  6. select实现简单TCP通信(ubuntu 18.04)

    一.服务器程序(server.c) #include <stdio.h> #include <unistd.h> #include <stdlib.h> #incl ...

  7. 三十八、Linux 线程——线程属性初始化、销毁、设置和获得分离属性

    38.1 线程属性初始化和销毁 #include <pthread.h> int pthread_attr_init(pthread_attr_t *attr); int pthread_ ...

  8. 七、文件IO——I/O处理方式和文件锁

    7.1 I/O 处理方式 7.1.1 I/O处理的五种模型 阻塞I/O模型 若所调用的 I/O 函数没有完成相关的功能就会使进程挂起,直到相关数据到达才会返回.如 终端.网络设备的访问. 非阻塞模型 ...

  9. hackrf入门

    http://www.hackrf.net/hackrf%E4%B8%8Egnuradio%E5%85%A5%E9%97%A8%E6%8C%87%E5%8D%97/

  10. MVC中的分部视图

    背景: 项目的工期马上就要到了,由于后台封装的很好,我们只需要用心熟悉框架,接下来后台的工作就是简单的代码工作了.原本以为最困难的时期已经过去,可没想到前台才是最困难的. B/S的基础十分薄弱,加上B ...