Jan 11 - Contains Duplicate; Array; Traverse; Integer;

遍历数组找出有没有重复的元素 首先想到用一个数组记录出现的元素的个数

代码:

public class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums.length == 0) return true;
int[] checkElement = new int[Integer.MAX_VALUE];
for(int i = 0; i < nums.length; i++){
if(checkElement[nums[i]] > 0) return false;
checkElement[nums[i]]++;
}
return true;
}
}

space cost太大: requested array size exceed VM limit

用hashmap来做吧:

public class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums.length == 0) return false;
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
if(map.containsKey(nums[i])) return true;
map.put(nums[i],1);
}
return false;
}
}

Accepted,但是runtime 不是很好。考虑下别的办法。

先把array排序一下 查了一下Arrays.sort() 用的是mergesort() runtime is O(nlog(n))

代码:

public class Solution {
public boolean containsDuplicate(int[] nums) {
if(nums.length == 0) return false;
Arrays.sort(nums);
//boolean[] checkElement = new boolean[15000];
//Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length-1; i++){
if(nums[i] == nums[i+1]) return true;
}
return false;
}
}

时间: 2024-10-13 11:52:06

Jan 11 - Contains Duplicate; Array; Traverse; Integer;的相关文章

Jan 11 - Contains Duplicate II; Array; Traverse; HashMap; HashSet;

代码: public class Solution { public boolean containsNearbyDuplicate(int[] nums, int k) { //int gap = nums.length; Map<Integer, Integer> map = new HashMap<>(); for(int i = 0; i < nums.length; i++){ Integer j = map.put(nums[i], i); if(j != nul

Oracle 11 g duplicate功能_复制dataguard备库

Qracle 11g duplicate功能 不用备份源库,通过网络复制出standby库 1.在standby上grid用户配置listener 注意是指定oracle用户的家目录: 监听状态: [[email protected] ~]$lsnrctl LSNRCTL for Linux:Version 11.2.0.4.0 - Production on 19-MAY-2014 18:46:15 Copyright (c)1991, 2013, Oracle.  All rights re

Jan 18 - Jump Game; Array; Greedy;

一开始以为是用DP解决,create a boolean array to store the status which mark whether the person can get to the end from current position. But, by this methods, we would encounter the worst case, which is there're hugh number of index and from which person would

217 Contains Duplicate (Array)

1 class Solution { 2 public boolean containsDuplicate(int[] nums) { 3 HashMap<Integer, Integer> m = new HashMap<Integer, Integer>(); 4 for(int i = 0; i < nums.length; i++) { 5 if (m.containsKey(nums[i])) { 6 return true; 7 }else { 8 m.put(n

Jan 14 - Power of three; Math; Integer; Digit;

No loop; No recursion; 我没找到规律 但是从讨论区看到别人的思路: If N is a power of 3: It follows that 3^X == N It follows that log (3^X) == log N It follows that X log 3 == log N It follows that X == (log N) / (log 3) For the basis to hold, X must be an integer. 代码: pu

Jan 19 - Unique Paths; Array; DP;

first of all, we're goin to create a 2D array(int[][] grid) to store the number of paths to each position. we get the minimum value of m and n, mark it as min, then look through the row and column of grid[i][i] in each loop, when i == 0, we know we a

Jan 17 - Permutations; BackTracking; Array; Recursion;

代码: public class Solution { List<List<Integer>> resultList = new ArrayList<>(); boolean flag = false; public List<List<Integer>> permute(int[] nums) { int len = nums.length; if(len == 0) return resultList; Arrays.sort(nums);

Jan 16 - Combination Sum; Array; BackTracking;

添加两个List:List<Integer> listSum 存储对应组合的和:List<List<Integer>> list 存储可能的组合 resultList存储和==target的组合 代码: public class Solution { public List<Integer> listSum; public List<List<Integer>> list; public List<List<Integer

C++11中的array

stl中的vector功能相比普通数据而言是要强大很多的,代价是需要动态的内存管理机制(分配,再分配,释放). 而有时候我们只需要普通的数组而已,这就带来了效率上的浪费. array就是用来代替普通的数组的,打开头文件,发现该模板中有如下语句: _Ty _Elems[_Size == 0 ? 1 : _Size]; 这是该模板类的数据成员,发现没,就是普通的数组,现在知道为什么它的第二个模板参数必须是const size_t了吧. 但是使用array模板相比使用普通的数据而言,有利于我们避开原始