LeetCode
来源:互联网 发布:大连住电软件 编辑:程序博客网 时间:2024/06/03 21:49
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?
很经典的面试题,找出数组中唯一一个只出现一次的数。
做法是把所有的数异或起来,因为两个相同的数异或为0,所以数组中两两异或为0,最后就剩下那个只出现过一次的数。时间复杂度O(n),空间复杂度O(1)
class Solution {public: int singleNumber(vector<int>& nums) { if (nums.empty()) return 0; int ans = 0; for(int i = 0; i < nums.size(); ++i) { ans = nums[i] ^ ans; } return ans; }};
阅读全文
0 0
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode
- leetcode:
- leetcode:
- LeetCode
- leetcode
- LEETCODE
- leetcode
- leetCode
- leetcode
- [leetcode]
- LeetCode
- leetcode
- leetcode:
- leetcode
- 原码,反码,补码,详解
- Myeclipse导入包出现不了Referenced Libraries
- Python中的MySQLConnector使用介绍
- ZCMU—1949
- 【IOS学习之常见问题】 App上架之发布App到Appstore 2(Application Loader的使用)
- LeetCode
- 掌握Maven_1
- Java基础拾遗:线程相关问题
- PX4源码分析6_uorb通信机制
- Hadoop--Hive安装错误解析
- JSP内置对象:在同一个页面实现成绩输入及总成绩显示
- 数据库中的事务是什么
- 第一天
- vim 插件 Ycm安装