['a','b','c','d','f'] -> 'e'
['O','Q','R','S'] -> 'P'
找出陣列中跳過的英文字母
想法:
已知char a~z & A~Z 對應 int 97~122 & 65~90
故可透過找到跳過的int數值再轉成char輸出
public class Kata
{
public static char findMissingLetter(char[] array)
{
char lossWord=' ';
for (int i=0;i< array.length;i++){
if(((int)array[i]+1)==array[i+1]){
}else {
lossWord= (char)((int)array[i]+1);
return lossWord;
}
}
return lossWord;
}
}
另種採用foreach循環
public class Kata
{
public static char findMissingLetter(char[] array){
char expectableLetter = array[0];
for(char letter : array){
if(letter != expectableLetter) break;
expectableLetter++;
}
return expectableLetter;
}
}