【算法编程 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 ...
随机推荐
- vue-cli + webpack 环境搭建
1.下载nodeJS,官网 https://nodejs.org/en/ . 2.安装nodeJS.安装完成后可以检测node -v 如果版本号的话则正常. 3.安装淘宝镜像.npm install ...
- element-ui select多选情况下获取label和value
直接上代码 <el-select v-model="value" multiple collapse-tags ref="select" @change= ...
- vue-quill-edito 字体倾斜加粗无效
长话短说,出现这种情况的原因80%-90%的概率在你项目里面有一个全局的 一般在reset.css重置文件中 font-weight:normal; font-style:normal; font-s ...
- 【转载】利用tasker推送手机短信到企业微信(App或者微信公众号)
本随笔转载自:https://www.52pojie.cn/thread-804477-1-1.html 转载声明:本文转自酷安tasker评论区 ID:随风荡 的评论,我只是按照我的需求进行了一次修 ...
- Gerrit代码评审流程
Gerrit代码评审流程 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.代码评审流程(如下图所示) 第一步:贡献者报建一个提交,并通过之前从gerrit下载的commit-ms ...
- C++(五十一) — 容器中常见算法(查找、排序、拷贝替换)
1.find(); find()算法的作用是在指定的一段序列中查找某个数,包含三个参数,前两个参数是表示元素范围的迭代器,第三个参数是要查找的值. 例:fing(vec.begin(), vec.en ...
- robotframework+Python3.7 接口自动化测试
具体的测试用例,password,channel,resultCode传给接口描述 集成了一些常见的测试接口方法 1. Get请求下,
- python内置模块笔记(持续更新)
常用函数name = '{wh}my \t name is {name},age is {age}.' print(name.capitalize()) # 字符串的开头字母大写 print(name ...
- Linux配置静态IP以及解决配置静态IP后无法上网的问题
式一.图形界面配置
- LeetCode 826. Most Profit Assigning Work
原题链接在这里:https://leetcode.com/problems/most-profit-assigning-work/ 题目: We have jobs: difficulty[i] is ...