welcome to Smartcat's cnblog

LeetCode练题——53. Maximum Subarray

1、题目

 

 

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

示例 1:

输入: [2,2,1]
输出: 1

示例 2:

输入: [4,1,2,1,2]
输出: 4

 

2、我的解答(copy力扣官方答案)

 

 1 # -*- coding: utf-8 -*-
 2 # @Time    : 2020/3/14 23:09
 3 # @Author  : SmartCat0929
 4 # @Email   : 1027699719@qq.com
 5 # @Link    : https://github.com/SmartCat0929
 6 # @Site    : 
 7 # @File    : 136. Single Number.py
 8 from typing import List
 9 
10 
11 class Solution:
12     def singleNumber(self, nums: List[int]) -> int:
13         # 亦或运算
14         a = 0
15         for i in nums:
16             a ^= i
17         return a
18 
19 
20 print(Solution().singleNumber([7, 1, 1, 7, 6, 5, 6]))

 

posted @ 2020-03-14 23:42  聪明猫  阅读(77)  评论(0)    收藏  举报