Given an array of integers of size n, where all elements are between 1 and n inclusive, find all of the elements of [1, n] that do not appear in the array. Some numbers may appear more than once.
給定一個有 n 的元素的正整數列表,列表中的元素是 1 到 n 的正整數,找到 [1, n] 區間中缺少的正整數,列表中可能出現相同的數值。
Test Case:
Input1: nums = [4, 6, 2, 6, 7, 2, 1]Output1: [3, 5]【下面是個廣告位】
參考代碼(Python 3):
class Solution: def findDisappearedNumbers(self, nums): for idx in range(len(nums)): index = abs(nums[idx]) - 1 nums[index] = -abs(nums[index]) return [idx for idx, num in enumerate(nums, start = 1) if num > 0] nums = [4, 6, 2, 6, 7, 2, 1]result = Solution().findDisappearedNumbers(nums)print(result)print(''.join(str(result).split()))如果您對內容有什麼需求建議,或者遇到了什麼軟體、應用使用方面的問題,請直接在公眾號主界面下發私信給我,我看都會及時回復。感謝大家的支持!
合作: classroom.it@hotmail.com
Hi
你好呀
我是 M先森
這是一個共同學習的平臺
我會把我會的 你們想學的
做成教程或者寫成文章
我們在這一起學英語學編程 學算法
學各種各樣
好玩的好用的軟體
記得要常回來看看呀
(^_^)a(^_^)a
點擊 閱讀原文 查看Python算法實戰系列~
喜歡就 分享 點讚 在看 三連吧!
Long-press QR code to transfer me a reward
As required by Apple's new policy, the Reward feature has been disabled on Weixin for iOS. You can still reward an Official Account by transferring money via QR code.