Created
June 16, 2014 18:23
-
-
Save k1xme/a833f1d440a33843c0ad to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution{ | |
// Space complexity: O(n); | |
// Time complexity: O(n); | |
public static char[] replaceSpaces(char[] str){ | |
if(str == null) return null; | |
if(str.length == 0 ) return str; | |
int length = 3 * str.length; // Triple its original size to ensure enough space to hold the result. | |
int i = 0; // The index of current character. | |
char[] result = new char[length]; // Create a new array to store the result string. | |
for(char c: str){ | |
if(c == " ") { | |
temp[i++] = "%"; | |
temp[i++] = "2"; | |
temp[i++] = "0"; | |
} | |
else{ | |
temp[i++] = c; | |
} | |
} | |
return temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment