码迷,mamicode.com
首页 > 其他好文 > 详细

【LeetCode】136.Excel Sheet Column Title

时间:2015-05-06 21:04:02      阅读:137      评论:0      收藏:0      [点我收藏+]

标签:

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?

 

Hide Tags
Hash Table Bit Manipulation
 题目要求是特定条件的,只有一个数字值出现一次
1 class Solution {
2 public:
3     int singleNumber(int A[], int n) {
4         int x;
5         for(size_t i=0; i<n; ++i)
6             x ^= A[i];
7         return x;
8     }
9 };
Status: Accepted
Runtime: 18 ms
Submitted: 3 months, 3 weeks ago
但是我觉得此处的x是未进行初始化的,查找C++Primer第五版,P40页有相应默认初始化定义:
“如果是内置类型的变量未被显示初始化,它的值由定义的位置决定。定义于任何函数体之外的变量被初始化为0。定义在函数体内部的内置类型变量将不被初始化。”
main函数也是函数,所以全局变量才会初始化为0,在类中的成员函数是否是初始化为0?这个还没有确定。
所以就修改了下
1 int singleNumber(int A[], int n) {
2     if (n < 1)
3         return NULL;
4     int x = A[0];
5     for(size_t i=1; i<n; ++i)
6         x ^= A[i];
7     return x;
8 }

此时竟然不AC了,提示Compile Error

Line 19: no matching function for call to ‘Solution::singleNumber(std::vector<int>&)’

【LeetCode】136.Excel Sheet Column Title

标签:

原文地址:http://www.cnblogs.com/helloWaston/p/4482984.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!