LeetCode 解题笔记(三)字符串


总目录:      LeetCode 解题笔记(一)总


一、基础篇

344. 反转字符串(2022/03/06)

标签:字符串

题目:
在这里插入图片描述

● 我的答案:

这应该是做过的最简单的一道题目

class Solution {
    
    
public:
    void reverseString(vector<char>& s) {
    
    
        char tmp;
        int n = s.size() - 1;
        for(int i=0; i<n; i++,n--) {
    
    
            tmp = s[n];
            s[n] = s[i];
            s[i] = tmp;
        }
    }
};

● 官方答案:使用 swap 函数:

class Solution {
    
    
public:
    void reverseString(vector<char>& s) {
    
    
        int n = s.size();
        for (int left = 0, right = n - 1; left < right; ++left, --right) {
    
    
            swap(s[left], s[right]);
        }
    }
};

7. 整数反转(2022/03/07)

链接: 7. 整数反转

题目:
在这里插入图片描述
标签: 字符串,数学

● 我的答案:

class Solution {
    
    
public:
    int reverse(int x) {
    
    
        int ret = 0;
        while(x) {
    
    
        	//以下次判断条件为,看答案后可知
            if (ret < INT_MIN / 10 || ret > INT_MAX / 10) {
    
    
                return 0;
            }
            ret = ret*10 + x%10;
            x = x/10; 
        }
        return ret;
    }
};

INT_MIN 在标准头文件 limits.h 中定义:

#define INT_MAX 2147483647
#define INT_MIN (-INT_MAX - 1)

387. 字符串中的第一个唯一字符(2022/03/08)

● 题目:
在这里插入图片描述
● 标签: 队列、哈希表、字符串

● 我的解法:

暴力解法,都失败了… 哈希方法:

class Solution {
    
    
public:
    int firstUniqChar(string s) {
    
    
        //哈希第一次记录频率, 26个字母而已
        int n = s.size();
        unordered_map<int,int> _map;    //无序的,有键值的用 unordered_map
        for(int i=0; i<n; i++) {
    
    
            _map[s[i]] ++;
        }
        /*
		for (char ch: s) {    //可以直接常引用了
          ++_map[ch];
    	}
		*/

        //第二次取最近的字符
        for(int i=0; i<n; i++) {
    
    
            if(_map[s[i]] == 1) return i; 
        }

        return -1;
    }
};

242. 有效的字母异位词(2022/03/09)

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

**● 标签:**字符串、哈希表、数组

● 题目:
在这里插入图片描述

● 我的答案:
哈希计数,然后对比,相减

class Solution {
    
    
public:
    bool isAnagram(string s, string t) {
    
    
        unordered_map<int,int> _map;
        for(auto const & num: s) {
    
    
            ++_map[num];
        }

        for(auto const & num: t) {
    
    
            if(_map.count(num)) {
    
    
                --_map[num];
                if(_map[num] == 0) {
    
    
                    _map.erase(num);
                }
            }
            else {
    
    
                return false;
            }
        }

        if(_map.size()!=0)  return false;
        else                return true;
    }
};

● 官方答案1: 判断长度,快速排序判断内容

class Solution {
    
    
public:
    bool isAnagram(string s, string t) {
    
    
        if (s.length() != t.length()) {
    
    
            return false;
        }
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        return s == t;
    }
};

● 官方答案2: 哈希表 + 数组,与我答案思路类似!

class Solution {
    
    
public:
    bool isAnagram(string s, string t) {
    
    
        if (s.length() != t.length()) {
    
    
            return false;
        }
        vector<int> table(26, 0);
        for (auto& ch: s) {
    
    
            table[ch - 'a']++;
        }
        for (auto& ch: t) {
    
    
            table[ch - 'a']--;
            if (table[ch - 'a'] < 0) {
    
    
                return false;
            }
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_16504163/article/details/123166876