[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.
class Solution {
public:
int singleNumber(int A[], int n) {
int i,j;
for(i=; i<n; i++)
{
for(j=i+; j<n; j++)
{
if(A[j]==A[i])
{
int temp = A[i+];
A[i+] = A[j];
A[j] = temp;
i++;
break;
}
}
if(j==n)
return A[i];
}
}
};
上面的是传统方法,时间复杂度是O(n2),空间复杂度是O(1)。
class Solution {
public:
int singleNumber(int A[], int n) {
int result=;
for(int i=; i<n; i++)
result = result ^ A[i];
return result;
}
};
用位异或运算实现,时间复杂度和空间复杂度均为O(1).
运用了异或运算具有交换律和结合律的性质。
交换律: a^b = b^a
结合律: (a^b)^c = a^(b^c)
另外,a^a=0。
[LeetCode OJ] Single Number之一 ——Given an array of integers, every element appears twice except for one. Find that single one.的更多相关文章
- [LeetCode OJ] Single Number之二 ——Given an array of integers, every element appears THREE times except for one. Find that single one.
class Solution { public: int singleNumber(int A[], int n) { ; ; ; i<=bits; i++) { ; ; ; j<n; j ...
- LeetCode OJ 80. Remove Duplicates from Sorted Array II
题目 Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For ex ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode OJ -Happy Number
题目链接:https://leetcode.com/problems/happy-number/ 题目理解:实现isHappy函数,判断一个正整数是否为happy数 happy数:计算要判断的数的每一 ...
- LeetCode OJ 33. Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode OJ 26. Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and ret ...
- 【LeetCode OJ】Remove Duplicates from Sorted Array
题目:Given a sorted array, remove the duplicates in place such that each element appear only once and ...
- LeetCode OJ:Search in Rotated Sorted Array(翻转排序数组的查找)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- LeetCode OJ:Number of Islands(孤岛计数)
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
随机推荐
- AlgorithmsI PA2: Randomized Queues and Deques Deque
本次作业考察利用array 或者linked list 实现规定时间复杂度的queue 和stack, 不能使用java 自带的stack 和queue. 目的是让我们掌握自己设计的函数的复杂度. D ...
- [Poetize II]太鼓达人
描述 Description 鼓的主要元件是M个围成一圈的传感器.每个传 感器都有开和关两种工作状态,分别用1和0表示.显然,从不同的位置出发沿顺时针方向连续检查K个传感器可以得到M个长度为K的01串 ...
- 有关linux下redis overcommit_memory的问题(转)
一.背景 公司的redis有时background save db不成功,通过log发现下面的告警,很可能由它引起的: [13223] 17 Mar 13:18:02.207 # WARNING ov ...
- 字符串(后缀自动机):HDU 4622 Reincarnation
Reincarnation Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)To ...
- 动态规划(背包问题):HRBUST 1377 金明的预算方案
金明的预算方案 金明今天很开心,家里购置的新房就要领钥匙了,新房里有一间金明自己专用的很宽敞的房间.更让他高兴的是,妈妈昨天对他说:“你的房间需要购买哪些物品,怎么布置,你说了算,只要不超过N元钱就行 ...
- delphi record 内存怎么释放
delphi record 内存怎么释放 是不需要释放的,除非你使用指针方式生成的.
- Android 子线程请求ASP.NET后台
首先定义布局文件,及点击事件 public class MainActivity extends Activity { private final int MSG_HELLO = 0; private ...
- android画虚线的自定义VIew
package com.yesway.ycarplus.view; import android.annotation.SuppressLint; import android.content.Con ...
- KMP算法,Boyer-Moore算法
http://www-igm.univ-mlv.fr/~lecroq/string/node19.html 首先看普通的串的模式匹配算法开始讲起,才能更深入的了解KMP算法及其优点. 1. 普通的串的 ...
- [转载]Python兵器谱
转载自:http://www.52nlp.cn/python-网页爬虫-文本处理-科学计算-机器学习-数据挖掘 曾经因为NLTK的缘故开始学习Python,之后渐渐成为我工作中的第一辅助脚本语言,虽然 ...