Codewars题记 :Find the missing letter

1、题目:

#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'
(Use the English alphabet with 26 letters!)

Have fun coding it and please don't forget to vote and rank this kata! :-)

I have also created other katas. Take a look if you enjoyed this kata!

2、我的解决方案

 1 class Kata {
 2     public static char findMissingLetter(char[] array) {
 3         int nextASCLL = (int)array[0];
 4         
 5         int lostChar = 0;
 6         
 7         for (int i = 0; i < array.length; i++) {
 8             if ((int)array[i]!=nextASCLL) {
 9                 lostChar = nextASCLL;
10                 break;
11             }
12             nextASCLL++;
13         }
14         
15         return (char)lostChar;
16     }
17 }

3、最佳解决方案

 1 public class Kata
 2 {
 3   public static char findMissingLetter(char[] array){
 4     char expectableLetter = array[0];
 5     for(char letter : array){
 6       if(letter != expectableLetter) break;
 7       expectableLetter++;
 8     }
 9     return expectableLetter;
10   }
11 }

4、总结

  这个题目主要是对char的ascll进行处理。

  每次看最佳解决方案都能学习到最简单的写法。

转载于:https://www.cnblogs.com/RivenLw/p/11284965.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值