学习目标:
- 学习算法从易到难,show coding time, 动手练习的时候到了
学习内容:
例如:一个整数数组 nums 。如果任一值在数组中出现 至少两次 ,返回 true ;如果数组中每个元素互不相同,返回 false 。
提示:{9, 1, 2, 1, 6, 3, 0, 4} 返回 true ; {1, 2, 3, 6, 4} 返回 false
方案一:采用set集合
/**
* 用set集合去重
* @param nums
* @return
*/
public static boolean removeDuplicates(int[] nums) {
int size = nums.length;
if (size <= 0) {
return false;
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < nums.length; i++) {
if (!set.add(nums[i])) {
return true;
}
}
return false;
}
方案二:通过for循环遍历方式匹配是否相等 时间复杂度为O(n^2)
/**
* 循环遍历方式进行匹配 返回结果
* @param nums
* @return
*/
public static boolean removeDuplicates(int[] nums) {
int size = nums.length;
if (size <= 0) {
return false;
}
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] == nums[j]){
return true;
}
}
}
return false;
}
学习时间:
- 每天有所收获,每天记录下来,日积月累。
学习产出:
例如:
- 技术笔记 1 遍