gpt4 book ai didi

java - 不循环替换索引

转载 作者:行者123 更新时间:2023-11-30 07:38:23 28 4
gpt4 key购买 nike

我的问题是:

首先我有 1000 个字符的字符串变量,还有另一组 1000 个字符的字符串变量
1]在第一组变量中包含“1110000XXXXX0001111....”像这样等等,直到1000

2] 在第二组变量中包含“1110000101010001111...”,如此类推,直到 1000

3]我需要获取第一个变量中X的位置并替换第二个变量中类似位置的值

例如:第一个数据变量“000XXX000X0” 第二个数据变量“00011000010”X 应替换为第二组数据中的位置值。

注意:要在没有循环的情况下完成因为如果我们将循环放在一个循环中运行 1000 次,并且“X”可能位于字符串中 1000 个字符中的任何位置

例如:1 条记录 1000 次
如果 100K 记录意味着 1000*100K(性能失败)

所以需要解决方案。

请帮我解决这个问题。

我的代码是:

 String    sInputStr="0X11XXXXX000000000000000000000000000000000000000000000000X000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";

String sDbStr="0111111110000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000011";


int iLength=sInputStr.length();
for(int i=0;i<iLength;i++){
if(sInputStr.charAt(i)=='X'){

}else{
if(i>sDbStr.length()){
break;
}else{
sChar[i] = sInputStr.charAt(i);
}
}
}//End of For
sVal=String.valueOf(sChar);
System.out.println("sVal == " +sVal);

friend 们帮帮我

最佳答案

你所需要的就是这样

    class FirstApp {
public static void main(String[] args) {
String sDbStr="0111111110000001234000000000000011";
StringBuilder sNewStr= new StringBuilder("011111111000000XXXX00000000000001112");
String findStr = "X";
int lastIndex = 0;
System.out.println("Starting");
long startTime = System.currentTimeMillis();
String result = replaceValues("X", sDbStr, sNewStr);
long endTime = System.currentTimeMillis();
System.out.println("Result");
System.out.println(result);
System.out.println(String.valueOf(endTime-startTime));
}

public static String replaceValues(String toReplace, String fromStr, StringBuilder toStr) {
int lastIndex = toStr.indexOf(toReplace);
if(lastIndex != -1){
toStr.replace(lastIndex,lastIndex+1,Character.toString(fromStr.charAt(lastIndex)));
System.out.println(toStr);
return replaceValues(toReplace, fromStr, toStr);
} else {
return toStr.toString();
}
}
}

示例结果:

Starting
0111111110000001XXX00000000000001112
01111111100000012XX00000000000001112
011111111000000123X00000000000001112
011111111000000123400000000000001112
Result
011111111000000123400000000000001112

更新更新了解决方案,以确保使用 stringBuilder 和递归减少执行时间

关于java - 不循环替换索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35054294/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com