題目
創建一個函數,該函數將返回一個字符串,該字符串組合了三個輸入字符串的所有字母。取所有輸入的第一個字母并將它們彼此相鄰分組。為每個字母執行此操作,請參閱下面的示例!
例)輸入:“aa”,“bb”,“cc”=>輸出:“abcabc”
注意:您可以預期所有輸入的長度都相同。
測試用例:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class TripleExampleTests {
@Test
public void tests() {
assertEquals("ttlheoiscstk", Kata.tripleTrouble("this", "test", "lock"));
assertEquals("abcabc", Kata.tripleTrouble("aa","bb","cc"));
assertEquals("Batman", Kata.tripleTrouble("Bm", "aa", "tn"));
assertEquals("LexLuthor", Kata.tripleTrouble("LLh","euo","xtr"));
}
}
解題
My
public class Kata {
public static String tripleTrouble(String one, String two, String three) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i<one.length(); i++) {
sb.append(one.charAt(i)).append(two.charAt(i)).append(three.charAt(i));
}
return sb.toString();
}
}
Other
public class Kata {
public static String tripleTrouble(String s1, String s2, String s3) {
StringBuilder sb = new StringBuilder(s1.length()*3);
for (int i=0; i<s1.length(); i++) {
sb.append(s1.charAt(i)).append(s2.charAt(i)).append(s3.charAt(i));
}
return sb.toString();
}
}
后記
我發現我有點生疏了,不難的代碼還在想寫法應該是這樣吧,時間是記性的大敵啊。