Colleagues go to the interview to ask for the maximum and the substring, for example to give you a positive negative array, 1-1 2-4 5 6-3, its largest and is 5 6 composed of substrings and 11. According to its isolation, there is no such structure within any substring 2-4, because if the substring is included, it must not be the maximum substring (2-4<0), so according to this nature, the array can be segmented, and the partition of the flag is less than 0 of the structure.
1 Public Static voidMain (string[] args) {2 3 int[] Array = {1,2,-4,3,5,-4,7,8,2,-1,6,-5,-200,3,47,-2};4 5 System.out.println (Findmaxvalue (array));6 7 //find the maximum substring8 int[] Subarray =Findmaxsubarray (array);9 for(intTmp:subarray) {TenSystem.out.print (tmp+ ""); One } A - } - the /** - * @Description: Find the maximum substring and - * @paramArray - * @return + * @returType: int - */ + Public Static intFindmaxvalue (int[] Array) { A intMax = 0; at intsum = 0; - for(inti=0;i<array.length;i++){ -sum = sum +Array[i]; - if(Sum >max) { -Max =sum; - in}Else if(sum<0){ -sum = 0; to } + } - returnMax; the } * $ /**Panax Notoginseng * @Description: Find the maximum substring of a string - * @paramArray the * @return + * @returType: int[] A */ the Public Static int[] Findmaxsubarray (int[] Array) { + intMax = 0; - intsum = 0; $ intStart = 0; $ intEnd = 0; - intTmpstart = 0; - for(inti=0;i<array.length;i++){ thesum = sum +Array[i]; - if(Sum >max) {WuyiMax =sum; theStart =Tmpstart; -End =i; Wu}Else if(sum<0){ -sum = 0; AboutTmpstart =i; $ } - } - returnArrays.copyofrange (Array, start+1, end+1);//interception does not include itself -}
There is also a request, given an array, to find the longest substring in the array, the head and tail of the substring is the same number. For example: 1,2,3,4,2,4 2 spacing is 3
1 Public Static voidMain (string[] args) {2 3 int[] Array = {1,2,-4,3,5,-4,7,8,2,-1,6,-5,-200,3,47,-2};4 //looking for the eldest son of a string5 int[] Lenarray =Findmaxlengtharray (array);6 for(intTmp:lenarray) {7System.out.print (tmp+ "");8 }9 Ten } One A /** - * @Description: Find the oldest string - * @paramArray the * @return - * @returType: int[] - */ - Public Static int[] Findmaxlengtharray (int[] Array) { + //key places the elements of the array, value the first element holds the starting position, the second position ends, and the third element holds the distance -Map<integer, integer[]> map =NewHashmap<integer, integer[]>(); + for(inti=0;i<array.length;i++){ A if(Map.get (array[i]) = =NULL){ atinteger[] INTs =NewInteger[3]; -Ints[0]=i;//Initial Position -Ints[1]=i;//End Position -ints[2]=0;//Initial length - Map.put (Array[i], ints); -}Else{ inMap.get (Array[i]) [1]=i; -Map.get (Array[i]) [2]=i-map.get (Array[i]) [0]; to } + } -Iterator<entry<integer, integer[]>> Iterator =Map.entryset (). iterator (); the intMax = 0; * intStart = 0; $ intEnd = 0;Panax Notoginseng intKey = 0; - while(Iterator.hasnext ()) { theEntry<integer, integer[]> Entry =Iterator.next (); + if(Entry.getvalue () [2]>max) { Amax = Entry.getvalue () [2]; theStart = Entry.getvalue () [0]; +End = Entry.getvalue () [1]; -Key =Entry.getkey (); $ } $ } -SYSTEM.OUT.PRINTLN ("Maximum length number is:" +key); -SYSTEM.OUT.PRINTLN ("Maximum length is:" +max); the returnArrays.copyofrange (Array, start+1, end+1);//interception does not include itself -}
In fact, a hashmap,value is used to store the start position, the end position and the length, so over and over the group, and then traverse HashMap to find the longest distance, integer[] change to hashmap efficiency will be higher, the time complexity of O (n), Higher spatial Complexity
Java notation for maximum substring and eldest-son string