zoukankan      html  css  js  c++  java
  • 数组中只出现一次的数字

    题目描述

    一个整型数组里除了两个数字之外,其他的数字都出现了两次。请写程序找出这两个只出现一次的数字。
     
     1 class Solution {
     2 public:
     3     void FindNumsAppearOnce(vector<int> data,int* num1,int *num2) {
     4         map<int,int> mm;
     5         int len = data.size();
     6         if (len <=1)
     7         {
     8                 *num1 = 0;
     9                 *num2 = 0;
    10                 return ;
    11         }
    12         for (int i = 0 ;i < len ; ++i)
    13         {
    14             if(mm.count(data[i]) == 0)
    15             {
    16                 mm[data[i]] = 1;
    17             }
    18             else
    19             {
    20                 ++mm[data[i]];
    21             }
    22         }
    23         int cnt = 1;
    24         for (map<int,int>::iterator it = mm.begin() ; it != mm.end() ; ++it)
    25         {
    26             if(it->second == 1)
    27             {
    28                 if(cnt == 1)
    29                 {
    30                     *num1 = it->first;
    31                     --cnt;
    32                 }
    33                 else
    34                 {
    35                     *num2 = it->first;
    36                     break;
    37                 }
    38             }
    39         }
    40     }
    41 };
  • 相关阅读:
    except与besides
    think用法
    walk用法
    complain用法
    go through用法
    herd用法
    ridiculous用法
    it is the same as用法
    let us say用法
    1002 A+B for Polynomials (25 分)(模拟)
  • 原文地址:https://www.cnblogs.com/xiaoyesoso/p/5160274.html
Copyright © 2011-2022 走看看