Swift算法8-Single Number

Given an 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?

class Solution {
    func singleNumber(var nums: [Int]) -> Int {
        //if nums.count == 1 { return nums[0] }
        for i in 1..<nums.count {
            nums[0] ^= nums[i]
        }
        return nums[0]
    }
}

well why i didn't check num.count == 1, it says an array of integerS. "S".
So i don't think it's necessary for me to check it.

and for ^: a ^ b = b ^ a, a ^ a = 0, a ^ 0 = a.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,493评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 14,354评论 0 33
  • 立秋了,太阳落山后气温就跟着凉爽起来,没有了夏天让人喘不过气的闷热。晚饭后带着他奶奶带着俩宝去散步,我吃的...
    逆时空成长阅读 1,756评论 0 1
  • 小贤妻送我2包白燕面包粉,这面包粉做起来的面包非常不错。于是我一家一家的做,这周末准备给老婶做,因为周五姑姑和小叔...
    mimi播报阅读 1,423评论 2 4

友情链接更多精彩内容