Tag: None yield load return CEP appears break next stop
Leecode topics are described as follows:
Given a non-empty array of integers, each of the remaining elements appears two times, except when an element occurs only once. Find the element that only appears once.
Ideas:
The first thing to think about is the use of sorting, which makes it easy to find non-repeating elements after sorting.
Later on there is a more ingenious solution, that is, to use XOR to find non-repeating elements, because the repeated elements of the different or after the elimination of each other to 0, the last array of elements after the different or calculated result is the only non-repeating element.
Code:
class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ num = 0 for i in nums: num = num ^ i return numdef stringToIntegerList(input): return json.loads(input)def intToString(input): if input is None: input = 0 return str(input)def main(): import sys def readlines(): for line in sys.stdin: yield line.strip(‘\n‘) lines = readlines() while True: try: line = lines.next() nums = stringToIntegerList(line) ret = Solution().singleNumber(nums) out = intToString(ret) print out except StopIteration: breakif __name__ == ‘__main__‘: main()
Leecode next day-use XOR to find non-repeating elements in an array