zoukankan      html  css  js  c++  java
  • [LeetCode#136, 137]Single Number, Single Number 2

    The question: Single Number

    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?

    My analysis:

    This problem could be solved in two very tricky ways.
    At here, I only use "XOR" method, and I would discuss the methond of using "digits counting" later.
    The key idea: the property of XOR.
    x ^ x = 0
    x ^ y ^ x = y (the order could in random arrangement)
    Thus for this problem. Since we have only one number appear once, other number apper perfectly twice. 
    we XOR all numbers in the array, and we would finally get the number that only appears once.
    int ret = A[0];
    for (int i = 1; i < A.length; i++) {
            ret ^= A[i];
    }

    My solution:

    public class Solution {
        public int singleNumber(int[] A) {
            if (A.length == 0 || A == null)
                return 0;
                
            int ret = A[0];
            for (int i = 1; i < A.length; i++) {
                ret ^= A[i];
            }
            return ret;
        }
    }

    The question: Single Number II

    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?

    My analysis:

    This problem involvs many skills in mainpulating on a Integer.
    The basic idea is:
    The single number's each digit would only appear once, while other number's each digit would appear third times.
    We count the appearance of each digts of all number in the array, then the digit does not appear times of three is a digit of the single number. 
    The skills in implementation:
    1. how to get a interger's binary representation.
        1.1 we need first have a interger array int[32] to record the appearance for each digit.
        1.2 we use "digit" operator to move the target digit to the last digit, and use '&' to get the digit.
            for (int i = 0; i < 32; i++) {  //elegant while loop 
                for (int j = 0; j < A.length; j++) {
                    num[i] += (A[j] >> i) & 1;   
                }
            }
        Note: if we want to get the digit at i, we move it rightward i-1 digits. Note here num[i] is the counter the digits appear 
        at i+1 th position.
    
    2. how to recover the integer from digits?
        for (int i = 0; i < 32; i++) {
            ret += (num[i] % 3) << i;//note the num[i] store the digit at position i+1.
        }

    My solution:

    public class Solution {
        public int singleNumber(int[] A) {
            int[] num = new int[32];
            int ret = 0;
            
            for (int i = 0; i < 32; i++) {
                for (int j = 0; j < A.length; j++) {
                    num[i] += (A[j] >> i) & 1;   
                }
            }
            
            for (int i = 0; i < 32; i++) {
                ret += (num[i] % 3) << i;
            }
            
            return ret;
        }
    }
  • 相关阅读:
    PKU JudgeOnline 题目分类
    调试时拼凑带端口的完整网址/域名
    智能电脑监控器,完美解决想监控别人在自己电脑上的一切操作。
    如何清理LDF文件
    使用母版页后FindConttol需要注意
    【外刊IT评论】代码覆盖率:80%,不能少
    推荐2本普通人参悟的书
    处理在母版页加AJAX环境下处理滚动条回发保持不动的问题
    虚拟目录中的web.config不被上级目录的web.config影响的处理
    C++中^符号的意思
  • 原文地址:https://www.cnblogs.com/airwindow/p/4230060.html
Copyright © 2011-2022 走看看