剑指offer之空格替换(Python)
来源:互联网 发布:u盘windows无法格式化 编辑:程序博客网 时间:2024/06/11 23:28
题目描述
请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
这题如果利用Python会比较简单,三种思路:
1、使用Python 的append函数
2、使用Python的split函数
3、直接利用字符串进行拼接
代码如下:
# -*- coding: utf-8 -*-"""Created on Sat Oct 14 17:20:46 2017@author: gb_xiaoMail: mingliumengshao@163.com""""""题目描述:请实现一个函数,将一个字符串中的空格替换成“%20”。例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。"""def function1(string): result = [] for i in string: if i == " ": result.append("%20") else: result.append(i) return ''.join(result)def function2(string): lists = string.split(" ") return '%20'.join(lists)def function3(string): result = "" for i in string: if i == " ": result = result + "%20" else: result = result + i return resultdef main():# string = "We Are Happy" print function1("We Are Happy") print function2("We Are Happy") print function3("We Are Happy") if __name__ == "__main__": main()
阅读全文
0 0
- 剑指offer之空格替换(Python)
- (二)剑指Offer之替换空格
- 剑指Offer之替换空格
- 剑指Offer之 - 替换空格
- 剑指offer之替换空格
- 剑指offer之空格替换
- 剑指offer之替换空格
- 剑指offer之替换空格
- 剑指offer之替换空格
- 《剑指offer》之替换空格
- 剑指offer之 替换空格
- 剑指offer之替换空格
- 替换空格(剑指offer)
- (剑指offer)替换空格
- 剑指offer 算法2 替换空格(python)
- 剑指offer之替换空格问题
- 剑指offer面试题之替换空格
- 码农小汪-剑指Offer之5 -替换空格
- eclipse连接MSSQL2000实例
- 谈谈document.ready和window.onload的区别
- JDK源码获取
- 原型链模式扩展
- XSS浅学习
- 剑指offer之空格替换(Python)
- Linux常用指令
- HttpURLConnection 请求
- spring 源码学习笔记(一)—— spring ioc 之加载XML转换为BeanDefinition
- 论文管理方法
- Failed to resolve: com.android.support:26.X.X 问题的解决
- 基于Python安装TensorFlow、库安装和Jupyter Notebook
- php静态页面局部动态化
- 周六周日还不闲着,加油学习