Give a non-negative integer to find the maximum number of decrements contained in this nonnegative integer. The descending number of a number refers to the number of contiguous digits ranging from large to small.
such as: 95345323, the decrement number has: 953,95,53,53,532,32, then the largest decrement number is 953.
If the number entered is negative, return-1.
If the number of decrements is not found, it also returns-1.
Code implementation:
Package Huaweiimport ("FMT" "Sort" "StrConv") func test5base () {num: = 431492degressiveNums: = getdegressivenums (num) Max: = -1if len (degressivenums) > 0 {max = Getmax (degressivenums)}fmt. Println ("Max:", max)}//gets all decrements of num func getdegressivenums (num int) []int {if num < 0 {return []int{-1}}degressivenums]: = Make ([]int, 0) Numstr: = StrConv. Itoa (num) Length: = Len (numstr)//String length of I for I: = 2; i < length; i++ {//Intercept for J from J: = 0; J < length-i+1; J + + {//intercept number n, err: = StrConv. Atoi (Numstr[j:j+i]) checkerror (err, "string to Integer")//is the number of decrements if Isdegressive (n) {degressivenums = append ( Degressivenums, N)}}}return degressivenums}//determines whether the number num is a decrement number func isdegressive (num int) bool {Weishu: = make ([]int, 0) for Num >= 1 {n: = num% 10weishu = append (Weishu, n) Num/= 10}return sort. Intsaresorted (Weishu)}//gets the largest number of Func Getmax in a slice (nums []int) int {if len (nums) = = 0 {Panic ("empty Slice.")} Max: = nums[0]for I: = 1; I < Len (nums); i++ {if nums[i] > max {max = Nums[i]}}return Max}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Algorithm Basics: Maximum decrement number problem (Golang implementation)