【算法编程 C++ Python】字符串替换
题目描述
#include <iostream>
using namespace std;
class Solution {
public:
void replaceSpace(char * str, int length) {
// 在str原地址上替换,先遍历空格数量,再从后向前替换(从前向后替换费时费力)
int n = strlen(str);// 原字符串长度,不包含\0
int space_num = ;
for (int i = ; i < n; i++) {
if (str[i] == ' ') {
space_num++;
}
}
int n_new = n + space_num*; //一个空格变成三个字符%20
while ((n>=)&&(n_new>n))
{
cout << str[n] << endl;
// 从'\0'开始
if (str[n] == ' ') {
str[n_new--] = '';
str[n_new--] = '';
str[n_new--] = '%';
}
else { str[n_new--] = str[n];
}
n--;
}
}
}; int main()
{
Solution obj;
char* str = "hello world";
int n = strlen(str);// 字符串长度,不包含\0
obj.replaceSpace(str, n);
cout << str << endl;
cin.get();
cin.get();
return ;
}
C++的代码在vs2015会有写入冲突,还是用string比较好不会有这种冲突。。但这段代码在牛客网上是通的……
Python:
# -*- coding:utf-8 -*-
class Solution:
def replaceSpace(self, s):
s_new = ""
n = len(s)
for i in xrange(n):
if s[i]==' ':
s_new += "%20"
else:
s_new += s[i]
return s_new if __name__ == '__main__':
obj = Solution()
s = "hello world"
print obj.replaceSpace(s)
在牛客网上测试时间与空间占用:
C++: 3ms,488k
Python:19ms,5728k
【算法编程 C++ Python】字符串替换的更多相关文章
- StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing the strings?
StackOverFlow排错翻译 - Python字符串替换: How do I replace everything between two strings without replacing t ...
- python 字符串替换
字符串替换可以用内置的方法和正则表达式完成.1用字符串本身的replace方法: a = 'hello word'b = a.replace('word','python')print b 2用正则表 ...
- python字符串替换的2种有效方法
python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'我把a字符串里的word替换为python1用字符串本身 ...
- python字符串替换的2种方法
python 字符串替换可以用2种方法实现:1是用字符串本身的方法.2用正则来替换字符串 下面用个例子来实验下:a = 'hello word'把a字符串里的word替换为python 1.用字符串本 ...
- python 字符串替换功能 string.replace()可以用正则表达式,更优雅
说起来不怕人笑话,我今天才发现,python 中的字符串替换操作,也就是 string.replace() 是可以用正则表达式的. 之前,我的代码写法如下,粗笨: 自从发现了正则表达式也生效后,代码变 ...
- python 字符串替换、正则查找替换
import re if __name__ == "__main__": url = " \n deded<a href = "">这是第 ...
- python字符串替换之re.sub()
re.sub(pattern, repl, string, count=0, flags=0) pattern可以是一个字符串也可以是一个正则,用于匹配要替换的字符,如果不写,字符串不做修改.\1 代 ...
- 【算法编程 C++ Python】根据前序遍历、中序遍历重建二叉树
题目描述 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的前序遍历和中序遍历的结果中都不含重复的数字.例如输入前序遍历序列{1,2,4,7,3,5,6,8}和中序遍历序列{4,7, ...
- 【算法编程 C++ python】单链表反序输出
题目描述 输入一个链表,从尾到头打印链表每个节点的值. 以下方法仅仅实现了功能,未必最佳.在牛客网测试, C++:3ms 480k Python:23ms 5732k /** * struct L ...
随机推荐
- python基础知识(七)---数据类型补充、"雷区"、编码
数据类型补充."雷区".编码 1.数据类型补充 str: #字符串数据类型补充 s1=str(123) #常用于类型转换 print(s1) #capitalize()首字母大写 ...
- js对象的深入理解(六)
一.对象的创建(四种模式)1.工厂模式工厂模式示例: function createPerson(name,age){ var obj = { name:name, age:age, sayName: ...
- Mysql慢查询日志以及优化
慢查询日志设置 当语句执行时间较长时,通过日志的方式进行记录,这种方式就是慢查询的日志. 1.临时开启慢查询日志(如果需要长时间开启,则需要更改mysql配置文件) set global slow_q ...
- Windows Server 2008 R2 + IIS 环境部署Asp.Net Core App
Windows + IIS 环境部署Asp.Net Core App 环境:Windows Server 2012, IIS 8, Asp.Net Core 1.1. 不少人第一次在IIS中部署A ...
- Jmeter计数器的使用-转载
说一下jmeter中,配置元件-计数器的使用. 如果需要引用的数据量较大,且要求不能重复或者需要自增,那么可以使用计数器来实现. 1.启动jmeter,添加线程组,右键添加配置元件——计数器,如下图: ...
- python(练习题)
1.请写出冒泡排序将list1进行排序? List1 = [1,2,34,12,33,25,12,33,90,28] 2.使用python语言打印出name=”I love python”的长度 3. ...
- vue中引入mui报Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them的错误
在vue中引入mui的js文件的时候,报如下的错误: 那是因为我们在用webpack打包项目时默认的是严格模式,我们把严格模式去掉就ok了 第一步:npm install babel-plugin-t ...
- 【linux-command】Chrome安装linux-command插件
一.linux-command是什么 550 多个 Linux 命令,内容包含 Linux 命令手册.详解.学习,值得收藏的 Linux 命令速查手册.Githb地址: https://github. ...
- Python_正则表达式语法
1.正则表达式中的操作符: 2.re库的使用: import re #search方法要求只要待匹配的字符串中包含正则表达式中的字符串就可以 match = re.search('python+',' ...
- window安装gcc、g++、make等编译环境
1. MinGW官网下载:http://www.mingw.org 点击右上角Downloads 点击下载 mingw-get-setup.exe 2. 百度网盘(2019年4月从官网下 ...