- 浏览: 183557 次
- 性别:
- 来自: 济南
文章分类
最新评论
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?
给定一个数组,每个元素都出现了两次,只有一个元素出现一次,要求时间复杂度为O(n),空间复杂度为O(1),找到这个元素。我们可以采取位运算,两个相同的数异或结果为0,任何数异或0都为它本身,因此我们只需要将数组中的数全部进行异或运算最后的结果就是出现一次的数,这样时间复杂度为O(n),空间复杂度为O(1)。代码如下:
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
给定一个数组,每个元素都出现了两次,只有一个元素出现一次,要求时间复杂度为O(n),空间复杂度为O(1),找到这个元素。我们可以采取位运算,两个相同的数异或结果为0,任何数异或0都为它本身,因此我们只需要将数组中的数全部进行异或运算最后的结果就是出现一次的数,这样时间复杂度为O(n),空间复杂度为O(1)。代码如下:
public class Solution { public int singleNumber(int[] nums) { if(nums == null) return -1; int result = 0; for(int i = 0; i < nums.length; i++) result ^= nums[i]; return result; } }
发表评论
-
498. Diagonal Traverse
2019-11-15 13:52 265Given a matrix of M x N eleme ... -
496 Next Greater Element I
2019-11-14 13:50 267You are given two arrays (witho ... -
Word Break II
2016-03-09 03:15 384Given a string s and a dictiona ... -
Insert Interval
2016-03-08 02:11 374Given a set of non-overlapping ... -
Merge Intervals
2016-03-07 05:25 497Given a collection of intervals ... -
Merge k Sorted Lists
2016-03-07 04:03 563Merge k sorted linked lists and ... -
Multiply Strings
2016-03-06 07:27 475Given two numbers represented a ... -
N-Queens II
2016-03-06 03:06 664Follow up for N-Queens problem. ... -
N-Queens
2016-03-06 02:47 469The n-queens puzzle is the prob ... -
First Missing Positive
2016-03-05 03:09 429Given an unsorted integer array ... -
Spiral Matrix
2016-03-04 03:39 575Given a matrix of m x n element ... -
Trapping Rain Water
2016-03-04 02:54 580Given n non-negative integers r ... -
Repeated DNA Sequences
2016-03-03 03:10 426All DNA is composed of a series ... -
Increasing Triplet Subsequence
2016-03-02 02:48 898Given an unsorted array return ... -
Maximum Product of Word Lengths
2016-03-02 01:56 930Given a string array words, fin ... -
LRU Cache
2016-02-29 10:37 602Design and implement a data str ... -
Super Ugly Number
2016-02-29 07:07 673Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 842Given an integer matrix, find t ... -
Coin Change
2016-02-29 04:39 783You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
《Single Number算法详解与调试演示》 在编程领域,算法是解决问题的核心工具,而Single Number问题则是一个典型的算法题目。这个题目要求在一个整数数组中,找出唯一出现一次的数字,而其他数字都出现了两次。这是...
《位运算处理数组中的数——以LeetCode Single Number II为例》 在计算机科学中,位运算是一种高效且灵活的数据处理手段,尤其在处理数组中特定数值的问题时,它能展现出强大的能力。LeetCode上的Single Number II...
- `Solution` 类包含了一个名为 `singleNumber` 的方法,该方法接收一个整型数组 `nums` 作为参数。 - 在 `singleNumber` 方法内部,定义了一个变量 `result` 初始化为 0。 - 使用增强型 for 循环遍历数组 `nums`,...
def singleNumber(nums): single_num = 0 for num in nums: single_num ^= num return single_num ``` 在这个代码中,`single_num`初始值为0,然后遍历整个数组`nums`,对每个元素执行异或操作。由于每个元素...
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up ... and a single number counts as a sum.) Your job is to solve this problem in general.
public int singleNumber(int[] nums) { int res = 0; for (int n : nums) { res = res ^ n; } return res; } ``` **解析:** 这段代码巧妙地利用了异或运算的特性来解决问题。异或运算有以下特点: 1. 任何数...
vector<int> singleNumber(vector<int>& nums) { int xor_two = nums[0]; int last_bit = 0; vector<int> result = {0,0}; for(int i = 1; i (); i++) xor_two ^= nums[i]; last_bit = xor_two & (~(xor_two ...
function singleNumber($nums) { $result = 0; foreach ($nums as $num) { $result ^= $num; } return $result; } ``` 这段代码中,`$result`初始值为0,然后遍历数组`$nums`,对每个元素执行异或操作,最终...
python python_leetcode题解之136_Single_Number
The output will be a single number, the average (mean) of the closing balances for the twelve months. It will be rounded to the nearest penny, preceded immediately by a dollar sign, and followed by ...
function singleNumber(nums) { let i = 0, j = nums.length - 1; while (i ) { if (nums[i] === nums[j]) { i++; j--; } else { return nums[i] !== nums[++i] ? nums[i] : nums[j]; } } return nums[i];...
在本例中,`resource.h`可能是资源定义的头文件,而`SingleNumber.rc`则包含了实际的资源脚本,比如游戏界面的图标(icon1.ico)和可能的位图资源(numbers1.bmp、numbers2.bmp、numbers.bmp)。这些位图可能用于在...
javascript js_leetcode题解之136-single-number.js
System.out.println("Single number: " + num); } public void printNumbers(int... nums) { System.out.println("Multiple numbers:"); for (int num : nums) { System.out.println(num); } } ``` 这样,当...
python python_leetcode题解之137_Single_Number_II
`CREATE PROCEDURE` 语句定义了一个名为 `proc_parameterNumber` 的存储过程,接收输入参数 `@SingleNumberId` 和输出参数 `@SingleNumber`。这个过程检查是否存在指定的 `SingleNumberId`,并更新或设置相应的 `@...
A checksum is an algorithm that scans a packet of data and returns a single number. The idea is that if the packet is changed, the checksum will also change, so checksums are often used for detecting ...
34. Single Number II:数组中除一个数字外,其他数字都出现了三次,找出只出现一次的数字。 六、其他 35. Spiral Matrix:按螺旋方式打印矩阵。 36. Integer to Roman:将整数转换为罗马数字。 37. Roman to ...
34. Single Number II:找出数组中唯一不出现一次的数字,数组中每个数字出现三次。 【杂项】 35. Spiral Matrix:螺旋遍历矩阵。 36. Integer to Roman:整数转换成罗马数字。 37. Roman to Integer:罗马数字转换...