zoukankan      html  css  js  c++  java
  • 面试题45:把数组排成最小的数

    输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个。例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323。

    解题思路

    • 回溯法(较暴力)
    • 重新定义比较数字函数(较高级)

    上代码(C++香)

    法一,回溯法递归出全排列
    #include <iostream>
    #include <vector>
    #include <stack>
    #include <string>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include <stdlib.h>
    #include "TreeNode.h"
    using namespace std;
    
    string minS = "99999999";
    
    void mySwap(vector<int> &num, int i, int j){
        int temp = num[j];
        num[j] = num[i];
        num[i] = temp;
    }
    
    bool isSwap(vector<int> num, int index, int n){
        for(int i = index + 1; i < n; i++){
            if(num[i] == num[index])
                return false;
        }
        return true;
    }
    
    void dfs(vector<int> &num, int index, int n){
    
        if(index == n){
            string nowS;
            for(int i = 0; i < n; i++)
                nowS = nowS + to_string(num[i]);
    
            if(nowS < minS)
                minS = nowS;
            return ;
        }
        for(int i = index; i < n; i++){
            if(isSwap(num, i, n)){
                mySwap(num, i, index);
                dfs(num, index + 1, n);
                mySwap(num, i, index);
            }
        }
    }
    
    // 使用全排列来判断
    string PrintMinNumber(vector<int> numbers) {
        dfs(numbers, 0, numbers.size());
        return minS;
    }
    
    
    int main()
    {
        vector<int> num;
        num.push_back(3);
        num.push_back(32);
        num.push_back(321);
        cout<<PrintMinNumber(num)<<endl;
        return 0;
    }
    
    法二:重新定义数字的比较函数

      比如数字332,因为323<332,所以32<3

    #include <iostream>
    #include <vector>
    #include <stack>
    #include <string>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include <stdlib.h>
    #include "TreeNode.h"
    using namespace std;
    
    const int g_MaxNumberLength = 10;
    
    char* g_StrCombine1 = new char[g_MaxNumberLength * 2 + 1];
    char* g_StrCombine2 = new char[g_MaxNumberLength * 2 + 1];
    
    // 重新定义数字比较函数
    int compare(const void* strNumber1, const void* strNumber2){
        strcpy(g_StrCombine1, *(const char**)strNumber1);
        strcat(g_StrCombine1, *(const char**)strNumber2);
    
        strcpy(g_StrCombine2, *(const char**)strNumber2);
        strcat(g_StrCombine2, *(const char**)strNumber1);
    
        return strcmp(g_StrCombine1, g_StrCombine2);
    }
    
    void PrintMinNumber(int numbers[], int length){
        if(length <= 0)
            return ;
        char** strNumbers = (char**)(new int[length]);
        for(int i = 0; i < length; i++){
            strNumbers[i] = new char[g_MaxNumberLength + 1];
            sprintf(strNumbers[i], "%d", numbers[i]);
        }
        
    	// 根据自定义的比较函数调用快排进行排序
        qsort(strNumbers, length, sizeof(char*), compare);
    
        for(int i = 0; i < length; i++)
            printf("%s", strNumbers[i]);
        printf("
    ");
    
        for(int i = 0; i < length; i++)
            delete[] strNumbers[i];
        delete[] strNumbers;
    }
    
    int main()
    {
        int num[] = {3,32,321};
    
        PrintMinNumber(num, 3);
    
        return 0;
    }
    
  • 相关阅读:
    Oracle 安装报错 [INS-06101] IP address of localhost could not be determined 解决方法输入日志标题
    Linux下安装oracle数据库提示DISPLAY not set. Please set the DISPLAY and try again。
    redhat 关机注销命令详解
    VirtualBox的四种网络连接方式
    修改RedHat的系统显示时间
    insufficient memory to configure kdump(没有足够的内存)解决方法(待验证、待解决)
    xen坑随笔 heartbeat dpkg垃圾数据库清除
    tomcat 监控脚本
    负载均衡随笔
    GIT命令介绍
  • 原文地址:https://www.cnblogs.com/flyingrun/p/13531123.html
Copyright © 2011-2022 走看看