Codility--- Distinct
Write a function
class Solution { public int solution(int[] A); }
that, given a zero-indexed array A consisting of N integers, returns the number of distinct values in array A.
Assume that:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−1,000,000..1,000,000].
For example, given array A consisting of six elements such that:
A[0] = 2 A[1] = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1
the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.
Complexity:
- expected worst-case time complexity is O(N*log(N));
- expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
import java.util.HashSet;
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
HashSet hs = new HashSet();
for(int i=0; i<A.length;i++) {
hs.add(A[i]);
}
return hs.size();
}
}
https://codility.com/demo/results/trainingB7XB2W-7ZM/
Codility--- Distinct的更多相关文章
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- SQL中distinct的用法
SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- Oracle 查询语句(where,order by ,like,in,distinct)
select * from production;alter table production add productionprice number(7,2); UPDATE production s ...
- Codility NumberSolitaire Solution
1.题目: A game for one player is played on a board consisting of N consecutive squares, numbered from ...
- codility flags solution
How to solve this HARD issue 1. Problem: A non-empty zero-indexed array A consisting of N integers i ...
- mysql中distinct的用法
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- distinct 与 group by 去重
例如下表格:表名:fruit id Name Price Num 1 西瓜 10 2 2 西瓜 11 2 3 香蕉 10 3 4 桃子 10 2 当我想获取Name不重复的数据,结果如下 id Nam ...
随机推荐
- mybatis在CRUD
一. 一个简短的引论: Mybatis本是apache的一个开源项目ibatis, 2010年这个项目由apache software foundation迁移到了google code, 而且改名为 ...
- mkdir-无法创建目录(单层目录中子目录的个数默认为32000个)
今天运行在一台机器上的脚本突然通知无法创建目录了,上去执行shell脚本,也出现同样的错误,如下: $ mkdir test mkdir: 无法创建目录"test": 过多的连接 ...
- linux tesseract识别名片
用tesseract识别名片,无任何训练 数字,字母识别的准确率比较高,没有错误,规范的汉字识别的还可以,比如名片背面,正面的就错误比较多了: 没有任何训练,识别的还算可以了:我们主要要的电话和QQ ...
- 我是怎么做App token认证的
使用Token来做身份认证在目前的移动客户端上非常流行,Token这个概念来源于OAuth认证,主要是在服务端实现.关于相关的原理,同学们自行百度.在这里,我简单介绍一下我是怎么具体实现的,重点描述t ...
- WPF制作的党旗
原文:WPF制作的党旗 --------------------------------------------------------------------------------引用或转载时请保 ...
- Hadoop源代码分析:HDFS读取和写入数据流控制(DataTransferThrottler类别)
DataTransferThrottler类别Datanode读取和写入数据时控制传输数据速率.这个类是线程安全的,它可以由多个线程共享. 用途是构建DataTransferThrottler对象,并 ...
- Ant—使用Ant构建一个简单的Java工程(两)
博客<Ant-使用Ant构建一个简单的Java项目(一)>演示了使用Ant工具构建简单的Java项目,接着这个样例来进一步学习Ant: 上面样例须要运行多条ant命令才干运行Test类中的 ...
- 逻辑回归原理介绍及Matlab实现
原文:逻辑回归原理介绍及Matlab实现 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/laobai1015/article/details/781 ...
- oracle_set运营商
oracle_set运营商 ①UNION/UNION ALL UNION 运算符返回的结果集,并设置两个查询.对于这两组重复部分的.重复数据删除 例: select id,name from t ...
- C# Thread 参数
Thread (ParameterizedThreadStart) 初始化 Thread 类的新实例,指定允许对象在线程启动时传递给线程的委托. Thread (ThreadStart) 初 ...