Area: HotSpot
Synopsis: In JDK 7, interned strings are no longer allocated in the permanent generation of the Java heap, but are instead allocated in the main part of the Java heap (known as the young and old generations), along with the other objects created by the application. This change will result in more data residing in the main Java heap, and less data in the permanent generation, and thus may require heap sizes to be adjusted. Most applications will see only relatively small differences in heap usage due to this change, but larger applications that load many classes or make heavy use of the String.intern() method will see more significant differences.
String str1 = new String("hello world");
String str2 = "hello world";
public class StrSaveAddress {
private static final String str3 = "hello world";
public static void strMain() {
String str1 = new String("hello world");
String str2 = "hello world";
String str4 = str3;
String str5 = str3;
System.out.println(str1 == str2);
System.out.println(str1.equals(str2));
System.out.println(str4 == str5);
}
public static void main(String[] args) {
strMain();
}
}
//输出:
false
true
true
最简单的做法,循环遍历字符串,每次碰到空格先移动之后字符,在替换空格。如下图流程:
第一步找到第一个空格
第二步,移动空格之后的字符2个位置,将空格替换成%20,彩色部分是需要移动的字符
第三步之后的流程和第二步一样,一直到遍历到最后一个字符
实现方式如下:
/**
* @author liaojiamin
* @Date:Created in 18:30 2020/11/3
*/
public class StrReplace {
/**
* 时间复杂度O(n^2) 空间复杂度O(1)
* @return a
* @author: liaojiamin
* @Param str 目标字符串 '\0' 结束
* @Param replaceChar 需替换字符
* @param length 字符数组总长度
* @date: 19:10 2020/11/3
*/
public static char[] replaceFirst1(char[] str, char replaceChar, int length){
if(null == str || str.length == 0){
return str;
}
int originStrLength = 0;
int replaceStrCount = 0;
while(str[originStrLength] != '\0'){
originStrLength ++;
if(str[originStrLength] == replaceChar){
replaceStrCount ++;
}
}
int resultLength = replaceStrCount * 2 + originStrLength;
if(resultLength > length){
return str;
}
int i = 0;
while(str[i] != '\0'){
if(str[i] == ' '){
for (int j = originStrLength; j > i; j--) {
str[j+2] = str[j];
}
str[i++] = '%';
str[i++] = '2';
str[i++] = '0';
originStrLength+=2;
}else {
i++;
}
}
return str;
}
public static char[] getChar(String str, int length){
char[] chars = new char[length];
for (int i = 0; i < str.length(); i++) {
chars[i] = str.charAt(i);
}
return chars;
}
public static void main(String[] args) {
int length = 100;
char[] chars = getChar("this is my way", length);
System.out.println(new String(replaceFirst1(chars, ' ', length)));
}
新字符串中修改,也不难,我们先计算好替换之后需要的总内存,新申请合适的内存,接着只需要将老的字符串一个一个复制进去,碰到空格则替换成%20替换即可。如下步骤
第一步申请新的字符串计算结果长度,申请新容量内存空间
需要的总内存
第二步:遍历字符串,将字符按位复制进新的内存位置中,如果遇到空格则复制20%,如下图:
第三部:依第二步骤继续复制,得到最终结果:
实现方式如下:
public class StrReplace {
/**
* 时间复杂度O(n) 空间复杂度O(n)
* @return a
* @author: liaojiamin
* @Param str 目标字符串 '\0' 结束
* @Param replaceChar 需替换字符
* @param length 字符数组总长度
* @date: 19:10 2020/11/3
*/
public static char[] replaceFirst(char[] str, char replaceChar, int length){
if(null == str || str.length == 0){
return str;
}
int countStrNum = 0;
int i = 0;
while(str[i] != '\0'){
i++;
if(str[i] == replaceChar){
countStrNum ++;
}
}
if(countStrNum == 0){
return str;
}
int resultStrLength = i + countStrNum*2;
if(resultStrLength > length){
return str;
}
char[] resultStr = new char[resultStrLength];
int resultPosition = 0;
for (int j = 0; j < i; j++) {
char thisChar = str[j];
if(thisChar == replaceChar){
resultStr[resultPosition++]='%';
resultStr[resultPosition++]='2';
resultStr[resultPosition++]='0';
}else {
resultStr[resultPosition++] = thisChar;
}
}
return resultStr;
}
public static char[] getChar(String str, int length){
char[] chars = new char[length];
for (int i = 0; i < str.length(); i++) {
chars[i] = str.charAt(i);
}
return chars;
}
public static void main(String[] args) {
int length = 100;
char[] chars = getChar("this is my way", length);
System.out.println(new String(replaceFirst(chars, ' ', length)));
}
}
public class StrReplace {
/**
* 最优解:时间复杂度O(n) 空间复杂度O(1)
* @return a
* @author: liaojiamin
* @Param str 目标字符串 '\0' 结束
* @Param replaceChar 需替换字符
* @param length 字符数组总长度
* @date: 19:10 2020/11/3
*/
public static char[] replaceFirst2(char[] str, char replaceChar, int length){
if(null == str || str.length == 0){
return str;
}
int originStrLength = 0;
int replaceStrCount = 0;
while(str[originStrLength] != '\0'){
originStrLength ++;
if(str[originStrLength] == replaceChar){
replaceStrCount ++;
}
}
int resultLength = replaceStrCount * 2 + originStrLength;
if(resultLength > length){
return str;
}
int i = resultLength;
while (originStrLength >= 0){
if(str[originStrLength] == ' '){
str[i--] = '0';
str[i--] = '2';
str[i--] = '%';
}else {
str[i--] = str[originStrLength];
}
originStrLength --;
}
return str;
}
public static char[] getChar(String str, int length){
char[] chars = new char[length];
for (int i = 0; i < str.length(); i++) {
chars[i] = str.charAt(i);
}
return chars;
}
public static void main(String[] args) {
int length = 100;
char[] chars = getChar("this is my way", length);
System.out.println(new String(replaceFirst2(chars, ' ', length)));
}
}
public class SplicStr {
/**
* 时间复杂度O(n),空间复杂度O(1)
* @author: liaojiamin
* @description: 拼接两个排序的数组,target 足够容纳两个数组
* @date: 14:54 2020/11/4
*
* @return
*/
public static int[] splicStr(int[] target, int[] origin){
if(null == target || null == origin){
return null;
}
int targetLength = 0;
while (target[targetLength] != '\0'){
targetLength ++;
}
int resultLength = targetLength + origin.length;
if(target.length < resultLength){
return null;
}
int resultPosition = resultLength - 1;
int targetPosition = targetLength - 1;
int originPosition = origin.length - 1;
while (resultPosition >= 0){
if(targetPosition >= 0 && originPosition >= 0
&& target[targetPosition] > origin[originPosition]){
target[resultPosition--] = target[targetPosition--];
}else {
target[resultPosition--] = origin[originPosition--];
}
}
return target;
}
public static void main(String[] args) {
int[] target = new int[20];
int[] origin = new int[10];
for (int i = 0; i < 10; i++) {
target[i] = i+300;
origin[i] = i*10;
}
int[] targetInt = splicStr(target, origin);
for (int i : targetInt) {
System.out.print(i + " ");
}
}
}
上一篇:数据结构与算法–数组:二维数组中查找
下一篇:数据结构与算法–排序算法总结