leetcode 专题—sort

此将主要将leetcode中sort专题的解答都放在这里,后续会慢慢加入

一:leetcode179 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 is 9534330.

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

分析:这里主要采用stl中的sort函数,注意对与3和33,我们认为其是相等的,另外3要比31大且比34小。最后需要注意的是stl中sort函数对于相等操作要返回false,返回true的话会报异常,警告信息为:

Debug Assertion Failed!

File: c:\program files (x86)\microsoft visual studio

10.0\vc\include\algorithm

Line: 3657

Expression: invalid operator<

调试的时候查看源码就知道了,源码如下:

bool __CLRCALL_OR_CDECL _Debug_lt_pred(_Pr _Pred, _Ty1& _Left, _Ty2& _Right,

const wchar_t *_Where, unsigned int _Line)

{// test if _Pred(_Left, _Right) and _Pred is strict weak ordering

if (!_Pred(_Left, _Right))                                // 相等返回true, else if中仍然返回true

return (false);

else if (_Pred(_Right, _Left))

_DEBUG_ERROR2("invalid operator<", _Where, _Line);//断言出现在这里。

return (true);

}

代码:

bool cmp(const string &str1, const string &str2){
	if(str1 == str2) return false;     // 对于STL中的sort 相等不能返回true 看源码就知道了 否则就会报异常
    int m = str1.size(), n = str2.size();
    int i= 0, j = 0;
    while(i < m && j < n){
        if(str1[i] < str2[j]) return true;
        else if(str1[i] > str2[j]) return false;
        else{
            if(i+1 == m && j+1 == n) return false;    // 这个主要处理比如3 和33的情况 可以认为是相等的
            if(i+1 < m)i++;
			else i = 0;
            if(j+1 < n)j++;
			else j = 0;

        }
    }
    return true;
}

class Solution {
public:
    string largestNumber(vector<int>& nums) {

      //  if(nums.size() == 0) return str;
        numsStr.resize(nums.size());
        for(int i = 0; i < nums.size(); i++){
            stringstream ss;
            ss << nums[i];
            ss >> numsStr[i];
        }
        sort(numsStr.begin(), numsStr.end(), cmp);
        string str;
        for(vector<string>::reverse_iterator iter = numsStr.rbegin(); iter != numsStr.rend(); iter++)
            str += *iter;
        if(str[0] == '0') str = "0";
        return str;
    }
private:
    vector<string> numsStr;
};
时间: 2024-11-05 16:31:04

leetcode 专题—sort的相关文章

LeetCode Insertion Sort List

class Solution { public: ListNode *insertionSortList(ListNode *head) { if (head == NULL) return NULL; ListNode* sorted_head = head; ListNode* unsorted_head = head->next; head->next = NULL; ListNode* cur = unsorted_head; while (cur != NULL) { unsorte

【LeetCode】Sort Colors

LeetCode OJ Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, w

[leetcode]Insertion Sort List @ Python

原题地址:http://oj.leetcode.com/problems/insertion-sort-list/ 题意:对链表进行插入排序. 解题思路:首先来对插入排序有一个直观的认识,来自维基百科. 代码循环部分图示: 代码: class Solution: # @param head, a ListNode # @return a ListNode def insertionSortList(self, head): if not head: return head dummy = Lis

leetcode——Insertion Sort List 对链表进行插入排序(AC)

Sort a linked list using insertion sort. class Solution { public: ListNode *insertionSortList(ListNode *head) { if(head == NULL || head->next == NULL) return head; ListNode *result; result->val = INT_MIN; result->next = NULL; ListNode *cur=head,*

[LeetCode OJ] Sort Colors

Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue. Here, we will use the integers 0, 1, and 2 to represent the color red, white, and bl

LeetCode :: Insertion Sort List [详细分析]

Sort a linked list using insertion sort. 仍然是一个非常简洁的题目,让我们用插入排序给链表排序:这里说到插入排序,可以来回顾一下, 最基本的入门排序算法,就是插入排序了:时间复杂度为n^2,最基本的插入排序是基于数组实现的,下面给出基于数组实现的插入排序,来体会一个插入排序的思想: 以下仅为数组实现,不是解题代码,没兴趣可以跳过. void insertionsort (int a[], int N) { for (int i = 1; i < N; i+

LeetCode OJ - Sort List

题目: Sort a linked list in O(n log n) time using constant space complexity. 解题思路: 复杂度为O(n* logn) 的排序算法有:快速排序.堆排序.归并排序.对于链表这种数据结构,使用归并排序比较靠谱.递归代码如下: 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNod

LeetCode: Insertion Sort List [147]

[题目] Sort a linked list using insertion sort. [题意] 用插入排序方法排序链表 [思路] 直接搞 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode

leetcode linkedList sort

链表排序:算法-归并排序 public class LinkedSort { private static class ListNode { int val; ListNode next; ListNode(int x) { val = x; next = null; } } private ListNode mergeList(ListNode head1,ListNode head2){ if(head1==null){ return head2; }else if(head2==null)