Describe:
Implement input a set of integers greater than or equal to 0, according to the order from small to large output, after sorting there are consecutive numbers, only the minimum and maximum number of consecutive number of two.
Input:
A set of integers greater than or equal to 0, regardless of illegal input, separated by commas (",") between the integers, the total length of the input string is less than or equal to 100 bytes.
Output:
The sorted value, separated by a space between the individual integers.
Sample input:
1,4,3,110,2,90,7
Sample output:
1 4 7) 90 110
Code implementation:
Package Huaweiimport ("FMT" "Sort" "StrConv" "strings") func Test3base () {res: = "1,4,3,2,90,7,6,17,8,9" Sortnums (RES)} Func sortnums (resstring string) {numsstr: = strings. Split (Resstring, ",") Numsint: = Converttointslice (NUMSSTR)//sorts sort. Ints (Numsint)//consecutive digits retain only the result: = Removecenteritems (Numsint) fmt. PRINTLN (Result)}//will []string into []intfunc converttointslice (Numsstr []string)] []int {numsint: = make ([]int, Len (NUMSSTR)] ) for I, V: = range numsstr {n, err: = StrConv. Atoi (CheckError) (Err, "string to Integer") numsint[i] = N}return numsint}//Continuous number retains only the end-to-end func Removecenteritems (nums []int) []int {//Index position to remove removelist: = Make ([]int, 0, Len (nums)) for I: = 1; i < Len (nums)-1; i++ {if nums[i] = = Nums[i-1]+1 &A mp;& Nums[i] = = nums[i+1]-1 {removelist = append (Removelist, i)}}//remove for I: = Len (nums)-2; i > 0; i--{if checkcontains (removelist, i) {nums = append (Nums[:i], nums[i+1:] ...)} Return nums}//determine if there is an element in slice func checkcontains (nums []int, value int) bool {for _, V: = range Nums {if v = = Value {return True}}return false}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithm basics: Integer sorting problem (consecutive integers retain only two digits)