[45] LeetCode interview lined up the minimum number of array

1. Topic

Enter a positive integer array to array all the numbers 拼接起来lined up a number of printing all numbers can be spliced out in 最小one.

示例1:
输入: [10,2]           输出: "102"
示例2:
输入: [3,30,34,5,9]    输出: "3033459"

prompt:

  • 0 < nums.length <= 100

Description:

  • The output can be quite large, so you need to return a string instead of an integer
  • Stitching together leading figures may be 0, the final results do not need to remove the leading 0

Source: stay button (LeetCode)
link: https: //leetcode-cn.com/problems/ba-shu-zu-pai-cheng-zui-xiao-de-shu-lcof/
copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source.


2. Problem Solving

2.1 Brainstorming

  • In simple terms, this question is to ask the stitching together of the "minimum", in essence, is a fact 排序问题.
  • Considering the problem is sorted, quick discharge function can be used directly sort(). However, according to the subject demands to the stitching together of the "minimum", and therefore to change 排序判断规则.
  • The input is an array of integers, and splicing, the output string. Therefore, also familiar with 整型转字符型the
    method.

For the following three questions above us look at each analysis:
(1) for several basic sorting method, and the corresponding algorithm complexity analysis to understand what is necessary. If you do not know, I suggest you first meal Gaga: [US] not use the algorithm of bubble, selection, insertion sort, and not afford to lose this man!
(2) sort () function is a very popular fast discharge method, write code section not only time, but also in time, the comprehensive index on spatial complexity is also good.

  • Familiarize yourself with the basic usage sort () function:
在C++中,使用sort()函数前,需添加头文件:#include<algorithm>

1. 默认情况下是:从小到大排序(升序)
sort(arrNum.begin(), arrNum.end())

2.如果要从大到小排序(降序),则需要添加排序判断规则
bool compare(int a, int b){  
	return a>b;   //自定义判断规则
}
sort(arrNum.begin(), arrNum.end(), compare)  //降序
  • So for this question, we need to how 改变判断规则to make 拼接起来的数最小it? We can look at this picture:

Here Insert Picture Description

For example, "3" and "30", in the end who should be put in front? We just need to look at the stitching together: 330 or 303, it is clear that 303 should be small, so to 30 front row, 3 on the back.
Understandably, you want to make a minimum value after stitching, we must find ways to "Decimal" on the front, but each number 位数不同, so we can not be directly compared. Here you can use a 取巧的方法, 不去size comparisons number of each bit, 而是去before and after the string "size" after stitching. When the array, each adjacent elements meet this "size" relationships, and finally the array element is aligned with the splice after the minimum.
So how by changing the judge rules? Method is as follows:

bool compare(string s1, string s2){
	return s1+s2 < s2+s1;
}
sort(arr_str.begin(), arr_str.end(), compare)

Add 字符串大小比较rules: ASCII code value, and the larger from left to right, followed by comparing each character corresponding to large and small are small; if equal, the comparison continues behind the characters.

(3) transfer character integer

  • For most compilers, C ++ 11 support the new features, you can directly use the function to_string ()
使用前需添加头文件:#include<string>
str_num = to_string(num) //将整型数num转化成字符串str_num
  • Do not support the C ++ compiler 11 (such as some competition), the following methods may be used:
使用前需添加头文件:#include<sstream>
实现整型转字符串:
string NumToString(int num){ 
	string output;
	stringstream Convert;
	Convert<<num;
	Convert>>output;
	return output;
}

2.2 coding practices

After more than familiar with problem-solving ideas, we can write a simple and effective code:

class Solution {
public:
	static bool compare(string s1, string s2){
		return s1+s2 <s2+s1; //自定义排序规则 
	}
    string minNumber(vector<int>& nums) {
        vector<string> temp;
        for(int n : nums){
            temp.push_back(to_string(n)); //整型转字符串 
        }
        sort(temp.begin(), temp.end(), compare); //按自定义规则排序 
        string ans = "";
        for(string s : temp) ans += s; //拼接 
        return ans;
    }
};

Test Results:
Here Insert Picture Description

Published 56 original articles · won praise 455 · views 10000 +

Guess you like

Origin blog.csdn.net/wjinjie/article/details/105405016