场景:
刷力扣题,简单题
问题描述:
给定一个整数数组,判断是否存在重复元素。
如果存在一值在数组中出现至少两次,函数返回 true 。如果数组中每个元素都不相同,则返回 false 。
示例 1:
输入: [1,2,3,1]
输出: true
示例 2:
输入: [1,2,3,4]
输出: false
示例 3:
输入: [1,1,1,3,3,4,3,2,4,2]
输出: true
解法一:
暴力破解
嵌套循环,计算元素值小于索引为i的元素值的个数,使用另一个数组记录
public int[] smallerNumbersThanCurrent(int[] nums) {
int len=nums.length;
int res[]=new int[len];
for(int i=0;i<len;i++) {
int count=0;
for(int j=0;j<len;j++) {
if(nums[i]>nums[j]) {
count++;//统计小于nums[i]的元素的个数
}
}
res[i]=count;//新数组记录
}
return res;
}
运行效果: