ある弦を別の弦の真ん中に突き刺してから、新しい弦の真ん中に入れようとしてMonkey
います。
String One = "MonkeyPony";
String Two = "Monkey";
基本的に私がやろうとしているのは、何度も"Monkey"
真ん中に挿入することです."MonkeyPony"
"MonkeMonkeyyPony"
"MonkeMonMonkeykeyyPony"
ある弦を別の弦の真ん中に突き刺してから、新しい弦の真ん中に入れようとしてMonkey
います。
String One = "MonkeyPony";
String Two = "Monkey";
基本的に私がやろうとしているのは、何度も"Monkey"
真ん中に挿入することです."MonkeyPony"
"MonkeMonkeyyPony"
"MonkeMonMonkeykeyyPony"
StringBuilder を使用します。
StringBuilder builder = new StringBuilder(One);
for (int i = 0; i < 10; ++i) {
builder.insert(builder.length() / 2, Two);
System.out.println(builder.toString());
}
あなたはこれを試すことができます:
One = One.substr(0,One.length()/2)+Two+One.substr(One.length()/2+1, One.length();
文字列連結の最初の要素は、文字列Oneの前半を取り、次に単語Twoと連結して、残りの1つを追加します。
この質問があなたの実際の仕事からのものなのか、宿題/面接アルゴリズムの質問なのかはわかりません。
簡単な解決策は、元の文字列のインデックスを計算し、その位置に新しい文字列を挿入することです。これをn回行います。
私はアイデアを書いただけですが、それが大丈夫かどうかはわかりません。100(n)回挿入したいと言ってください。
Monkey
"Mon"
99回繰り返され(n-1)、もう1つは"key"
99回繰り返されますi
i
この文字列を次の位置に挿入します。99Mons + String TWO("Monkey") + 99keys
これが良い方法かどうかはわかりません...
私は自分のソリューションのパフォーマンスに興味があるので、@ Ame Burmeisterの簡単なソリューションを使用して、小さなパフォーマンステストを行いました。
私のテストでは:
Junit Test class
StopWatch
グアバからですwarming up
経過時間を測定する前に4回、回避するJit
100000
両方とも挿入でテストされました@Test
public void insertWithBuilder() {
final int n = 100000;
final String one = "MonkeyPony";
final String two = "Monkey";
final Stopwatch sw = new Stopwatch();
int x = 1;
for (; x <= 5; x++) {// loop 4 times (warming up) to avoid JIT
if (x == 5) {
sw.start();
}
final StringBuilder builder = new StringBuilder(one);
System.out.println("warming up times:" + x);
for (int i = 0; i < n; ++i) {
builder.insert(builder.length() / 2, two);
}
if (x == 5) {
sw.stop();
// System.out.println("builder:" + builder.toString());
}
}
System.out.println("SBuilder:" + sw.elapsedTime(TimeUnit.MILLISECONDS));
}
@Test
public void insertWithRepeatString() {
final int n = 100000;
final String one = "MonkeyPony";
final String two = "Monkey";
final Stopwatch sw = new Stopwatch();
int x = 1;
for (; x <= 5; x++) { // loop 4 times (warming up) to avoid JIT
System.out.println("warming up times:" + x);
if (x == 5) {
sw.start();
}
final StringBuilder builder = new StringBuilder(one);
final int m = two.length() / 2;
final String h1 = two.substring(0, m); //get Mon
final String r1 = two.substring(m); //get Key
final String head = new String(new char[n - 1]).replace("\0", h1); //build repeat string
final String tail = new String(new char[n - 1]).replace("\0", r1); //build repeat String
final StringBuilder builder2 = new StringBuilder(head);
builder2.append(two).append(tail);
builder.insert(builder.length() / 2, builder2);
if (x == 5) {
sw.stop();
// System.out.println("builder:" + builder.toString());
}
}
System.out.println("Idea:" + sw.elapsedTime(TimeUnit.MILLISECONDS));
}
warming up times:1
warming up times:2
warming up times:3
warming up times:4
warming up times:5
Idea:41
warming up times:1
warming up times:2
warming up times:3
warming up times:4
warming up times:5
SBuilder:4413
このソリューションは単純かもしれませんが、他のソリューションと比較すると膨大な時間がかかります。
ここでは部分文字列メソッドが使用されています。
import java.util.Scanner;
public class test {
public static void main(String[] args) {
int n=0;
String str1,str2=null;
Scanner in = new Scanner(System.in);
System.out.println("Enter the String1.!");
str1=in.nextLine();
System.out.println("Enter the String2.!");
str2=in.nextLine();
System.out.println("Enter the Count.!");
n=Integer.parseInt(in.nextLine());
in.close();
for(int i=0;i<n;i++)
{
//Concept: Say str1=TEST str2=123, Now the below code will do this TE + 123 + ST
str1=str1.substring(0,str1.length()/2) + str2 + str1.substring(str1.length()/2);
System.out.println(str1);
}
}
}