`
SunnyYoona
  • 浏览: 367414 次
社区版块
存档分类
最新评论

[LeetCode]179.Largest Number

 
阅读更多

【题目】

Given a list of non negative integers, arrange them such that they form the largest number.

For example, given[3, 30, 34, 5, 9], the largest formed number is9534330.

Note: The result may be very large, so you need to return a string instead of an integer.

【分析】

数字转换为字符串,按字典序从大到小排序,并拼接在一起就是最大的数

【代码】

/*********************************
*   日期:2015-01-18
*   作者:SJF0115
*   题目: 179.Largest Number
*   网址:https://oj.leetcode.com/problems/largest-number/
*   结果:AC
*   来源:LeetCode
*   时间复杂度:O(n)
*   空间复杂度:O(n)
*   博客:
**********************************/
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

class Solution {
public:
    static bool cmp(const string &a,const string &b){
        string ab = a + b;
        string ba = b + a;
        return ab > ba;
    }
    string largestNumber(vector<int> &num) {
        int count = num.size();
        vector<string> vec;
        if(count == 0){
            return NULL;
        }//if
        // 整数转换为字符串
        int zeroCount = 0;
        for(int i = 0;i < count;++i){
            // 统计0的个数
            if(num[i] == 0){
                zeroCount ++;
            }//if
            char str[20];
            sprintf(str,"%d",num[i]);
            vec.push_back(str);
        }//for
        // 处理一个特殊情况 全是0的情况
        if(zeroCount == count){
            return "0";
        }//if
        // 剪枝 只有一条数据
        if(count == 1){
            return vec[0];
        }//if
        // 从大到小排序
        sort(vec.begin(),vec.end(),cmp);
        // 拼接一个最大的数
        string result;
        for(int i = 0;i < count;++i){
            result += vec[i];
        }//for
        return result;
    }
};

int main(){
    Solution solution;
    vector<int> num;
    num.push_back(3);
    num.push_back(30);
    num.push_back(34);
    num.push_back(5);
    num.push_back(9);
    // 重新排列
    string result = solution.largestNumber(num);
    // 输出
    cout<<result<<endl;
    return 0;
}


特别注意一个特殊情况:


全是0的情况,只返回一个0即可。





分享到:
评论

相关推荐

    lrucacheleetcode-LeetCodeSheet:记录自己Leetcode之旅

    179. Largest Number Leetcode 75. Sort Colors Leetcode 215. Kth Largest Element Leetcode 4. Median of Two Sorted Arrays 注意:后两题是与快速排序非常相似的快速选择(Quick Select)算法,面试中很常考 链表...

    LeetCode最全代码

    268| [Missing Number](https://leetcode.com/problems/missing-number/) | [C++](./C++/missing-number.cpp) [Python](./Python/missing-number.py) | _O(n)_ | _O(1)_ | Medium | LintCode || 318| [Maximum ...

    leetcode寻找最近的-qyhc:群英荟萃,各种资源,欢迎各位大佬共享笔记或资源

    leetcode寻找最近的 设计模式 ...随时随记 随记笔记 ...最大数(golang)](Alg/leetcode/largestNumber.go [190. 颠倒二进制位(golang)](Alg/leetcode/reverseBits.go Docker 面试题 GO面试题 Redis面试题

    LeetCode C++全解

    1. Introduction ... Largest Rectangle in Histogram ix. Maximal Rectangle x. Palindrome Number xi. Search a 2D Matrix xii. Search for a Range xiii. Search Insert Position xiv. Find Peak Element

    扩展矩阵leetcode-Leetcode:LeetcodeAnswer-Java

    扩展矩阵leetcode Leetcode Leetcode Answer-Java 数组 11.乘最多水容器 maxArea 26.删除排序数组中的重复项 removeDuplicates 33.搜索旋转排序数组 ...34.在排序数组中查找元素的第一个和最后一个...largestNumber 324.摆

    leetcode装最多水-Leetcode:解决了leetcode问题

    largest element from an unsorted array in linear time. (1) If the number of elements in an array is large .Divide the array into arrays each with 5 elements. n/5 arrays. (2) Compute Median of these 5 ...

    leetcode316-LeetCode:leetcode的解决方案

    Largest Rectangle in Histogram:单调栈 Island Perimeter:简单对比(map+zip的使用) or 遍历查找 Max Area of Island:DFS(本来想用DP,发现出不来) Number of Islands:DFS My Calendar II:小空间匹配 My ...

    gasstationleetcode-LeetCode:Leet和其他OJ问题

    加油站 leetcode 力码 该文件夹不仅包含算法和数据库的leetcode问题,还包含其他公开...largest distance from the sum of any subarray to the average of subarray (sum(a[n])/(k+1)). Output: a number of the Im

    Coding Interview In Java

    leetcode Java 246 題目及解答 (英文) Contents 1 Rotate Array in Java 15 2 Reverse Words in a String II 19 3 Evaluate Reverse Polish Notation 21 4 Isomorphic Strings 25 5 Word Ladder 27 6 Word Ladder ...

    leetcode答案-easy_Maximum-Subarray:easy_Maximum-Subarray

    leetcode 答案 easy_Maximum-Subarray 提交链接 / Submit (You need register/login first before submit.) (在提交前你需要先注册或登录) 题目描述 / Description Given an integer array nums, find the ...

    cpp-算法精粹

    Largest Number 小结 查找 Search for a Range Search Insert Position Search in Rotated Sorted Array Search in Rotated Sorted Array II Search a 2D Matrix Search a 2D Matrix II Find Minimum in Rotated ...

Global site tag (gtag.js) - Google Analytics