查看: 1554| 回复: 4
跳转到指定楼层
上一主题 下一主题
收起左侧

[Leetcode] 【31. Next Permutation - Python】IDE通过,leetcode不通过

全局:

注册一亩三分地论坛,查看更多干货!

您需要 登录 才可以下载或查看附件。没有帐号?注册账号

x
本帖最后由 mmxmw 于 2019-8-2 09:03 编辑

以下是小弟的solution:

# 31. Next Permutation
# Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
#
# If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
#
# The replacement must be in-place and use only constant extra memory.
#
# Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
#
# 1,2,3 → 1,3,2
# 3,2,1 → 1,2,3
# 1,1,5 → 1,5,1

class Solution:
    def nextPermutation(self, nums) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        if len(nums) <= 1:
            return nums

        for i in list(range(len(nums)-1, 0, -1)):
            if nums <= nums[i-1]:
                pass
            else:
                k = self.switch_pos(i-1, len(nums)-1, nums)
                nums[i-1], nums[k] = nums[k], nums[i-1]
                a = nums[i:]
                a.sort()
                return nums[:i] + a

        nums.sort()
        return nums

    def switch_pos(self, start, end, nums):
        curr = end
        while curr >= start:
            if nums[curr] > nums[start]:
                return curr
            else:
                curr -= 1

if __name__ == '__main__':
    x = Solution()
    nums = [1, 3, 2]
    print(x.nextPermutation(nums))

Test case: nums = [1, 3, 2]

在本地output是[3, 1, 2],正确。但Leetcode提交后显示
Your input
[1, 3, 2]
Output
[2,3,1]
Expected
[2,1,3]

请问是咋回事?

上一篇:被开了,刷题面试,只剩50天了。
下一篇:请问诸位高阶一些的算法该如何自学(比如DP)?
全局:
没仔细看算法是否正确 但是for循环中的a没有modify nums in place
回复

使用道具 举报

🔗
 楼主| mmxmw 2019-8-2 15:01:57 | 只看该作者
全局:
yayafuture 发表于 2019-8-2 14:27
没仔细看算法是否正确 但是for循环中的a没有modify nums in place

是的, 没有modify nums in place。但我本意就不是modify in place。

a = nums[i:]
a.sort()
return nums[:i] + a

我只是把nums的一部分sort,然后再和nums的前半部分合并起来。
回复

使用道具 举报

全局:
mmxmw 发表于 2019/08/02 15:01:57


是的, 没有modify nums in place。但我本意就不是modify in place。

a = nums[i:]
a.sort()
return nums[:i] + a
...

那lc肯定不过啊……lc要求in place的

评分

参与人数 2大米 +3 收起 理由
kuboy + 2 给你点个赞!
mmxmw + 1 赞!

查看全部评分

回复

使用道具 举报

🔗
magicsets 2019-8-4 15:32:25 | 只看该作者
全局:
上面楼说的in-place问题是对的,你把
  1. return nums[:i] + a
复制代码

改成下面两行
  1. nums[:] = nums[:i] + a
  2. return
复制代码

就可以通过了

至于为什么IDE可以通过而LeetCode不通过,是因为你的本地代码使用了nextPermutation的返回值:
  1. print(x.nextPermutation(nums))
复制代码

而LeetCode测试这道题的代码应该是类似于这样:
  1. x.nextPermutation(nums)
  2. print(nums)
复制代码

评分

参与人数 2大米 +3 收起 理由
kuboy + 2 很有用的信息!
mmxmw + 1 完全正确,非常赞!

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册账号
隐私提醒:
  • ☑ 禁止发布广告,拉群,贴个人联系方式:找人请去🔗同学同事飞友,拉群请去🔗拉群结伴,广告请去🔗跳蚤市场,和 🔗租房广告|找室友
  • ☑ 论坛内容在发帖 30 分钟内可以编辑,过后则不能删帖。为防止被骚扰甚至人肉,不要公开留微信等联系方式,如有需求请以论坛私信方式发送。
  • ☑ 干货版块可免费使用 🔗超级匿名:面经(美国面经、中国面经、数科面经、PM面经),抖包袱(美国、中国)和录取汇报、定位选校版
  • ☑ 查阅全站 🔗各种匿名方法

本版积分规则

>
快速回复 返回顶部 返回列表