Leetcode Brushing Notes (C++) - Hash Table

Leetcode Brushing Notes (C++) - Hash Table

Sort out the ideas in the process of brushing the questions, and summarize and share them here.
github address: https://github.com/lvjian0706/Leetcode-solutions
The github project is just newly created, and the organized code and ideas will be uploaded one after another. The code is based on C++ and python. At the same time, the basic sorting algorithm will also be sorted and uploaded.

1. The sum of two numbers

Given an integer array nums and a target value target, please find the two integers whose sum is the target value in the array, and return their array subscripts.

You can assume that there is only one answer for each input. However, the same element in the array cannot be used twice.

Example:
given nums = [2, 7, 11, 15], target = 9
because nums[0] + nums[1] = 2 + 7 = 9
so return [0, 1]

class Solution {
    
    
public:
    /*
    在该数组中找出和为目标值的那两个整数,并返回他们的数组下标:使用哈希表存放已遍历元素即可;
    1. 定义哈希表用于存放已遍历元素,其中键为该元素,值为元素索引;
    2. 遍历数组,当target-nums[i]在哈希表中不存在时,将该元素存入哈希表中;
    3. 当target-nums[i]存在时,则说明target-nums[i]对应的值(该元素索引)以及当前元素的位置i即为答案;
    */
    vector<int> twoSum(vector<int>& nums, int target) {
    
    
        map<int, int> nums_map;
        vector<int> ans;
        for(int i=0; i<nums.size(); i++){
    
    
            map<int, int>::iterator iter = nums_map.find(target-nums[i]);
            if(iter == nums_map.end()){
    
    
                nums_map[nums[i]] = i;
            }
            else{
    
    
                ans.push_back(iter->second);
                ans.push_back(i);
                break;
            }
        }
        return ans;
    }
};

217. Duplicate elements exist

Given an array of integers, check whether there are duplicate elements.

The function returns true if any value appears in the array at least twice. Returns false if every element in the array is different.

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

class Solution {
    
    
public:
    /*
    给定一个整数数组,判断是否存在重复元素。
    方法1: 排序后判断前后元素是否相同,略;
    方法2: 使用Set或者哈希表保存不同元素,在保存过程中,判断当前元素是否已经在Set或者哈希表中出现过,如果有,则存在重复元素;
    */
    bool containsDuplicate(vector<int>& nums) {
    
    
        set<int> no_duplicate;
        for(int i=0; i<nums.size(); i++){
    
    
            if(no_duplicate.find(nums[i]) == no_duplicate.end()){
    
    
                no_duplicate.insert(nums[i]);
            }
            else return true;
        }
        return false;
    }
};

242. Valid anagrams

Given two strings s and t, write a function to determine whether t is an anagram of s.

Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Explanation:
You can assume that the string contains only lowercase letters.

class Solution {
    
    
public:
    /*
    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。
    使用哈希表判断两个字符串中字母出现次数是否一致即可;
    1. 新建map存放s中字母出现次数;
    2. 遍历t,当t中字母在哈希表中不存在或者个数为0时,返回false;
    */
    bool isAnagram(string s, string t) {
    
    
        if(s.length() != t.length()) return false;
        map<char, int> s_map;
        for(int i=0; i<s.length(); i++){
    
    
            if(s_map.find(s[i]) == s_map.end()){
    
    
                s_map[s[i]] = 1;
            }
            else s_map[s[i]]++;
        }
        for(int j=0; j<t.length(); j++){
    
    
            if(s_map.find(t[j]) == s_map.end() || s_map[t[j]]==0) return false;
            else s_map[t[j]]--;
        }
        return true;
    }
};

387. First unique character in a string

Given a string, find its first unique character and return its index. Returns -1 if not present.

Example:
s = "leetcode"
returns 0
s = "loveleetcode"
returns 2
Hint: You can assume that the string contains only lowercase letters.

class Solution {
    
    
public:
    /*
    找到字符串的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
    使用哈希表遍历两遍字符串即可;
    第一遍遍历保存字符出现次数,第二次遍历找第一个出现次数为1的字符;
    */
    int firstUniqChar(string s) {
    
    
        map<char, int> num_char;
        for(int i=0; i<s.length(); i++){
    
    
            if(num_char.find(s[i]) == num_char.end()){
    
    
                num_char[s[i]] = 1;
            }
            else num_char[s[i]]++;
        }
        for(int i=0; i<s.length(); i++){
    
    
            if(num_char[s[i]]==1) return i;
        }
        return -1;
    }
};

Guess you like

Origin blog.csdn.net/weixin_43273742/article/details/107741167