【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中存储的就是答案了。

代码如下:

 1 import java.io.*;
 2 import java.util.*;
 3 import java.text.*;
 4 import java.math.*;
 5 import java.util.regex.*;
 6
 7 public class Solution {
 8 static int lonelyinteger(int[] a) {
 9     int answer = 0;
10     for(int i = 0;i < a.length;i++)
11         answer = answer ^ a[i];
12     return answer;
13
14     }
15 public static void main(String[] args) {
16         Scanner in = new Scanner(System.in);
17         int res;
18
19         int _a_size = Integer.parseInt(in.nextLine());
20         int[] _a = new int[_a_size];
21         int _a_item;
22         String next = in.nextLine();
23         String[] next_split = next.split(" ");
24
25         for(int _a_i = 0; _a_i < _a_size; _a_i++) {
26             _a_item = Integer.parseInt(next_split[_a_i]);
27             _a[_a_i] = _a_item;
28         }
29
30         res = lonelyinteger(_a);
31         System.out.println(res);
32
33     }
34 }

【HackerRank】Lonely Integer

时间: 2024-10-10 08:46:39

【HackerRank】Lonely Integer的相关文章

【HackerRank】Median

题目链接:Median 做了整整一天T_T 尝试了各种方法: 首先看了解答,可以用multiset,但是发现java不支持: 然后想起来用堆,这个基本思想其实很巧妙的,就是维护一个最大堆和最小堆,最大堆存放前半部分较小的元素,最小堆存放后半部分较大的元素,并且最大堆的所有元素小于最小堆的所有元素:保持最大堆最多比最小堆多一个元素.每次插入元素的时候都先插入到最大堆,如果发现最大堆比最小堆多了两个个,那么就从最大堆里面拿出最大的放到最小堆里面:如果发现最大堆里面新插入的元素破坏了最大堆所有元素小于

【HackerRank】Bus Station

有n组好朋友在公交车站前排队.第i组有ai个人.还有一辆公交车在路线上行驶.公交车的容量大小为x,即它可以同时运载x个人. 当车站来车时(车总是空载过来),一些组从会队头开始走向公交车. 当然,同一组的朋友不想分开,所以仅当公交车能容纳下整个组的时候,他们才会上车.另外,每个人不想失去自己的位置,即组的顺序不会改变. 问题时如何选择公交车的容量大小x使得它可以运走所有组的人,并且公交车每次从车站出发时没有空位?(在车里的总人数恰好达到x)? 输入格式 第一行只包含一个整数n.第二行包含n个空格分

【HackerRank】Sherlock and MiniMax

题目连接:Sherlock and MiniMax Watson gives Sherlock an array A1,A2...AN. He asks him to find an integer M between P and Q(both inclusive), such that, min {|Ai-M|, 1 ≤ i ≤ N} is maximised. If there are multiple solutions, print the smallest one. Input For

【HackerRank】 The Full Counting Sort

In this challenge you need to print the data that accompanies each integer in a list. In addition, if two strings have the same integers, you need to print the strings in their original order. Hence, your sorting algorithm should be stable, i.e. the

【HackerRank】QuickSort(稳定快排,空间复杂度O(n))

QuickSort In the previous challenge, you wrote a partition method to split an array into 2 sub-arrays, one containing smaller elements and one containing larger elements. This means you 'sorted' half the array with respect to the other half. Can you

【HackerRank】Find the Median(Partition找到数组中位数)

In the Quicksort challenges, you sorted an entire array. Sometimes, you just need specific information about a list of numbers, and doing a full sort would be unnecessary. Can you figure out a way to use your partition code to find the median in an a

【HackerRank】Utopian tree

The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occurs during the monsoon, when it doubles in height. The second growth cycle of the tree occurs during the summer, when its height increases by 1 meter.

【HackerRank】Pairs

题目链接:Pairs 完全就是Two Sum问题的变形!Two Sum问题是要求数组中和正好等于K的两个数,这个是求数组中两个数的差正好等于K的两个数.总结其实就是“骑驴找马”的问题:即当前遍历ar[i],那么只要看数组中是否存在ar[i]+K或者ar[i]-K就可以了,还是用HashMap在O(1)的时间完成这个操作. 题目有一点没说清楚的就是元素是否有重复,从Editorial来看似乎是没有重复,不过我还是用map的value记录了数出现的频率来处理了重复. 代码如下: 1 import j

【HackerRank】Closest Numbers

Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses also. Challenge Given a list of unsorted numbers, can you find the numbers that have the smallest absol