【HackerRank】Lonely Integer
There are N integers in an array A. All but one integer occur in pairs. Your task is to find out the number that occurs only once.
Input Format
The first line of the input contains an integer N indicating number of integers.
The next line contains N space separated integers that form the array A.
Constraints
1 <= N < 100
N % 2 = 1 ( N is an odd number )
0 <= A[i] <= 100, ∀ i ∈ [1, N]
Output Format
Output S, the number that occurs only once.
常见的题:数组中除了一个数,其他数都是成对出现的。要求找出只出现了一次的这个数。
利用a xor a = 0 和 0 xor a = a这两个公式,设置一个数answer初始化为0,然后依次和数组中每个数异或,最后answer中存储的就是答案了。
代码如下:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*; public class Solution {
static int lonelyinteger(int[] a) {
int answer = 0;
for(int i = 0;i < a.length;i++)
answer = answer ^ a[i];
return answer; }
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int res; int _a_size = Integer.parseInt(in.nextLine());
int[] _a = new int[_a_size];
int _a_item;
String next = in.nextLine();
String[] next_split = next.split(" "); for(int _a_i = 0; _a_i < _a_size; _a_i++) {
_a_item = Integer.parseInt(next_split[_a_i]);
_a[_a_i] = _a_item;
} res = lonelyinteger(_a);
System.out.println(res); }
}
【HackerRank】Lonely Integer的更多相关文章
- 【LeetCode】397. Integer Replacement 解题报告(Python)
[LeetCode]397. Integer Replacement 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/inte ...
- 【HackerRank】Running Time of Quicksort
题目链接:Running Time of Quicksort Challenge In practice, how much faster is Quicksort (in-place) than I ...
- 【leetcode】Reverse Integer(middle)☆
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 总结:处理整数溢出 ...
- 【leetcode】Reverse Integer
题目描述: Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 很简单 ...
- 【LeetCode】273. Integer to English Words
Integer to English Words Convert a non-negative integer to its english words representation. Given i ...
- 【LeetCode】12. Integer to Roman 整型数转罗马数
题目: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from ...
- 【leetcode】12. Integer to Roman
题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...
- 【转】【java】论integer是地址传递还是值传递
转自:http://www.tuicool.com/articles/AraaQbZ 论integer是地址传递还是值传递 Integer 作为传参的时候是地址传递 , 可以参考如下例子,在程序刚启动 ...
- 【LeetCode7】Reverse Integer★
题目描述: 解题思路: 反转的方法很简单,重点在于判断溢出的问题,下面给出了两种方法. Java代码: 方法一: 判断溢出方法:在执行完int newResult=result*10+tail语句后, ...
随机推荐
- int abs(int number)函数有感: 求补码和通过补码求对应的整数 C++(增加:数字的二进制表示中1的个数)
#include "limits.h" #include "math.h" int abs(int number) { int const mask = num ...
- hdu2068 RPG的错排 错排+组合
RPG的错排 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- Android_Exception_Solution Lib
1) Q: java.lang.ClassNotFoundException: Didn't find class "com.google.common.collect.Maps" ...
- [转]Maven2中snapshot快照库的使用
Post by 铁木箱子 in Java, 技术杂谈 on 2011-10-28 12:12. [转载声明] 转载时必须标注:本文来源于铁木箱子的博客http://www.mzone.cc[原文地址] ...
- 汇编 DOS的中断调用 INT 21H
DOS系统功能调用 这个汇编指令是用于提供DOS系统功能调用. 它是由DOS提供的一组实现特殊功能的子程序供程序猿在编写自己的程序时调用,以减轻编程的工作量. 分两种,re=view"> ...
- Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的频率分析
CODE: #!/usr/bin/python # -*- coding: utf-8 -*- ''' Created on 2014-7-2 @author: guaguastd @name: tw ...
- Win7系统安装 MySQL 5.7.23
1. 下载 MySQL 5.7版本:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 2. 解压到指定文件夹,mysql根目录下创建my ...
- vs报错找不到错在哪里!Validation failed for one or more entities
今天在处理Entity Framework修改数据库时,报错: Validation failed for one or more entities. See 'EntityValidationErr ...
- [Noip2016]天天爱跑步 LCA+DFS
[Noip2016]天天爱跑步 Description 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.?天天爱跑步?是一个养成类游戏,需要玩家每天按时上线,完成打卡任 ...
- jQuery实现鼠标放到图片上,放大图片
<script src="../../Script/jquery-1.7.2.js" type="text/javascript"></scr ...