If1, 2, 3, 4, 4, 5, 5, 6, 7, 8--True
This is with a hashtable,key is a number, value is the number of occurrences
Then traverse the original array, each number will be the hash from the beginning of their own 5 color number is 1, if the missing number indicates that cannot be divided
It's easy to be wrong!
Wrong many times, is the color of the next 5, if there is no color or the number of color is 0, error
1 Packagestraight;2 ImportJava.util.*;3 4 Public classSolution {5 Public BooleanDetermine (int[] arr) {6Hashmap<integer, integer> map =NewHashmap<integer, integer>();7 for(intElem:arr) {8 if(Map.containskey (elem)) {9Map.put (Elem, Map.get (elem) +1);Ten } One ElseMap.put (Elem, 1); A } - - for(inti=0; i<arr.length; i++) { the if(Map.get (arr[i]) = = 0)Continue; - for(intJ=arr[i]; j<arr[i]+5; J + +) { - if(!map.containskey (j))return false; - if(Map.get (j) = = 0)return false; + Else { -Map.put (J, Map.get (j)-1); + } A } at if(Map.get (arr[i]) > 0) i--; - } - return true; - } - - in /** - * @paramargs to */ + Public Static voidMain (string[] args) { - //TODO auto-generated Method Stub theSolution Sol =Newsolution (); * Booleanres = Sol.determine (New int[]{1,2,3,4,4,5,5,5,6,6,7,7,8,8,9}); $ if(RES) System.out.println ("true");Panax Notoginseng ElseSystem.out.println ("false"); - } the +}
G-Face by Prepare:straight Partition of A Deck of Cards