【LeetCode】1000题挑战(230/1000)

1000题挑战

没有废话,直接开刷!

目录

1000题挑战

没有废话,直接开刷!

第一题:242. 有效的字母异位词 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第二题:257. 二叉树的所有路径 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第三题:258. 各位相加 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第四题:263. 丑数 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第五题:290. 单词规律 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

题量截图:

写在最后:


第一题:242. 有效的字母异位词 - 力扣(Leetcode)

题目接口:

class Solution {
public:
    bool isAnagram(string s, string t) {

    }
};

解题思路:

这道题有两种思路:

1. 我一开始想到就是哈希

2. 直接排序开摆

代码:

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

过过过过啦!!!!

第二题:257. 二叉树的所有路径 - 力扣(Leetcode)

题目接口:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {

    }
}; 

解题思路:

这道题的可以用dfs也可以用bfs。

我用的是dfs,

具体思路是:

用深搜搜索每一条路径,

如果走到了叶子节点,就把这一条路径记录下来,

搜索完整棵树,所有路径就都出来了。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    vector<string> binaryTreePaths(TreeNode* root) {
        vector<string> path;
        _tree_path(root, "", path); //dfs搜索
        return path;
    }
private:
    void _tree_path(TreeNode* root, string paths, vector<string>& path) {
        if(root != nullptr) {
            paths += to_string(root->val);
            if(!root->left && !root->right) { //到叶子结点了,就插入路径
                path.push_back(paths);
            }
            else {
                paths += "->";
                _tree_path(root->left, paths, path); //搜索左子树
                _tree_path(root->right, paths, path); //搜索右子树
            }
        }
    }
}; 

过过过过啦!!!!

第三题:258. 各位相加 - 力扣(Leetcode)

题目接口:

class Solution {
public:
    int addDigits(int num) {

    }
}; 

解题思路:

这道题就是一个简单模拟,

不过题目提出了一个挑战,

就是让我们用O(1)的方法解决,

这个其实需要推导出数学公式。

我就只是简单模拟了,下面是代码:

代码:

class Solution {
public:
    int addDigits(int num) {
        int sum = num;
        while(sum > 9) {
            sum = 0;
            while(num) {
                sum += num % 10;
                num /= 10;
            }
            num = sum;
        }
        return sum;
    }
}; 

过过过过啦!!!!

第四题:263. 丑数 - 力扣(Leetcode)

题目接口:

class Solution {
public:
    bool isUgly(int n) {

    }
};

解题思路:

这道题需要用到数学方法来做,

作为数学学渣,我就只能用暴力求解了:

代码:

class Solution {
public:
    bool isUgly(int n) {
        if(n == 0) return false;
        while(n % 2 == 0) n /= 2;
        while(n % 3 == 0) n /= 3;
        while(n % 5 == 0) n /= 5;
        return n == 1;
    }
};

过过过过啦!!!!

第五题:290. 单词规律 - 力扣(Leetcode)

题目接口:

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        
    }
};

解题思路:

这道题需要用哈希去解,

用两个哈希双射,然后遍历,

如果有值对应不上就返回false即可。

代码:

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<string, char> pas;
        unordered_map<char, string> spa;
        int n = s.size(), i = 0;
        for(auto ch : pattern) {
            if(i == n + 1) return false; //两个字符串的个数不匹配
            int j = i;  
            while(j < n && s[j] != ' ') j++; //更新右边界
            const string& tmp = s.substr(i, j - i); //截取s里面的字符串

            //如果该值存在哈希里面且不等于对应的值,则返回false
            if(pas.count(tmp) && pas[tmp] != ch) return false;
            if(spa.count(ch) && spa[ch] != tmp) return false;

            //把值存进哈希
            pas[tmp] = ch; 
            spa[ch] = tmp;
            i = j + 1; //更新左边界
        }
        return i == n + 1; //两个字符串的个数匹配
    }
};

过过过过啦!!!!

题量截图:

写在最后:

以上就是本篇文章的内容了,感谢你的阅读。

如果感到有所收获的话可以给博主点一个哦。

如果文章内容有遗漏或者错误的地方欢迎私信博主或者在评论区指出

猜你喜欢

转载自blog.csdn.net/Locky136/article/details/130265702