codewars find the missing letter

#Find the missing letter

Write a method that takes an array of consecutive (increasing) letters as input and that returns the missing letter in the array.

You will always get an valid array. And it will be always exactly one letter be missing. The length of the array will always be at least 2.
The array will always contain letters in only one case.

Example:

['a','b','c','d','f'] -> 'e'
['O','Q','R','S'] -> 'P'

将数组中每一位和它上一位assic+1的字符比较(从第二位开始),若不相等,first+1对应的字母即为缺少的字母

public class Kata
{
  public static char findMissingLetter(char[] array)
  {
   int first =  array[0];//初始first 为第一个字母的assic码
   for(int i = 0; i< array.length; ){
    while((char)(first+1) == array[i+1]){
    first += 1;
    i++;
    }
    first++;
    break;
   }
   return (char)first;
  }
}

猜你喜欢

转载自blog.csdn.net/qq_37393071/article/details/79839561