剑指offer 面试题4 替换空格
来源:互联网 发布:淘宝怎么开虚拟充值 编辑:程序博客网 时间:2024/06/06 09:27
面试题4 替换空格
主要思路:从后往前扫描,遇到空格就进行替换。
在C++中,是可以做到在原字符串的基础上修改的,就是不申请新的字符数组,达到节省空间的目的,但是java中字符数组的长度是固定的,所以必须得新申请一个字符数组。
要注意的是java中的字符串不是以\0结尾的。字符串长度就是实际长度,不需要加1.
public class ReplaceBlank {
public static void main(String[] args) {
String text = "We are happy.";
char[] temp = text.toCharArray();
int newlength = temp.length;
for (int i=0;i<temp.length;i++)
if (' '==(temp[i])) newlength+=2;
char[] newChar = new char[newlength];
int j=newlength-1;
for (int i=temp.length-1;i>=0;i--){
if (' '==(temp[i])){
newChar[j--] = '0';
newChar[j--] = '2';
newChar[j--] = '%';
}else {
newChar[j--] = temp[i];
}
}
System.out.println(new String(newChar));
}
}
0 0
- 剑指offer-->面试题4 替换空格
- 《剑指offer》面试题4 替换空格
- 【剑指offer】面试题4:替换空格
- 【剑指offer】面试题4替换空格
- 剑指offer 面试题4 替换空格
- 剑指Offer:面试题4 替换空格
- 剑指offer-面试题4-替换空格
- 《剑指Offer》面试题4:替换空格
- 【剑指offer】 面试题4: 替换空格
- 剑指offer 面试题4---替换空格
- 剑指offer面试题4-- 替换空格
- 【剑指offer】面试题4:替换空格
- 剑指Offer:面试题4 替换空格
- 剑指offer-面试题4:替换空格
- 剑指Offer 面试题4 替换空格
- 剑指offer-----面试题4(替换空格)
- 剑指offer面试题4 替换空格
- 剑指offer面试题4:替换空格
- iBatis简单入门教程
- 任务和特权级保护(一)
- request.getRemoteAddr() 获取的值为0:0:0:0:0:0:0:1的原因及解决办法
- Jenkins进阶系列之——15Maven获取Jenkins的Subversion的版本号
- 编码规范建议
- 剑指offer 面试题4 替换空格
- git
- QQ笔试
- 语音专题第三讲,语言模型
- u3d坐标系详解
- Jenkins进阶系列之——16一个完整的JENKINS下的ANT BUILD.XML文件
- is not translated in "en" (English)
- js使用注意事项
- ViewPager轮播图