今日任务
- 哈希表理论基础
- 242.有效的字母异位词
- 349. 两个数组的交集
- 202. 快乐数
- 1. 两数之和
哈希表理论基础
解决冲突有两种方法:拉链法和线性探测法。
使用线性探测法,一定要保证tableSize大于dataSize。
LeetCode 242.有效的字母异位词
如果想到了的话挺简单的实现起来,没想到的话就要想很久的逻辑实现。区别蛮大的
bool isAnagram(char* s, char* t) {
//想不出来,原来它是使用了26个字母大小
int a[26];
for(int i=0;i<26;i++){
a[i]=0;
}
char *p=s;
char *q=t;
while(*p!='\0'){
a[*p-'a']++;
p++;
}
while(*q!='\0'){
a[*q-'a']--;
q++;
}
for(int i=0;i<26;i++){
if(a[i]!=0) return false;
}
return true;
}
LeetCode 349. 两个数组的交集
自己写的时间复杂度太高了,暴力解法。
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize) {
const int max=nums1Size>nums2Size?nums1Size:nums2Size;
//int a[max]={0};
int* a = (int*)malloc(sizeof(int) * max); // 动态分配内存
for (int i = 0; i < max; ++i) {
a[i] = 0;
}
int count=0;
for(int i=0;i<nums1Size;i++){
for(int j=0;j<nums2Size;j++){
if(nums1[i]==nums2[j]){
int m=0;
for(;m<count;m++){
if(a[m]==nums1[i]) break;
}
if(m==count) a[count++]=nums1[i];
break;
}
}
}
*returnSize=count;
return a;
}
使用哈希表记得数组大小要确定才能用,它使用了一个set,我在学习C语言的时候没见到过,不会。下面我粘贴一个代码随想录的C语言代码:
int* intersection1(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
int nums1Cnt[1000] = {0};
int lessSize = nums1Size < nums2Size ? nums1Size : nums2Size;
int * result = (int *) calloc(lessSize, sizeof(int));
int resultIndex = 0;
int* tempNums;
int i;
/* Calculate the number's counts for nums1 array */
for(i = 0; i < nums1Size; i ++) {
nums1Cnt[nums1[i]]++;
}
/* Check if the value in nums2 is existing in nums1 count array */
for(i = 0; i < nums2Size; i ++) {
if(nums1Cnt[nums2[i]] > 0) {
result[resultIndex] = nums2[i];
resultIndex ++;
/* Clear this count to avoid duplicated value */
nums1Cnt[nums2[i]] = 0;
}
}
* returnSize = resultIndex;
return result;
}
他就还是上一个思路,先填充一个满足题意的数据,再去比较检查输出最终的结果。
LeetCode 202. 快乐数
哦豁,一看题目解释就知道写不出来,循环判断的条件我好像找到了,就是不能的就是会循环回去所以它是一个不快乐的数。第一:那我就想着要不要创建数组检查,但是我不想这么做。第二:还有可以直接判断1、0两个数,因为平方和肯定是一个证书,那么就只要检查是不是只有一个1,其他都为0。这种好像会有无限循环。啊,这两种我都不想写了。。。
暴力第二错误解法:
bool isHappy(int n) {//明显不可能有四位数的出现,只有三种可能100,010,001
int curr=n;
for(int i=0;i<100;i++){
if(curr==100 || curr==10 || curr==1){
return true;
}
int hun=curr/100;
int ten=(curr/10)%10;
int num=curr%10;
curr=hun*hun+ten*ten+num*num;
}
return false;
}
我在题解中找到了一个通俗易懂的解题代码
bool isHappy(int n) {
int sum=0;
int y=100;
while(y--){
while(n){
int x=n%10;
sum+=pow(x,2);
n=n/10;
}
if(sum==1) return true;
else
{
n=sum;
sum=0;
}
}
return false;
}
1. 两数之和
这个题目两层for循环的解法很简单,我以前认为哈希表就是数组,我现在才发现哈希表就是哈希表,有他自己的数据定义,使用方法。
不会!看了题解,了解了一点,基本能看懂,官方题解如下:
struct hashTable {
int key;
int val;
UT_hash_handle hh;
};
struct hashTable* hashtable;
struct hashTable* find(int ikey) {
struct hashTable* tmp;
HASH_FIND_INT(hashtable, &ikey, tmp);
return tmp;
}
void insert(int ikey, int ival) {
struct hashTable* it = find(ikey);
if (it == NULL) {
struct hashTable* tmp = malloc(sizeof(struct hashTable));
tmp->key = ikey, tmp->val = ival;
HASH_ADD_INT(hashtable, key, tmp);
} else {
it->val = ival;
}
}
int* twoSum(int* nums, int numsSize, int target, int* returnSize) {
hashtable = NULL;
for (int i = 0; i < numsSize; i++) {
struct hashTable* it = find(target - nums[i]);
if (it != NULL) {
int* ret = malloc(sizeof(int) * 2);
ret[0] = it->val, ret[1] = i;
*returnSize = 2;
return ret;
}
insert(nums[i], i);
}
*returnSize = 0;
return NULL;
}
作者:力扣官方题解
链接:https://leetcode.cn/problems/two-sum/solutions/434597/liang-shu-zhi-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。