SequenceOfNumbers
SRM 255 · 2005-07-28 · by Andrew_Lazarev
SRM 255 · 2005-07-28 · by Andrew_Lazarev · Sorting
Problem Statement
Problem Statement
It is a common mistake to sort numbers as strings. For example, a sorted sequence like {"1", "174", "23", "578", "71", "9"} is not correctly sorted if its elements are interpreted as numbers rather than strings.
You will be given a String[] sequence that is sorted in non-descending order using string comparison. Return this sequence sorted in non-descending order using numerical comparison instead.
Constraints
- sequence will contain between 2 and 50 elements inclusive.
- Each element of sequence will contain between 1 and 9 characters inclusive.
- Each element of sequence will consist of only digits ('0'-'9').
- Each element of sequence will not start with a '0' digit.
- sequence will be ordered lexicographically.
Examples
0)
{"1","174","23","578","71","9"}
Returns: {"1", "9", "23", "71", "174", "578" }
1)
{"172","172","172","23","23"}
Returns: {"23", "23", "172", "172", "172" }
2)
{"183","2","357","38","446","46","628","734","741","838"}
Returns: {"2", "38", "46", "183", "357", "446", "628", "734", "741", "838" }
3)
{"1","2"}
Returns: {"1", "2" }
4)
{"199999999","2"}
Returns: {"2", "199999999" }
Submissions are judged against all 42 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Coding Area
Language: C++17 · define a public class SequenceOfNumbers with a public method vector<string> rearrange(vector<string> sequence) · 42 test cases · 2 s / 256 MB per case