关闭

Leetcode160: Largest Number

标签: leetcodealgorithm
163人阅读 评论(0) 收藏 举报
分类:

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 is 9534330.

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

class Solution {
public:
    static bool compare(string& s1, string& s2)
    {
        return (s1+s2)>(s2+s1);
    }
    string largestNumber(vector<int>& nums) {
        vector<string> num(nums.size());
        for(int i = 0; i < nums.size(); i++)
        {
            num[i] = to_string(nums[i]);
        }
        sort(num.begin(), num.end(), compare);
        string res;
        bool flag = false;
        for(int i = 0; i < num.size(); i++)
        {
            if(num[i] != "0"){
                res += num[i];
                flag = true;
            }
            else if(flag)
            {
                res += num[i];
            }
        }
        if(!flag) res.push_back('0');
        return res;
    }
};


0
0
查看评论

LeetCode 160 :Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists:  A: ...
  • sunao2002002
  • sunao2002002
  • 2015-05-14 22:06
  • 1303

LeetCode 160 Intersection of Two Linked Lists(链表相交)(Linked List)(*)

翻译写一个程序来找出两个单链表相交的开始处。例如,如下有两个链表:在节点c1处开始相交。批注: 如果两个链表根本没有相交,返回NULL。 在函数返回后链表必须保留原有的数据结构。 你可以假设在整个链结构中没有循环。 你的代码最后可以在O(n)时间和O(1)空间运行。原文Write a program...
  • NoMasp
  • NoMasp
  • 2016-01-24 09:26
  • 5078

LEETCODE 160

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: ...
  • zj443108444
  • zj443108444
  • 2016-03-10 18:24
  • 161

【LeetCode-面试算法经典-Java实现】【160-Intersection of Two Linked Lists(单链表的交集)】

【160-Intersection of Two Linked Lists(单链表的交集)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Write a program to find the node at which the intersection of two ...
  • DERRANTCM
  • DERRANTCM
  • 2015-08-22 06:45
  • 3379

LeetCode 160 — Intersection of Two Linked Lists(C++ Python)

题目:https://oj.leetcode.com/problems/intersection-of-two-linked-lists/ Write a program to find the node at which the intersection of two singly linked...
  • dragon_dream
  • dragon_dream
  • 2014-12-02 20:59
  • 1851

leetcode 160: Intersection of Two Linked Lists

Intersection of Two Linked Lists Total Accepted: 8676 Total Submissions: 32571 Write a program to find the node at which the intersection of two si...
  • xudli
  • xudli
  • 2014-12-30 03:17
  • 2755

LeetCode 160. Intersection of Two Linked Lists 题解 —— Java

题目链接:https://leetcode.com/problems/intersection-of-two-linked-lists/#/description 题目要求:找出两个链表的交叉点,若两链表没有交叉点,返回null。 思路:(1)首先遍历两个链表,计算出两个链表的长度  ...
  • xiaoyi357
  • xiaoyi357
  • 2017-03-20 21:10
  • 167

【LeetCode】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], th...
  • ljiabin
  • ljiabin
  • 2015-01-13 15:04
  • 11283

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 f...
  • feliciafay
  • feliciafay
  • 2015-01-21 15:58
  • 2605

[LeetCode160] Intersection of Two Linked Lists

Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: ...
  • sbitswc
  • sbitswc
  • 2015-08-14 12:31
  • 286
    个人资料
    • 访问:84346次
    • 积分:2493
    • 等级:
    • 排名:第17425名
    • 原创:148篇
    • 转载:119篇
    • 译文:0篇
    • 评论:6条
    个人主页
    文章分类
    最新评论