LeetCode136:Single Number
题目:
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与任何数异或结果为原数
也可先进行排序再遍历排序后的数组,当某个数没有重复时,结果就是它了
也可用bitmap进行,不多说了。
实现代码:
#include <iostream> using namespace std;
/*
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?
*/
class Solution {
public:
int singleNumber(int A[], int n) {
int ret = ;
for(int i = ; i < n; i++)
ret ^= A[i];
return ret; }
}; int main(void)
{
int arr[] = {,,,,,,};
int len = sizeof(arr) / sizeof(arr[]);
Solution solution;
int once = solution.singleNumber(arr, len);
cout<<once<<endl;
return ;
}
LeetCode136:Single Number的更多相关文章
- 异或巧用:Single Number
异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...
- LeetCode137:Single Number II
题目: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode之“散列表”:Single Number
题目链接 题目要求: Given an array of integers, every element appears twice except for one. Find that single ...
- lintcode 中等题:Single number III 落单的数III
题目 落单的数 III 给出2*n + 2个的数字,除其中两个数字之外其他每个数字均出现两次,找到这两个数字. 样例 给出 [1,2,2,3,4,4,5,3],返回 1和5 挑战 O(n)时间复杂度, ...
- leetcode:Single Number
public int SingleNumber(int[] nums) { if(nums==null||nums.Length%2==0) return 0; int ret=nums[0]; fo ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- [LeetCode] Single Number II 单独的数字之二
Given an array of integers, every element appears three times except for one. Find that single one. ...
- [LeetCode] Single Number 单独的数字
Given an array of integers, every element appears twice except for one. Find that single one. Note:Y ...
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
随机推荐
- 部分2020年交期的PR回写到SAP中
描述:此问题一直存在,只是用户没有发现,最近提出,部分2020年交期的PR回写到SAP中 优化: SELECT MAX (PR.ORDERID), PR.ITEM, SUBSTR (PR.RECOMM ...
- SharePoint 2010 图表控件
需求: 统计每周的事件创建数量及关闭数量 以折线的形式显示 一张图表显示两条折线 知识点: 图表控件的使用 外部内容类型 数据库(sql)view(数据源) 结果:
- Finding Hotels
Finding Hotels http://acm.hdu.edu.cn/showproblem.php?pid=5992 Time Limit: 2000/1000 MS (Java/Others) ...
- 表单提交的两种请求方式:post与get。post与get两者的对比分析
post与get两者的对比分析:
- jetty 8.0 add filter example
http://zyn010101.iteye.com/blog/1679798 package com.cicc.gaf.sso.server;import java.io.IOException;i ...
- python使用ip代理抓取网页
在抓取一个网站的信息时,如果我们进行频繁的访问,就很有可能被网站检测到而被屏蔽,解决这个问题的方法就是使用ip代理 .在我们接入因特网进行上网时,我们的电脑都会被分配一个全球唯一地ip地址供我们使用, ...
- PAT 1074 宇宙无敌加法器(20)(代码+思路+测试点分析)
1074 宇宙无敌加法器(20 分)提问 地球人习惯使用十进制数,并且默认一个数字的每一位都是十进制的.而在 PAT 星人开挂的世界里,每个数字的每一位都是不同进制的,这种神奇的数字称为"P ...
- 用java创建UDF,并用于Hive
典型代码如下: 导入UDF类: import org.apache.hadoop.hive.ql.exec.UDF; public class UpperCassUDF extends UDF{ pu ...
- apache的80端口被占用
1.netstart -ano | findstr "80":查看80端口是否被占用,并找出对应的pid 2.关掉pid对应的进程
- 在cygwin中执行bat文件
在cygwin中执行bat文件 #!/bin/bash dir_gen_image_bat=C:/MinGW/msys/1.0/home/Administrator/feitian_audio/FOT ...