第三章 哈希表* part01*

56 阅读2分钟

第三章 哈希表* part01*

242.有效的字母异位词

题目链接 : leetcode.cn/problems/va…

需要 注意 的 点

字符串 结束 标志 的 判断

字母 ASCII 到 数字 的 映射 / 简化 映射

similar 的 操作 , 在 简化 时 , 如果 只需要 判断 , 可 采取 互补 向消 的 方式 进行 验证 , 以 降低 运算 资源 的 消耗

Code :

class Solution {
public:
    bool isAnagram(string s, string t) {
        int * times_Letter_Array_s = new int[26];
​
        int * times_Letter_Array_t = new int[26];
​
        int i = 0;
​
        for(; i<26; i++)
        {
            times_Letter_Array_s[i] = 0;
​
        }
​
​
        for( i = 0 ; i<26; i++)
        {
            times_Letter_Array_t[i] = 0;
            
        }
​
        for( i = 0 ; s[i] != '\0' ;i++ )
        {
            times_Letter_Array_s[s[i] - 'a'] ++ ; 
​
        }
​
        for( i = 0 ; t[i] != '\0' ;i++ )
        {
            times_Letter_Array_t[t[i] - 'a'] ++ ; 
​
        }
​
​
        for( i = 0 ; i < 26 ; i++)
        {
            if(times_Letter_Array_s[i] != times_Letter_Array_t[i] )
            {
                return 0 ;
​
            }
​
​
​
        }
​
        return 1;
​
​
​
    }
};

349.两个数组的交集

题目连接 : leetcode.cn/problems/in…

当 数组 中 实际 可用 的 信息 比较 稀疏 时

可以 建立 Set 缩小 范围 , 使 数据 的 存储 / 使用 效率 增加

Code :

class Solution {
public:
    vector<int> intersection(vector<int>& nums1, vector<int>& nums2) {
​
        unordered_set<int> frequence_num1 ;   // 注意 不要 忘了 定义 Set 里 元素 的 类型 
        unordered_set<int> frequence_num2 ;
​
        int i = 0;
​
        int len_nums1 = nums1.size();
        int len_nums2 = nums2.size();
​
        vector<int> vec_receive ;
​
        for( ; i < len_nums1; i++ )
        {
            frequence_num1.insert(nums1[i]);
​
        }
​
                // 查询 的 范围 缩小 了 , 同时 还可以 基于 散列表 进行 查询
​
        i = 0;
        for( ;i < len_nums2 ; i++)
        {                   //“ 必要 的 遍历 ” 
            frequence_num2.insert(nums2[i]);
​
        }
​
        /*
        i = 0;
        int len_frequence_num2 = frequence_num2.size();
        for( ;i < len_frequence_num2 ; i++)
        {                   //“ 必要 的 遍历 ” 
            if(frequence_num1.find(len_frequence_num2[i])!=frequence_num1.end() )
            {
                vec_receive.push_back(len_frequence_num2[i]);
            }
​
        }
        */
​
        for(auto iterator = frequence_num2.begin() , end = frequence_num2.end(); iterator != end ; iterator ++)
        {                   //“ 必要 的 遍历 ” 
            if(frequence_num1.find(*iterator)!=frequence_num1.end() )
            {
                vec_receive.push_back(*iterator);
            }
​
        }
​
        return vec_receive ;
​
​
    }
};

202.快乐数

题目 地址 : leetcode.cn/problems/ha…

注意 状态变化 中 可能 形成 的 闭环

Code :

class Solution {
public:
    bool isHappy(int n) {
​
        unordered_set<int> record_num_Process ;
                                            // 次序 重复    与   中间 状态(这里 是 数)  重复
        if(n == 0)
        {
            return 0 ;
        }
​
        int temp_Square_Sum = 0;        
​
                                // 将 n 输(输入) 进去
​
        temp_Square_Sum = n ;
​
        int digit_Bit ;
​
                            // 重复 闭环 的 检测  
​
                                    //  这里 数(中间状态) 一样 (再次 变换 到 xx 数) (这里 是 从 某一 点(相对) 进行 观测)  便 会 形成 闭环
​
        do
        {
            // 内 循环 
​
            int Cache_Bits_num = temp_Square_Sum ;
​
            temp_Square_Sum = 0;
​
            while(Cache_Bits_num != 0)
            {
                digit_Bit = Cache_Bits_num % 10;
                temp_Square_Sum += ((digit_Bit) * (digit_Bit)) ;
                Cache_Bits_num /= 10;
            }
​
            if(record_num_Process.find(temp_Square_Sum) == record_num_Process.end())
            {
                record_num_Process.insert(temp_Square_Sum);
            }
            else
            {
                return 0;
            }
​
​
        }while(temp_Square_Sum != 1);
​
        return 1;
​
        
​
​
​
                                                                    
​
    }
};

1.两数之和

题目 链接 : leetcode.cn/problems/tw…

//注意 时间 尽量 不要 熬夜

class Solution {
public:
    struct TwoNum
    {
        int num1;
        int num2;
​
    };
​
    vector<int> twoSum(vector<int>& nums, int target) {
        
        unordered_map< int , int >  map_ItemValue_And_Index ;
​
        int i = 0;
​
        int j = 0;
​
        int len = nums.size();
​
        vector<int> result ; 
​
        if(len == 1)
        {
            cout<< "输入 了 只 含有 一个 元素 的 数组"<<endl;
            //return  ;
        }
​
        int sum_Temp = 0;
​
        TwoNum cache_TwoNum;
​
        for( ; i < len ; i++ )
        {
            map_ItemValue_And_Index.emplace(nums[i],i);
​
        }
​
        for(i = 0 ; i < len ; i++ )
        {
          auto iterator = map_ItemValue_And_Index.find(target - nums[i]);
            if( iterator != map_ItemValue_And_Index.end())
            {
              if(iterator->second != i)         //  注意 排除 该 数 (寻找 搭配 /搭档 ) 和 自己 的 组合
              {
                result.push_back(i);
​
                result.push_back(iterator->second);
​
                break;                        // 这里 是 有 一种 情况 直接 结束 循环 , 注意 不要 跟 对称 的 情况 / 其它 情况  混 了
​
              }
              
            }
​
        }
        
​
    
        return result ;
​
​
        
    }
};