- 浏览: 183612 次
- 性别:
- 来自: 济南
文章分类
最新评论
Given an array of integers, every element appears three times 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),找到这个元素,我们采取位运算来解决。每个整型的数都有32位,我们记录数组中所有元素每位上的和,然后与3进行模运算,这样计算完32位上的值,最后的结果就是出现一次的元素。因为出现三次的元素与三取模后肯定为0,只保留了出现一次的元素。代码如下:
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
给定一个数组,每个元素都出现了三次,只有一个元素出现一次,要求时间复杂度为O(n),空间复杂度为O(1),找到这个元素,我们采取位运算来解决。每个整型的数都有32位,我们记录数组中所有元素每位上的和,然后与3进行模运算,这样计算完32位上的值,最后的结果就是出现一次的元素。因为出现三次的元素与三取模后肯定为0,只保留了出现一次的元素。代码如下:
public class Solution { public int singleNumber(int[] nums) { if(nums == null) return -1; int result = 0; for(int i = 0; i < 32; i++) { int sumBit = 0; for(int j = 0; j < nums.length; j++) { sumBit += (nums[j] >> i) & 1; } result |= (sumBit % 3) << 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 581Given 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 674Write a program to find the nth ... -
Longest Increasing Path in a Matrix
2016-02-29 05:56 843Given 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 705For a undirected graph with tre ...
相关推荐
《位运算处理数组中的数——以LeetCode Single Number II为例》 在计算机科学中,位运算是一种高效且灵活的数据处理手段,尤其在处理数组中特定数值的问题时,它能展现出强大的能力。LeetCode上的Single Number II...
《Single Number算法详解与调试演示》 在编程领域,算法是解决问题的核心工具,而Single Number问题则是一个典型的算法题目。这个题目要求在一个整数数组中,找出唯一出现一次的数字,而其他数字都出现了两次。这是...
python python_leetcode题解之137_Single_Number_II
在本压缩包中,我们关注的是一个Python编程与LeetCode面试相关的题目——“只出现一次的数字II”(Single Number II)。这是一道常见的数据结构与算法问题,常常出现在求职面试中,对于考察应聘者对位操作、哈希表...
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:罗马数字转换...
- "数组中两个数字只出现一次"(Single Number II)则是进一步考察位运算技巧。 ** Miscellaneous(杂项)** 杂项部分包括了一些不那么容易归类的问题,如螺旋矩阵(Spiral Matrix)、整数转罗马数字(Integer to ...
Number(落单的数) 、 / Medium Single Number II(落单的数 II) 、 Medium Single Number III(落单的数 III) Medium Hash Function(哈希函数) Easy Space Replacement(空格替换) Easy Insert Interval Easy Two ...
python python_leetcode题解之136_Single_Number
137 | [Single Number II](https://leetcode.com/problems/single-number-ii/) | [C++](./C++/single-number-ii.cpp) [Python](./Python/single-number-ii.py) | _O(n)_ | _O(1)_ | Medium ||| 190 | [Reverse Bits]...
- **2.1.24 Single Number II** - 找出数组中只出现一次的数字,其余数字均出现三次。 - 实现思路:位操作,利用位掩码记录每位出现次数。 - **2.2 单链表** - **2.2.1 Add Two Numbers** - 两个链表表示的...
javascript js_leetcode题解之136-single-number.js
在Xcode中,开发者可以创建一个新的项目,选择Single View App模板,然后在主视图控制器中编写代码来实现随机数的生成。 在Objective-C中,我们可以使用`arc4random_uniform()`函数来生成指定范围内的随机数。这个...
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