程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Leetcode brushing Python] 260. Numbers that only appear once III

編輯:Python

1 topic

Given an integer array nums in which exactly two elements appear only once and all other elements appear twice.Find those two elements that appear only once.You can return answers in any order.

Advanced: Your algorithm should have linear time complexity.Can you do it using only constant space complexity?

Example 1:

Input: nums = [1,2,1,3,2,5]
Output: [3,5]
Explanation: [5, 3] is also a valid answer.

Example 2:

Input: nums = [-1,0]
Output: [-1,0]

2 parsing

Use a dictionary to store existing numbers and occurrences

3 Python implementation

class Solution:def singleNumber(self, nums: List[int]) -> List[int]:freq = collections.Counter(nums)res = []for k,v in freq.items():span>if v==1:res.append(k)return res

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved