【待】1.4 Write a method to replace all spaces in a string with'%20'.
2015-09-16 21:07
531 查看
Write a method to replace all spaces in a string with'%20'. You may assume that the string has sufficient space at the end of the string to hold the dditional
characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
EXAMPLE
Input: "Mr John Smith"
Output: "Mr%20Dohn%20Smith"
【初步思路】:
新建一个字符数组,大小为原字符串的三倍, 然后遍历原字符串,将相应元素置入数组(如果是空格则用%20代替)
暂时没想到 in-place 的方法。
【时间复杂度】:O(n)
【空间复杂度】:O(n)
【优化】:in-place 方法。
先遍历一遍数组(原数组真实长度设为 len), 求得空格个数 count。则修改后数组长度应为 len+2*count。然后采用两个指针,从右至左遍历数组,不是空格则拷贝置新位置,是空格则修改为 "%20".
【时间复杂度】:O(n)
【空间复杂度】:O(1)
附上 test code
2015-09-16
characters, and that you are given the "true" length of the string. (Note: if implementing in Java, please use a character array so that you can perform this operation in place.)
EXAMPLE
Input: "Mr John Smith"
Output: "Mr%20Dohn%20Smith"
【初步思路】:
新建一个字符数组,大小为原字符串的三倍, 然后遍历原字符串,将相应元素置入数组(如果是空格则用%20代替)
暂时没想到 in-place 的方法。
【时间复杂度】:O(n)
【空间复杂度】:O(n)
public char[] solu(char[] s) { char[] result = new char[s.length * 3]; for (int i = 0, j = 0; i < s.length; i++) { int ascii = s[i]; if (ascii == 32 ) { result[j++] = '%'; result[j++] = '2'; result[j++] = '0'; } else { result[j] = s[i]; j++; } } return result; }
【优化】:in-place 方法。
先遍历一遍数组(原数组真实长度设为 len), 求得空格个数 count。则修改后数组长度应为 len+2*count。然后采用两个指针,从右至左遍历数组,不是空格则拷贝置新位置,是空格则修改为 "%20".
【时间复杂度】:O(n)
【空间复杂度】:O(1)
public class Solution { public static int solu(char[] s, int len) { int count = 0; for (int i = 0; i < len; i++) { if (s[i] == ' ') { count++; System.out.println(count); } } int oldPointer = len - 1; int newPointer = len + 2 * count - 1; while(oldPointer != newPointer) { if (s[oldPointer] == ' ') { s[newPointer--] = '0'; s[newPointer--] = '2'; s[newPointer--] = '%'; oldPointer--; } else { s[newPointer--] = s[oldPointer--]; } } return count; // useful for test; } }
附上 test code
import static org.junit.Assert.*; import org.junit.Test; public class SolutionTest { @Test public void testCount() { char[] s1 = helper(""); char[] s2 = helper("ab"); char[] s3 = helper(" "); char[] s4 = helper(" a"); assertTrue(Solution.solu(s1, 0) == 0); assertTrue(Solution.solu(s2, 2) == 0); assertTrue(Solution.solu(s3, 1) == 1); assertTrue(Solution.solu(s4, 2) == 1); char[] s_1 = {}; char[] s_2 = {'a', 'b'}; char[] s_3 = {'%', '2', '0'}; char[] s_4 = {'%', '2', '0', 'a'}; assertTrue(equal(s1, s_1)); assertTrue(equal(s2, s_2)); assertTrue(equal(s3, s_3)); assertTrue(equal(s4, s_4)); } public static boolean equal(char[] s1, char[] s2) { for (int i = 0; i < s2.length; i++) { if (s1[i] != s2[i]) { return false; } } return true; } public static char[] helper(String s) { char[] c = new char[100]; for (int i = 0; i < s.length(); i++) { c[i] = s.charAt(i); } return c; } }
2015-09-16
相关文章推荐
- hdu 4540 威威猫系列故事——打地鼠
- Java中的“别名现象”
- 域名那些事儿
- Oracle学习之常见问题处理
- hdu 5446
- 从评教移动端分析MVC
- 关于cocos2d-js中使用 ClippingNode 以及 BlendFunc 来实现遮罩
- matlab中简单的图像旋转算法
- 获取Mac地址
- Android自定义View之三种流行进度条的写法
- hdu 5446
- CSS 基础
- this 指针
- HDU 1536 S-Nim(sg函数)
- javaEE --- Hibernate
- Effective C++——条款43(第7章)
- NetworkRequest 网络请求
- 前端之路
- Java多线程与并发库高级应用 学习笔记 1-9课
- 更改AD查询LDAP条目的1000限制