/**
* Return the smallest character that's strictly larger than the search character,
* If No such character exists, return the smallest character in the array
* @param sortedstr:sorted List of letters, sorted in ascending order.
* @param c:character for which, we are searching.
* Given The following inputs we expect the corresponding output:
* [' C ', ' F ', ' j ', ' P ', ' V '], ' a ' = ' C '
* [' C ', ' F ', ' j ', ' P ', ' V '], ' c ' = ' f '
* [' C ', ' F ', ' j ', ' P ', ' V '], ' k ' = ' P '
* [' C ', ' F ', ' j ', ' P ', ' V '], ' z ' = ' C '//The Wrap around case
* [' C ', ' f ', ' K '], ' f ' = ' k '
* [' C ', ' f ', ' K '], ' c ' = ' f '
* [' C ', ' f ', ' K '], ' d ' = ' f '
*/
There seems to be a bug
Importjava.awt.List;Importjava.util.ArrayList; Public classsolution{ Public Static CharFindnextchar (Char[] list,Charc) {if(List = =NULL|| List.length = = 0) Throw NewIllegalArgumentException ("Null or empty list!"); intStart = 0; intEnd = List.length-1; if(c < list[0] | | c >= list[list.length-1]) returnList[0]; while(Start <end) { intMid = (start + end)/2; if(c = =List[mid]) { if(List[mid + 1] >c)returnList[mid + 1]; ElseStart= Mid + 1; } Else if(C <List[mid]) {End= Mid-1; } ElseStart= Mid + 1; } if(List[start] = =c)returnList[start + 1]; returnList[start]; } Public Static voidMain (string[] args) {Char[] list = {' C ', ' F ', ' j ', ' P ', ' V '}; Char[] target = {' A ', ' C ', ' f ', ' k ', ' V ', ' Z '}; for(CharC:target) System.out.println (c + "+" +Findnextchar (list, c)); }}
http://www.careercup.com/question?id=5726366532108288
[?] *smallest Character strictly larger than the Search Character