Why not one use python build-in sort function for list? Is it not the point of question?


  • 0
    G

    def sortColors(self, nums):

        return nums.sort()

  • 0
    T

    The point of the question is to get your self ready for interview. Your answer like this would not be accepted during the interview.


  • 0
    G

    thank you for your help
    I try another code:
    class Solution(object):
    def sortColors(self, nums):
    """
    :type nums: List[int]
    :rtype: void Do not return anything, modify nums in-place instead.
    """
    dic={}
    for i in nums:
    dic[i]=dic.get(i,0)+1
    for j in range(0,dic[0]):
    nums[j]=0
    for k in range(dic[0],dic[0]+dic[1]):
    nums[k]=1
    for p in range(dic[0]+dic[1],dic[0]+dic[1]+dic[2]):
    nums[p]=2
    It works well on my own computer, but in leetcode, it does not work because of keyerror[1]

    Can you tell me where is the problem ?
    thank you


  • 0
    G

    class Solution(object):

    def sortColors(self, nums):
        """
        :type nums: List[int]
        :rtype: void Do not return anything, modify nums in-place instead.
        """
        dic={}
        for i in nums:
            dic[i]=dic.get(i,0)+1
        for j in range(0,dic[0]):
            nums[j]=0
        for k in range(dic[0],dic[0]+dic[1]):
            nums[k]=1
        for p in range(dic[0]+dic[1],dic[0]+dic[1]+dic[2]):
            nums[p]=2
    

    it works in my computer, but will not work in leetcode. what is the problem with my codes?

    thx


  • 0
    M

    There are cleaner ways think of keeping pointers to list of 0s and 2s and 1s ( maybe). No dictionary and all. Just use the given vector and pointers.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.