LeetCode刷题之一:寻找只出现一次的数字

投简历的时候看到了个刷题网站,http://www.nowcoder.com/527604,就做了一套题,现记录下来。

题目为:

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?

解题思路为:其他数字都出现两次,只有一个数字出现一次,思考要用什么方法才能让那些出现两次的数字经过某个操作能相互消除呢?那就是异或操作

代码为:

public class Solution {
    public int singleNumber(int[] A) {
        int result = 0;
        for(int number: A)
            result = result ^ number;
        return result;
    }
}
时间: 2024-10-12 02:58:17

LeetCode刷题之一:寻找只出现一次的数字的相关文章

leetcode刷题四<寻找两个有序数组的中位数>

给定两个大小为 m 和 n 的有序数组 nums1 和 nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1 和 nums2 不会同时为空. 示例 1: nums1 = [1, 3] nums2 = [2] 则中位数是 2.0 示例 2: nums1 = [1, 2] nums2 = [3, 4] 则中位数是 (2 + 3)/2 = 2.5 思路简单直接撸代码吧 double findMedianSortedArrays(

【leetcode刷题笔记】Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example,Given [100, 4, 200, 1, 3, 2],The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run in

【leetcode刷题笔记】Permutations II

Given a collection of numbers that might contain duplicates, return all possible unique permutations. For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1,1]. 题解:跟Permutation差不多,只是这次会有重复的元素,如下图所示,如果只用DFS的话就会产生重复的排列: 上

【leetcode刷题笔记】Search a 2D Matrix

Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties: Integers in each row are sorted from left to right. The first integer of each row is greater than the last integer of the previous ro

leetcode 刷题之路 76 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once. For example, Given 1->1->2, return 1->2. Given 1->1->2->3->3, return 1->2->3. 删除排序链表中重复的节点,删除操作完成后,原链表中的重复节点只保留一个. 思路,遍历链表,如果当前节点和下一个节点重复

【leetcode刷题笔记】Anagrams

Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题解: 所谓的anagrams,只若干个词,它们包含的字母的个数和种类完全一样,只是字符的顺序不一样.比如说bus,usb,sub就是一组angrams.同一组angrams具有排序后相同的特点,比如对上述三个单词按字典序排序分别得到bsu,bsu,bsu.我们用这一点

【leetcode刷题笔记】3Sum

Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. Note: Elements in a triplet (a,b,c) must be in non-descending order. (ie, a ≤ b ≤ c) The solut

【leetcode刷题笔记】Divide Two Integers

Divide two integers without using multiplication, division and mod operator. 题解:要求不用乘除和取模运算实现两个数的除法. 那么用加减法是很自然的选择.不过如果一次只从被除数中剪掉一个除数会TLE.所以我们借助移位运算,依次从被除数中减去1个除数,2个除数,4个除数......当减不动的时候,再依次从被除数中减去......4个除数,2个除数,1个除数. 例如50除以5的计算过程如下: dividend exp tem

leetcode 刷题之路 84 Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 思路,根据数组构成的特点可知,数组中所有数的某一位上1的个数总和为3*n或者3*n+1. 当只出现一次的整数该位为0时,总和为3*n,当只出现一次的整数该位为1时,总和为3*n+1. 因此我们可以计算数

【leetcode刷题笔记】Triangle

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. For example, given the following triangle [ [2], [3,4], [6,5,7], [4,1,8,3] ]  The minimum path sum from top to bottom is 11 (