牛客网剑指offer-替换空格
来源:互联网 发布:java经典书籍 编辑:程序博客网 时间:2024/06/08 19:00
package www.niuke.offer;/* * 请实现一个函数,将一个字符串中的空格替换成“%20”。 * 例如,当字符串为We Are Happy. * 则经过替换之后的字符串为We%20Are%20Happy。 */public class test02 {public String replaceSpace(StringBuffer str) {int len = str.length();// 字符串的长度int count = 0;// 字符串中的空格的个数for (int i = 0; i < len; i++) {if (str.charAt(i) == ' ')count++;}int newlen = len + 2 * count;int index = newlen - 1;char[] ptr = new char[newlen];while (len > 0) {if (str.charAt(len - 1) != ' ') {ptr[index--] = str.charAt(len - 1);} else {ptr[index--] = '0';ptr[index--] = '2';ptr[index--] = '%';}--len;}return new String(ptr);}public static void main(String[] args) {String s = "We Are Happy";StringBuffer str = new StringBuffer(s);System.out.println(new test02().replaceSpace(str));}}
阅读全文
0 0
- 牛客网剑指offer-替换空格
- 牛客网剑指offer-02替换空格
- 牛客网剑指offer编程题--替换空格
- [剑指offer]替换空格
- [剑指offer]替换空格
- 剑指offer:替换空格
- 替换空格--《剑指offer》
- 剑指Offer:替换空格
- 【剑指offer】替换空格
- 《剑指Offer》--替换空格
- 剑指offer--替换空格
- 【剑指offer】替换空格
- 【剑指offer】空格替换
- 剑指offer-替换空格
- 剑指offer:替换空格
- [剑指offer]替换空格
- 《剑指offer》替换空格
- 剑指offer:替换空格
- Android系列之音乐播放器的实现一(播放网络音频文件)
- 求合法帧
- 在ElasticSearch之下(深入理解Shard和Lucene Index)
- 卷积神经网络之目标检测总结
- 关于部署基于Office 365的ADFS的进一步探讨
- 牛客网剑指offer-替换空格
- Python起步之数据结构和算法
- 消息中间件
- 关于蓝牙权限的获取与开启
- cocos2d-x + vs2015 android游戏开发环境搭建 及 新建项目
- 带头结点的单链表和不带头结点的单链表的倒数第K个节点
- Find The Multiple
- BZOJ 1036: [ZJOI2008]树的统计Count (LCT题解)
- 排序二:希尔排序