Top 150 Questions - 1.5
来源:互联网 发布:工程设计优化方法手段 编辑:程序博客网 时间:2024/05/04 12:29
1.5 Write a method to replace all spaces in a string with ‘%20’.
package Question1_5;public class Question1_5{public static void main(String[] args){Question1_5 q = new Question1_5();System.out.println(q.ReplaceSpace(new String("abcdef").toCharArray()));System.out.println(q.ReplaceSpace(new String("abc d e f").toCharArray()));System.out.println(q.ReplaceSpace(new String(" ").toCharArray()));System.out.println(q.ReplaceSpace(new String(" ").toCharArray()));}public char[] ReplaceSpace(char[] str){// considering the worst case, where all the elements are space, we must have an array whose// size is three times the input array size. If memory size is a main consideration, we can// first compute the number of spaces. char[] strClone = new char[str.length * 3 + 1];int i, j;for (i = 0, j = 0; i < str.length; i++){if (str[i] != ' ')strClone[j++] = str[i];else{strClone[j++] = '%';strClone[j++] = '2';strClone[j++] = '0';}}// j is at the end of strClonestrClone[j] = '\0';return strClone;}}
- Top 150 Questions - 1.5
- Top 150 Questions - 1.1
- Top 150 Questions - 1.2
- Top 150 Questions - 1.3
- Top 150 Questions - 1.4
- Top 150 Questions - 1.6
- Top 150 Questions - 1.7
- Top 150 Questions - 1.8
- Top 150 Questions - 2.1
- Top 150 Questions - 2.2
- Top 150 Questions - 2.3
- Top 150 Questions - 2.4
- Top 150 Questions - 2.5
- The Top HR Questions
- Top SQL Questions
- Leetcode TOP Questions
- top interview questions 1
- top interview questions 2
- 黑马程序员 HTML总结(一)
- poj 1376 Robot
- 在cloudfound上部署自己的应用
- 解决SQL语句中含有中文字符无法查询问题
- 首届华中区程序设计邀请赛暨第十届武汉大学程序设计大赛网络预赛I题(HDOJ-4223)
- Top 150 Questions - 1.5
- Foxmail 7.0版本怎么让导入的Rss 地址自动分组。
- eclipse如何设置代码自动完成为全字符
- Linux进程间通信-FIFO管道
- 获取Android的Java源代码并在Eclipse中关联查看
- 配置serv-u
- user management of linux
- xor的性质和运算
- NodeJs中的express框架获取http参数