Python3解leetcode Single Number
问题描述:
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
Input: [2,2,1]
Output: 1
Example 2:
Input: [4,1,2,1,2]
Output: 4
思路:
考虑异或运算, 例如5^6,就是101^110,结果是011.即两个完全相同的数字进行异或运算,得到的结果为0,。
根据以上特性,将所有num中的数字依次进行异或运算,则其中重复的项经过运算为0,最终结果即单个的项
又由于尽量不适用其他内存,因而考虑将每一步异或结果放置于num[0]的位置
代码:
class Solution:
def singleNumber(self, nums: List[int]) -> int:
for i in nums[1:]:
nums[0] = i^nums[0]
return nums[0]
Python3解leetcode Single Number的更多相关文章
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- LeetCode Single Number I / II / III
[1]LeetCode 136 Single Number 题意:奇数个数,其中除了一个数只出现一次外,其他数都是成对出现,比如1,2,2,3,3...,求出该单个数. 解法:容易想到异或的性质,两个 ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- LeetCode Single Number III
原题链接在这里:https://leetcode.com/problems/single-number-iii/ 题目: Given an array of numbers nums, in whic ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- LeetCode——Single Number II(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode: Single Number I && II
I title: Given an array of integers, every element appears twice except for one. Find that single on ...
随机推荐
- Perl语言学习笔记 15 智能匹配与give-when结构
1.智能匹配操作符 替代绑定操作符: 在哈希中查找某一个键: 比較两个数组是否全然同样: 查找列表中是否存在某个元素: 智能匹配操作符与顺序无关.~~ 左右元素能够互换 2.智能操作符优先级 3.gi ...
- programming review (c++): (3)graph, binary search
I.graph #include <iostream> #include <vector> using namespace std; vector<vector<, ...
- phpstudy nginx下curl请求本地其他项目
curl 请求的时候 如果用post请求,传递参数为 数组的时候 header 头 会被设置为 multipart/form-data 如果是字符串 形式 header 头会被设置为applica ...
- [python学习] 简单爬取图片站点图库中图片
近期老师让学习Python与维基百科相关的知识,无聊之中用Python简单做了个爬取"游讯网图库"中的图片,由于每次点击下一张感觉很浪费时间又繁琐.主要分享的是怎样爬取HTML的知 ...
- IOS GameCenter验证登陆
#import "GameKitHelper.h" #import "GameConstants.h" @interface GameKitHelper () ...
- 总是想把Linux服务器上的重要文件备份到本地,在此转一篇实现windows和linux互传文件的文章
尝试从windows xp向ubuntu11.10传文件 ubuntu使用的是ssh windows使用的是putty和其附带的pscp 首先配置ubuntu: 1.先使用netstat -tl或se ...
- HTML5(lufylegend.js练习)
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- Java for LeetCode 097 Interleaving String 【HARD】
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example, Given: s1 ...
- JETSON TK1~Ubuntu14.04 Armhf源更新
Ubuntu armhf版本的源网址不同于普通Ubuntu系统,如果采用如下网址会出现问题,导致sudo apt-get update出现Error. 之前的连接: deb http://archiv ...
- spring-boot2代码
App.java package com.kfit; import org.springframework.boot.SpringApplication; import org.springframe ...