• Leetcode 205 同构字符串


      依然是简单题,熟悉 uthash 的使用。终于想通了,为何封装 HASH_ADD 等相关方法时,需要传入指针的指针,因为 uthash 库的实现都是写在宏中的,编译后不是函数调用,而是代码替换!

      C:

    #include "stdbool.h"
    #include <string.h>
    #include "./lib/uthash-master/include/uthash.h"
    
    typedef struct Map
    {
        char k;
        char v;
        UT_hash_handle hh;
    } Map;
    
    bool mapContains(char key, Map **map)
    {
        Map *reMap = NULL;
        HASH_FIND(hh, *map, &key, sizeof(char), reMap);
        if (reMap == NULL)
            return false;
        return true;
    }
    
    int mapAdd(char key, char value, Map **map)
    {
        Map *m = (Map *)malloc(sizeof(Map));
        m->k = key;
        m->v = value;
        if (mapContains(key, map))
            return false;
        HASH_ADD(hh, *map, k, sizeof(char), m);
        return true;
    }
    
    char mapGet(char key, Map **map)
    {
        Map *tmp = NULL;
        HASH_FIND(hh, *map, &key, sizeof(char), tmp);
        return tmp->v;
    }
    
    void MapClear(Map **map)
    {
        struct Map *current, *tmp;
        HASH_ITER(hh, *map, current, tmp)
        {
            HASH_DEL(*map, current);
            free(current);
        }
    }
    
    bool isIsomorphic(char *s, char *t)
    {
        int len = strlen(s);
        Map *dict0 = NULL, *dict1 = NULL;
        bool re = true;
        for (int i = 0; i < len; i++)
        {
            char char0 = s[i], char1 = t[i];
            if ((mapContains(char0, &dict0) && mapGet(char0, &dict0) != char1) || (mapContains(char1, &dict1) && mapGet(char1, &dict1) != char0))
            {
                re = false;
                break;
            }
            mapAdd(char0, char1, &dict0);
            mapAdd(char1, char0, &dict1);
        }
        MapClear(&dict0);
        MapClear(&dict1);
        return re;
    }

      JAVA:

    public final boolean isIsomorphic(String s, String t) {
            Map<Character, Character> dict0 = new HashMap<Character, Character>();
            Map<Character, Character> dict1 = new HashMap<Character, Character>();
            for (int i = 0; i < s.length(); i++) {
                char char0 = s.charAt(i), char1 = t.charAt(i);
                if (dict0.keySet().contains(char0) && dict0.get(char0) != char1) return false;
                if (dict1.keySet().contains(char1) && dict1.get(char1) != char0) return false;
                dict0.put(char0, char1);
                dict1.put(char1, char0);
            }
            return true;
        }

      JS:

    /**
     * @param {string} s
     * @param {string} t
     * @return {boolean}
     */
    var isIsomorphic = function (s, t) {
        let dict0 = new Map(), dict1 = new Map();
        for (let i = 0; i < s.length; i++) {
            let char0 = s.charAt(i), char1 = t.charAt(i);
            if (dict0.get(char0) && dict0.get(char0) != char1) return false;
            if (dict1.get(char1) && dict1.get(char1) != char0) return false;
            dict0.set(char0, char1);
            dict1.set(char1, char0);
        }
        return true;
    };

  • 相关阅读:
    urllib2使用总结
    Scrapy简介
    python3使用多代理访问网站
    ISO9000 质量管理和质量保证系列国际标准
    怎样花两年时间去面试一个人
    Robot Framework 快速入门_中文版
    PMP项目经理认证
    Scrapy安装介绍
    批处理写的俄罗斯方块
    TL9000 电信业质量体系管理标准
  • 原文地址:https://www.cnblogs.com/niuyourou/p/15980666.html
Copyright © 2020-2023  润新知