Contains Duplicate
Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.
Example 1:
Input: [1,2,3,1]
Output: true
Example 2:
Input: [1,2,3,4]
Output: false
Example 3:
Input: [1,1,1,3,3,4,3,2,4,2]
Output: true
思路:把一列数的大小想象成山坡的海拔,如果两个数相等(同海拔高度),那么这个数列向上走(正值)和向下走(负值)是相互抵消的。
计算这个数组中每个元素和前一个元素的差值,如果全为正值或全为负值则返回false,如果有差值列表中累计相加之和为零,那么证明这里有两个数是相等的。
但我感觉这个思路有点点复杂。。
好吧果然。。
C语言
int comp (const void * a, const void * b) {
return ( *(int*)a - *(int*)b );
}
bool containsDuplicate(int* nums, int numsSize) {
// Sort
qsort(nums, numsSize, sizeof(int), comp);
// Loop
for (int i = 0; i < numsSize - 1; i++) {
if (nums[i] == nums[i+1]) return true;
}
return false;
}
python3
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
l = len(nums)
if l < 2:
return False
nums.sort()
for i in range(l-1):
if nums[i] == nums[i+1]:
return True
return False
Success
Details
Runtime: 48 ms, faster than 64.14% of Python3 online submissions for Contains Duplicate.
Memory Usage: 16.3 MB, less than 82.49% of Python3 online submissions for Contains Duplicate.
Contains Duplicate II
Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.
Example 1:
Input: nums = [1,2,3,1], k = 3
Output: true
Example 2:
Input: nums = [1,0,1,1], k = 1
Output: true
Example 3:
Input: nums = [1,2,3,1,2,3], k = 2
Output: false
C语言
#define abs(a,b) (a > b ? a - b : b - a)
bool containsNearbyDuplicate(int* nums, int numsSize, int k) {
int* order = malloc(sizeof(int)*numsSize);
int flag = 1;
for (int i = 0; i < numsSize; i++){
order[i] = i;
}
while (flag){
flag = 0;
for(int i = 0; i < numsSize-1; i++){
if (nums[i] > nums[i+1]){
nums[i] ^= nums[i+1];
nums[i+1] ^= nums[i];
nums[i] ^= nums[i+1];
order[i] ^= order[i+1];
order[i+1] ^= order[i];
order[i] ^= order[i+1];
flag = 1;
}
else if(nums[i] == nums[i+1] && abs(order[i], order[i+1]) <= k){
return true;
}
}
}
return false;
}
Success
Details
Runtime: 8 ms, faster than 90.48% of C online submissions for Contains Duplicate II.
Memory Usage: 9.3 MB, less than 78.79% of C online submissions for Contains Duplicate II.
python3
class Solution:
def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
h = {}
for i, num in enumerate(nums):
if num in h and i - h[num] <= k:
return True
h[num] = i
return False
既然内存大小不变,这个思路是将元素与位置互换,如果有数存在h里面了,且两个的坐标小于k,则返回true。
Success
Details
Runtime: 48 ms, faster than 83.24% of Python3 online submissions for Contains Duplicate II.
Memory Usage: 20.3 MB, less than 13.64% of Python3 online submissions for Contains Duplicate II.
Tips:
关于python3中枚举的另一个方法
如果要统计文件的行数,可以这样写:
count = len(open(filepath, 'r').readlines())
这种方法简单,但是可能比较慢,当文件比较大时甚至不能工作。
可以利用enumerate():
count = 0
for index, line in enumerate(open(filepath,'r')):
count += 1