zoukankan      html  css  js  c++  java
  • LinCode落单的数

    easy 落单的数

    60%
    通过

    给出2*n + 1 个的数字,除当中一个数字之外其它每一个数字均出现两次。找到这个数字。


    您在真实的面试中是否遇到过这个题? 
    Yes
    例子

    给出 [1,2,2,1,3,4,3]。返回 4

    挑战

    一次遍历,常数级的额外空间复杂度

    位运算

    class Solution {
    public:
    	/**
    	 * @param A: Array of integers.
    	 * return: The single number.
    	 */
        int singleNumber(vector<int> &A) {
            // write your code here
            int a =0;
            for (auto x :A){
                a ^=x;
            }
            return a;
        }
    };
    

    暴力求解

    class Solution {
    public:
    	/**
    	 * @param A: Array of integers.
    	 * return: The single number.
    	 */
        int singleNumber(vector<int> &A) {
            // write your code here
            for (auto x:A){
                int i = 0;
                 for (auto y:A){
                     if(x == y){
                         ++i;
                     }
                 }
                 if (1==i){
                     return x;
                 }
            }
        }
    };
    


  • 相关阅读:
    Hive的架构和工作流程
    Hive的定义及搭建
    HBase API操作
    HBase相关概念简介
    HBase shell常用命令
    HBase的简介和搭建
    scrapy useragent
    scrapy settings
    scrapy中的request对象
    python语法
  • 原文地址:https://www.cnblogs.com/lxjshuju/p/6769111.html
Copyright © 2011-2022 走看看