代码随想录day06|242.有效的字母异位词 349. 两个数组的交集、202. 快乐数 、 1. 两数之和

题目链接:242.有效的字母异位词 





bool isAnagram(char* s, char* t) {
    int record[26] = {0};
    for(int i = 0; i < strlen(s);i++)
    {
        record[s[i] - 'a']++;
    }
    for(int i = 0; i < strlen(t); i++){
        record[t[i] - 'a']--;
    }
    for(int i = 0; i < 26; i++){
        if(record[i] != 0)
            return false;
    }
    return true;
}

题目链接:349. 两个数组的交集

int* intersection(int* nums1, int nums1Size, int* nums2, int nums2Size, int* returnSize){
    int hash1[1001] = {0};
    int hash2[1001] = {0};
    for(int i = 0; i < nums1Size; i++){
        hash1[nums1[i]] = 1;
    }
    for(int i = 0; i < nums2Size; i++){
        hash2[nums2[i]] = 1;
    }
    int *ret = (int*)malloc(sizeof(int)*1001);
    int k = 0;
    for(int i = 0; i < 1001; i++){
        if(hash1[i] == 1 && hash2[i] == 1){
            ret[k++] = i;
        }
    }
    (*returnSize) = k;
    return ret;
}

int count(int n){
    int res = 0;
    int a=0;
    while(n){
        a = n%10;
        n = n/10;
        res += a*a;
    }
    return res;
}


bool isHappy(int n){
    int fast = n,slow = n;
    do{
        slow = count(slow);
        fast = count(fast);
        fast = count(fast);
    }while(slow != fast);
    return (slow == 1);
}

题目链接:202. 快乐数

   

题目链接:1. 两数之和 

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;
}

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值