`
cozilla
  • 浏览: 89203 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

[Leetcode] Single Number 1 & 2

 
阅读更多

Single Number

 
AC Rate: 1243/2713
My Submissions

 

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 {
public:
    int singleNumber(int A[], int n) {
        if (n == 0) return 0;
        int x = A[0];
        for (int i = 1; i < n; i++) x^=A[i];
        return x;
    }
};

 

 

Single Number II

 
AC Rate: 657/2247
My Submissions

 

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?

 

 

class Solution {
public:
    int singleNumber(int A[], int n) {
        int x[32];
        memset(x, 0, sizeof(x));
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < 32; j++) {
                x[j] += (A[i] >> j) & (1);
                x[j] %= 3;
            }
        }
        int res = 0;
        for (int i = 0; i < 32; i++) {
            res += (x[i] << i);
        }
        return res;
    }
};

 

 

分享到:
评论

相关推荐

    leetcode Single Number II - 位运算处理数组中的数 - 代金桥 - 博客园1

    扩展二:给定一个包含n个整数的数组,有一个整数x出现b次,一个整数y出现c次,其他所有的数均出现a次,其中b和c均不是a的倍数,找出x和y。中每一位二进制位1出

    LeetCode最全代码

    136 | [Single Number](https://leetcode.com/problems/single-number/) | [C++](./C++/single-number.cpp) [Python](./Python/single-number.py) | _O(n)_ | _O(1)_ | Easy ||| 137 | [Single Number II]...

    LeetCode2 Add Two Numbers

    The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. You may assume the two numbers do not contain any leading ...

    颜色分类leetcode-leetcode.etc:OJ、leetcode等解决方案

    颜色分类leetcode leetcode.etc My solutions of the problems in Online judge website, leetcode, lintcode, etc. leetcode: 13 problems solved lintcode: 49 problems solved Title URL Solution Difficulty ...

    Leetcode的ac是什么意思-LeetCodeInJava:leetcode-java

    Leetcode的ac是什么意思 LeetCodeInJava List #98 Validate Binary Search Tree #100 Same Tree #104 Maximum Depth of Binary Tree #122 Best Time to Buy and Sell Stock II #136 Single Number #150 Evaluate ...

    leetcode答案-leetcode-java:leetcode的Java代码

    leetcode 答案leetcode-java leetcode.com 的 Java 答案 ================索引================ ...Single Number com.leetcode.tree Balanced Binary Tree Maximum Depth of Binary Tree Same Tree

    leetcode切割分组-leetcode:leetcode

    136_single_number.py # 位操作:异或(xor)操作 x ^ 0 = x; x ^ x = 0 sum 001_two_sum.py # 求list中能加和成指定值的两个位置 015_3_sum**.py # 求list中能加和成0的三个值 数列 004_median_of_two_sorted_arrays....

    leetcode分类-LeetCode:LeetCode在线裁判C++

    各种SingleNumber变种 LinkListCycle I II 其他变种 编程之美 Preorder Traversal Inorder Traver sal postorder 非递归 不用栈! 字符串常用操作 字符串 各种转换 Pow(x,n) 优化 Permutations (交换 取子集两种方式...

    LeetCode:LeetCode解决方案

    preorder-traversal链表reorder-list链表linked-list-cycle-ii链表linked-list-cycle动态规划word-break-ii动态规划word-break链表copy-list-with-random-pointer复杂度single-number-ii复杂度single-number动态规划

    leetcode和oj-leetCode:尝试一些OJ

    Single Number 52.2% Easy 371 两个整数的和 51.6% Easy 104 二叉树的最大深度 50.1% Easy 325% Add the Easy 389.数字 49.9% 简单 226 反转二叉树 48.9% 简单 283 移动零点 46.9% 简单 404 左叶总和 45.5% 简单 383...

    dna匹配leetcode-leetcode:leetcode刷题

    dna匹配 leetcode leetcode刷题--C++ ...Single Number 异或 Copy List with Random Pointer 单链表 map Max Points on a Line 斜率 map, int&gt; Fraction to Recurring Decimal map long long 正负号 Repeated DNA S

    LeetCode:LeetCode题解

    LeetCode LeetCode题解 传送门 # 标题 解决方案 困难 笔记 1个 简单的 3 简单的 7 简单的 9 简单的 22 中等的 26 简单的 27 Remove_Element ... Single_NumberII Java 中等的 167 Two_Sum_II_Input_

    leetcode答案-leetcode:leetcode

    Single Number 碰巧我知道异或的解法。如果不知道的话,想想还是有点费事的。 Maximum Depth of Binary Tree 这?也太简单了吧。。一行代码,一个尾递归搞定啊。。 终于想清楚了,leetcode的AC率应该是:在线编辑、...

    leetcode添加元素使和等于-LeetCode:力扣唱片

    leetcode添加元素使和等于 LeetCode LeetCode Record 归并快排的区别: 思想不同:归并从局部到整体,快排...single out the missing number. How? First, we need to understand the properties of XOR: firstly, XOR'

    leetcode用例构造-30-Days-LeetCodingChallenge:30天LeetCodingChallenge

    leetcode 用例构造30 天 LeetCodingChallenge C++ 最有效的解决方案 注意:在顶部添加以下内容以优化延迟 static int lambda_0 ...1: ...[2,2,1] ...singleNumber (vector&lt; int &gt;& nums) { int res = 0 ; fo

    leetcoderuntimeerrorjava-leetcode:面试准备的数据结构和算法

    singleNumber ( self , nums : List [ int ]) -&gt; int : 它是所谓的“类型提示”(或“函数注释”;自 Python 3.0 起可用)。 -&gt; List[int] 意味着函数应该返回一个整数列表。 nums: List[int], target: int 表示 ...

    LeetCode去除数组重复元素-Arithmetic-Swift:一些算法的swift实现

    1. Two Sum 两数相加 2. Add Two Numbers 链表翻转 61. Rotate List 最大不同 318. Maximum Product of Word Lengths 【这个题目LTE 复杂度已经降下了】 最长不重复字符串3. Longest Substring Without Repeating ...

    lrucacheleetcode-LeetCode_30Day:力扣30天挑战赛

    lru缓存leetcode 力扣_30天 力扣 30 天挑战赛 日 问题描述 问题和解决方案链接 Git 解决方案页面 1 SINGLE NUMBER 2 HAPPY NUMBER 3 MAXIMUM SUBARRAY 4 Move Zeroes 5 Best Time to Buy and Sell Stock II 6 GROUP ...

    leetcode2sumc-add-two-numbers-solution:我的LeetCodeC解决方案:添加两个数字

    single digit. Add the two numbers and return the sum as a linked list. You may assume the two numbers do not contain any leading zero, except the number 0 itself. Example 1: Input: l1 = [2,4,3], l2 = ...

    leetcode蓄水池JAVA-leetcode-with-[removed]:wrapped_gift:用各种解决方案和单元测试来练习Leetcode

    https://leetcode.com/problems/single-number/ level : - easy tags : - recursive - linked list solutions : - reverseList - runtime : 52 ms, beats 99.80% - memory : 35 MB, beats 47.37% - ...

Global site tag (gtag.js) - Google Analytics