- 浏览: 183446 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
给定一个数组,里面有两个数字出现了一次,其他都出现了两次,找出这两个元素,要求在线性时间内完成。我们用位运算来完成,首先将数组中所有的元素进行异或运算得到一个值helper,然后用helper & (~(helper - 1)) 这样就得到了一个某一位为1其他位全为0的数tell,并且1所在的位上两个单独出现的数肯定不同。我们通过tell将数组中的元素分为两部分,分别与tell进行位与运算,最终得到两个单独的数。代码如下:
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
The order of the result is not important. So in the above example, [5, 3] is also correct.
Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
给定一个数组,里面有两个数字出现了一次,其他都出现了两次,找出这两个元素,要求在线性时间内完成。我们用位运算来完成,首先将数组中所有的元素进行异或运算得到一个值helper,然后用helper & (~(helper - 1)) 这样就得到了一个某一位为1其他位全为0的数tell,并且1所在的位上两个单独出现的数肯定不同。我们通过tell将数组中的元素分为两部分,分别与tell进行位与运算,最终得到两个单独的数。代码如下:
public class Solution { public int[] singleNumber(int[] nums) { if(nums == null || nums.length < 2) return new int[0]; int helper = 0; for(int i = 0; i < nums.length; i++) { helper ^= nums[i]; } int tell = helper & (~(helper - 1)); int single1 = 0; int single2 = 0; for(int i = 0; i < nums.length; i++) { if((nums[i] & tell) == 0) { single1 ^= nums[i]; } else { single2 ^= nums[i]; } } int[] result = {single1, single2}; 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 672Write 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 782You are given coins of differen ... -
Minimum Height Trees
2016-02-29 04:11 704For a undirected graph with tre ...
相关推荐
《Single Number算法详解与调试演示》 在编程领域,算法是解决问题的核心工具,而Single Number问题则是一个典型的算法题目。这个题目要求在一个整数数组中,找出唯一出现一次的数字,而其他数字都出现了两次。这是...
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 ...
《位运算处理数组中的数——以LeetCode Single Number II为例》 在计算机科学中,位运算是一种高效且灵活的数据处理手段,尤其在处理数组中特定数值的问题时,它能展现出强大的能力。LeetCode上的Single Number II...
Number(落单的数) 、 / Medium Single Number II(落单的数 II) 、 Medium Single Number III(落单的数 III) Medium Hash Function(哈希函数) Easy Space Replacement(空格替换) Easy Insert Interval Easy Two ...
标题 "手稿_V1.097" 涉及的是一个编程问题,该问题源自 LeetCode 上的 "Single Number III" 题目。在 C++ 的背景下,我们需要找到一个整数数组中只出现一次的两个元素,而其他元素都出现两次。描述中的代码给出了一...
python python_leetcode题解之136_Single_Number
260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [C++](./C++/single-number-iii.cpp) [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium || 268| [Missing ...
Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same ...Single Number ...Number ...Single Number III #274 H-Index #283 Move Zeroes #292 Nim Game #318 Maximum P
javascript js_leetcode题解之136-single-number.js
python python_leetcode题解之137_Single_Number_II
在Xcode中,开发者可以创建一个新的项目,选择Single View App模板,然后在主视图控制器中编写代码来实现随机数的生成。 在Objective-C中,我们可以使用`arc4random_uniform()`函数来生成指定范围内的随机数。这个...
只出现一次的数字 III(Single Number III)**:这是一道关于数组和位操作的题目,要求找出数组中唯一一个只出现一次的数字,而其他数字都出现两次。可以利用异或操作实现。 7. **338. 计数质数(Counting Bits)**:...
As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve...
We consider the problem of estimating detailed 3D structure from a single still image of an unstructured environment. Our goal is to create 3D models that are both quantitatively accurate as well as ...
- `Solution` 类包含了一个名为 `singleNumber` 的方法,该方法接收一个整型数组 `nums` 作为参数。 - 在 `singleNumber` 方法内部,定义了一个变量 `result` 初始化为 0。 - 使用增强型 for 循环遍历数组 `nums`,...
Deep Mixture density network for voice conversion. Single layer Multi Layer Perceptron Neural Network with desired number of mixure components.
As each number is read, print it only if it is not a duplicate of a number already read. Prepare for the “worst case” in which all 20 numbers are different. Use the smallest possible array to solve...
PAT甲级 1024 Palindromic Number A number that will be the same when it is written... All single digit numbers are palindromic numbers. Non-palindromic numbers can be paired with palindromic ones via a se