zoukankan      html  css  js  c++  java
  • 把数组排成最小的数

    题目描述

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

    /*对vector容器内的数据进行排序,按照 将a和b转为string后
     若 a+b<b+a  a排在在前 的规则排序,
     如 2 21 因为 212 < 221 所以 排序后为 21 2 
      to_string() 可以将int 转化为string
    */
    class Solution {
    public:
        static bool cmp(int a,int b) {
            string A="";
            string B="";
            A+=to_string(a);
            A+=to_string(b);
            B+=to_string(b);
            B+=to_string(a);
            return A<B;
        }
        
        string PrintMinNumber(vector<int> numbers) {
            string answer="";
            sort(numbers.begin(),numbers.end(),cmp);
            for(int i=0;i<numbers.size();i++) {
                answer+=to_string(numbers[i]);
            }
            return answer;
                    
        }
    };
    

      

    class Solution {
    public:
        string PrintMinNumber(vector<int> numbers) {
           string result;
           int length=numbers.size();
           if(length<=0)
               return result;
           stringstream stream;
           //string *str=new string[length];
           vector<string> vec;
           for(int i=0;i<length;i++)
            {
                //方法1:用to_string将数字转换为字符串
                vec.push_back(to_string(numbers[i]));
                //方法2:借助借助字符串流stringstream将数字转换为字符串
                /*string str;
                stream<<numbers[i]; 
                stream>>str;
                vec.push_back(str);
                stream.clear();   //记得要清空
                */
            }
           sort(vec.begin(),vec.end(),comp);
           for(int i=0;i<length;i++)
             { 
                 result+=vec[i];
             }
            return result;
        }
         static bool comp(string str1,string str2)  //若comp函数放在外面则可以去掉static关键字,原因如下:
         {                             //sort中的比较函数compare要声明为静态成员函数或全局函数,不能作为普通成员函数,否则会报错。
                                       //因为:非静态成员函数是依赖于具体对象的,而std::sort这类函数是全局的,因此无法再sort中调
            string s1=str1+str2;       //用非静态成员函数。静态成员函数或者全局函数是不依赖于具体对象的, 可以独立访问,无须创建任何
            string s2=str2+str1;       //对象实例就可以访问。同时静态成员函数不可以调用类的非静态成员。
            return s1<s2;
         }
    };
    

      

     http://blog.csdn.net/fanzitao/article/details/7895344

    http://blog.csdn.net/taoyanqi8932/article/details/52541312

    class Solution {
    public:
        string PrintMinNumber(vector<int> numbers) {  
            sort(numbers.begin(),numbers.end(),[](const int& a,const int& b){ 
            return to_string(a)+to_string(b)<to_string(b)+to_string(a);});
            string res;
            for (auto c:numbers)    
                res+=to_string(c);
            return res;
        }
    };
    

      

    拥抱明天! 不给自己做枷锁去限制自己。 别让时代的悲哀,成为你人生的悲哀。
  • 相关阅读:
    Entity SQL 初入
    ObjectQuery查询及方法
    Entity Framework 的事务 DbTransaction
    Construct Binary Tree from Preorder and Inorder Traversal
    Reverse Linked List
    Best Time to Buy and Sell Stock
    Remove Duplicates from Sorted Array II
    Reverse Integer
    Implement Stack using Queues
    C++中const限定符的应用
  • 原文地址:https://www.cnblogs.com/dd2hm/p/7375493.html
Copyright © 2011-2022 走看看