替换字符串中的空格为$$$。要求时间复杂度为O(N)
来源:互联网 发布:图像清晰度检测算法 编辑:程序博客网 时间:2024/05/16 17:55
替换字符串中的值,如果是一个换一个,那么我们在遍历的时候就恶意直接替换,以为没有多余空间的使用,但是现在要将一个空格,换为三个‘
#define _CRT_SECURE_NO_WARNINGS 10#include<iostream>using namespace std;void replace(char str[],int length){ int i = 0; int num = 0; while (str[i] != '\0') { if (str[i] == ' ') num++; i++; } int newlength = length + 2 * num; if (newlength <= length) return; int indexold = length; int indexnew = newlength; while (indexold >= 0 && indexnew>indexold) { if (str[indexold] == ' ') { str[indexnew--] = '$'; str[indexnew--] = '$'; str[indexnew--] = '$'; } else str[indexnew--]=str[indexold]; indexold--; } printf("%s\n", str);}int main(){ char str[100] = "talk is cheap show me the code"; int len = strlen(str); replace(str,len); system("pause"); return 0;}
阅读全文
0 0
- 字符串替换--替换字符串中的空格为$$$。要求时间复杂度为O(N)
- 替换字符串中的空格为$$$。要求时间复杂度为O(N)
- 替换字符串中的空格为$$$。要求时间复杂度为O(N)
- 【每日一题】替换字符串中的空格为$$$。要求时间复杂度为O(N)
- 查找一个字符串中第一个只出现两次的字符,要求时间复杂度为O(N)。-替换字符串中的空格为$$$。要求时间复杂度为O(N)
- 将字符串中的空格替换,时间复杂度为0(n)解法
- 将字符串中的所有空格去掉,要求时间复杂度O(N)
- 剑指offer,时间复杂度为o(n)的空格替换算法,44页例子代码
- 排序一个数组要求时间复杂度为O(N)
- 【每日一题】查找一个字符串中第一个只出现两次的字符。要求时间复杂度为O(N),空间复杂度为O(1)
- 给定一个字符串,查找该字符串中第一个重复出现的字符索引。要求:时间复杂度为O(n)。
- 字符串左旋满足时间复杂度为O(n)
- 时间复杂度为O(n),空间复杂度为O(1)
- 要求对数组a进行排序,要求时间复杂度为O(N)
- 时间复杂度为O(n)的排序
- 时间复杂度为O(n)的排序
- poj 3061 时间复杂度为O(n)
- 【字符串】查找一个字符串中第一个只出现两次的字符。比如:“abcdefabcdefabc”中第一个只出现两次为‘d’,要求时间复杂度为O(N),空间复杂度为O(1)
- idea主题设置
- 如何避免点击事件被执行多次
- 访问Hbase,待更新方法一
- PAT (Advanced Level) Practise 1038 Recover the Smallest Number (30)
- 尝试加载oracle客户端时引发BadImageFormatException.如果在安装32位Oracle客户端组件的情况下以64位模式运行
- 替换字符串中的空格为$$$。要求时间复杂度为O(N)
- CSDN博客积分规则和获取积分方法
- HTML&CSS精选笔记_浮动与定位
- UML之行为图
- 51Nod 1091 线段的重叠
- freebsd 与 nginx 与 openssl编译 http2
- commons-io使用笔记
- 《剑指offer》孩子们的游戏---约瑟夫问题
- java--public_private_void_static功能作用