用的java public class Solution { public String replaceSpace(StringBuffer str) { StringBuffer out=new StringBuffer(); for (int i = 0; i < str.toString().length(); i++) { char b=str.charAt(i); if(String.valueOf(b).equals(" ")){ out.append("%20"); }else{ out.append(b); } } return out.toString(); } }
public class Solution { public String replaceSpace(StringBuffer str) { String sti = str.toString(); char[] strChar = sti.toCharArray(); StringBuffer stb = new StringBuffer(); for(int i=0;i<strChar.length;i++){ if(strChar[i]==' '){ stb.append("%20"); }else{ stb.append(strChar[i]); } } return stb.toString(); } }
class Solution { public: void replaceSpace(char *str,int length) { string s(str); int i=0; while((i=s.find(' ',i))>-1){ s.erase(i,1); s.insert(i,"%20"); } auto ret=s.c_str(); strcpy(str,ret); } };
};
第二遍自己做发现了更简单的办法哟~ public class Solution { public String replaceSpace(StringBuffer str) { String s = str.toString(); if(str==null) return s; char []ss=s.toCharArray(); StringBuffer sb = new StringBuffer(); for(int i=0;i<ss.length;i++) { if(ss[i]==' ') { sb.append("%20"); } else sb.append(ss[i]); } return sb.toString(); } }
class Solution { public: void replaceSpace(char *str,int length) { int spaceNum=0; for(int i=0;i<length;i++) { if(str[i]==' ') spaceNum++; } int newIndex=length+2*spaceNum; char *index=str+length; while(index>=str) { if(*index==' ') { str[newIndex--]='0'; str[newIndex--]='2'; str[newIndex--]='%'; } else{ str[newIndex--]=*index; } index--; } } };
//JAVA的同学看这里。我用JAVA按照C++的思路写了一份,原理和思路,我这里用了输出语句展示替换过程。其他老哥已经讲得很清楚了
class Solution { public: void replaceSpace(char *str,int length) { int count=0; for(int i=0;i<length;i++) { if(str[i]==' ') count++; } int newLength=length+2*count; for(int i=length-1;i>=0;i--) { if(str[i]!=' ') { str[i+2*count]=str[i]; } else { str[i+2*count]='0'; str[i+2*count-1]='2'; str[i+2*count-2]='%'; count--; } } } };
public class Solution {
public String replaceSpace(StringBuffer str) {
StringBuffer st = new StringBuffer();
for(int i = 0; i < str.length() ; i++){
char s = str.charAt(i);
if(String.valueOf(s).equals(" ")){
st.append("%20");
}
else{
st.append(s);
}
}
return st.toString();
}
}
package alex.suda.jzOffer; import java.util.Scanner; public class ReplaceSpace { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Scanner scanner = new Scanner(System.in); while (scanner.hasNext()) { String s = scanner.nextLine(); StringBuffer str = new StringBuffer(s); System.out.println(replaceSpace(str)); } } public static String replaceSpace(StringBuffer str) { if (str == null) { return null; } int blankNum = 0; int length = str.length(); int newLength = 0; for (int i = 0; i < length; i++) { if (str.charAt(i) == ' ') { blankNum++; } } newLength = length + 2 * blankNum;//替换后的字符串长度 char[] newChars = new char[newLength];//新的字符数组 int index = newLength - 1; for(int i=length-1;i>=0;i--) { if (str.charAt(i) == ' ') { newChars[index--] = '0'; newChars[index--] = '2'; newChars[index--] = '%'; } else { newChars[index--] = str.charAt(i); } } return new String(newChars); } }
package HuaWei; public class Replace { /** * 请实现一个函数,将一个字符串中的空格替换成“%20”。 * 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。 */ public static void main(String[] args) { String s = "We Are Happy"; re(s); } public static void re(String s ){ String[] all = s.split(" "); StringBuffer sb = new StringBuffer(); StringBuffer sb1 = new StringBuffer(); for(int i = 0;i<all.length;i++){ if(i!=all.length-1){ sb1.append(all[i]+"%20"); } else sb1.append(all[i]); } System.out.println(sb.append(sb1)); } }