zoukankan      html  css  js  c++  java
  • [LeetCode] Largest Number

    想到的就是排序,把大的放在前面,

    注意下面的程序,如果数字本身是0,也会加到str中,

    如果用while(tmp),那么就加不到str中了,

    这里相当于一个do while,不过个人不喜欢用do while,所以这样写

    29     while(1)
    30                 {
    31                     remainder = tmp%10;
    32                     str.push_back(remainder+'0');
    33                     tmp = tmp/10;
    34                     if(tmp == 0)
    35                         break;
    36                 }

    另外,比较函数,直接用两个字符串str1 和str2构造str3={str1,str2},str4={str2, str1},然后比较str3,str4,比较好比 

    后面的noZeroIdx的逻辑是提交的时候报错改动的,testcase是0,0, 我输出了00,所以针对0做了特殊处理,处理掉前面的0。

     1 bool myfunction (string str1,string str2)
     2 {
     3     str1.append(str2);
     4     str2.append(str1);
     5 
     6     for(int i = 0; i < str1.size(); i++)
     7     {
     8         if(str1[i] > str2[i])
     9             return true;
    10         else if(str1[i] < str2[i])
    11             return false;
    12     }
    13     return false;
    14 }
    15 
    16 
    17 
    18 class Solution {
    19     public:
    20         string largestNumber(vector<int> &num) {
    21            vector<string> strs;
    22            string result;
    23 
    24            for(int i = 0; i < num.size(); i++)
    25            {
    26                 int tmp = num[i];
    27                 int remainder = 0;
    28                 string str;
    29                 while(1)
    30                 {
    31                     remainder = tmp%10;
    32                     str.push_back(remainder+'0');
    33                     tmp = tmp/10;
    34                     if(tmp == 0)
    35                         break;
    36                 }
    37                 reverse(str.begin(), str.end());
    38                 strs.push_back(str);
    39            }
    40            sort(strs.begin(), strs.end(), myfunction);
    41            for(int i = 0; i< strs.size(); i++)
    42            {
    43                 result.append(strs[i]);
    44                 //cout << strs[i] <<"	";
    45            }
    46 
    47             int noZeroIdx = 0;;
    48             for(int i = 0; i < result.size(); i++)
    49             {
    50                 if(result[i] == '0')
    51                     noZeroIdx++ ;
    52                 else
    53                     break;
    54             }
    55             if(noZeroIdx == result.size())
    56             {
    57                 result.clear();
    58                 result.append("0");
    59             }
    60             else
    61                 result = result.substr(noZeroIdx);
    62            return result;
    63         }
    64 };
  • 相关阅读:
    HashMap 使用小结
    linux下的文本处理命令sed&awk&grep
    HashMap和Hashtable的区别 .Properties
    Linux awk简简单单
    linux配置java环境变量(详细)
    linux后台运行程序及恢复
    为什么需要 单例设计模式(Singleton)?
    Linux文本处理命令
    使用Perf4J进行性能分析和监控
    sqlldr的用法总结
  • 原文地址:https://www.cnblogs.com/diegodu/p/4241894.html
Copyright © 2011-2022 走看看