Algorithm basics: Integer sorting problem (consecutive integers retain only two digits)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.