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

1000题挑战

没有废话,直接开刷!

目录

1000题挑战

没有废话,直接开刷!

第一题:202. 快乐数 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第二题:205. 同构字符串 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第三题:219. 存在重复元素 II - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第四题:228. 汇总区间 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

第五题:234. 回文链表 - 力扣(Leetcode)

题目接口:

解题思路:

代码:

过过过过啦!!!!

题量截图:

写在最后:


第一题:202. 快乐数 - 力扣(Leetcode)

题目接口:

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

    }
};

解题思路:

这道题的核心思路就是:

循环的结束标志,当一个数二次插入的时候证明死循环了,

当出现1就证明成功找到了,

那么怎么判断呢?

1. 可以用哈希

2. 可以用双指针

双指针的思路是一个先循环,另一个后循环,

如果后循环的那个值等于先循环的值,就证明重复了。

我用的是哈希,

代码如下:

代码:

class Solution {
public:
    bool isHappy(int n) {
        unordered_set<int> st;
        int sum = n;
        while(1) {
            sum = get_sum(sum); //计算快乐数
            if(sum == 1) return true; 

            //unordered_set 的find如果找不到就会返回end()
            //也就是说,如果find找到第二个sum证明无限循环了
            if(st.find(sum) != st.end()) return false;
            else st.insert(sum); //插入sum
        }
        return false;
    }
    int get_sum(int n) {
        int happy = 0;
        while(n) {
            happy += pow((n % 10), 2);
            n /= 10;
        }
        return happy;
    }
};

过过过过啦!!!!

第二题:205. 同构字符串 - 力扣(Leetcode)

题目接口:

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

    }
};

解题思路:

这道题我看了一下题目,

就想着用哈希去解答,

直接建两个哈希的映射关系,

然后如果一个字符出现两次,而他的映射值不同,证明他不是同构。

代码:

class Solution {
public:
    bool isIsomorphic(string s, string t) {
        unordered_map<char, char> mp1, mp2; //建两个哈希映射
        for(int i = 0; i < s.size(); i++) {
            char a = s[i], b = t[i];

            //如果这个字符出现两次,而且不是第一次映射的值,证明不是同构
            if(mp1.find(a) != mp1.end() && mp1[a] != b
            || mp2.find(b) != mp2.end() && mp2[b] != a) return false;
            mp1[a] = b;
            mp2[b] = a;
        }
        return true;
    }
};

过过过过啦!!!!

第三题:219. 存在重复元素 II - 力扣(Leetcode)

题目接口:

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {

    }
};

解题思路:

这道题我用的是滑动窗口+哈希做的,

我们维护一个k大的滑动窗口,

如果窗口在滑动的过程中, 出现了相同的值,

证明是true,如果没有就返回false。

代码:

class Solution {
public:
    bool containsNearbyDuplicate(vector<int>& nums, int k) {
        unordered_map<int, bool> mp;
        for(int i = 0; i < nums.size(); i++) {
            if(i > k) mp[nums[i - k - 1]] = false; //控制左边界
            if(mp[nums[i]]) return true; //如果出现相同的值,返回true
            mp[nums[i]] = true; //控制右边界
        }
        return false;
    }
};

过过过过啦!!!!

第四题:228. 汇总区间 - 力扣(Leetcode)

题目接口:

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {

    }
};

解题思路:

这道题,题目让我们分离出单个的数字,还有连续的有序区间,

那我们直接遍历数组,然后如果是单个的数字,我们就直接进数组,

如果是连续的区间,我们就记录区间开始的下标和结束的下标,

然后根据题目要求组合,再插入进数组即可。

代码:

class Solution {
public:
    vector<string> summaryRanges(vector<int>& nums) {
        vector<string> v;
        for(int i = 0; i < nums.size(); i++) { //遍历
            int f = 0, t = i;
            string s;
            //如果是连续的区间,记录
            while(i + 1 < nums.size() && nums[i] == nums[i + 1] - 1) {
                f = 1;
                i++;
            }
            //如果是单个数字
            s += to_string(nums[i]);
            if(f) { //根据题目要求组合
                s.clear();
                s += to_string(nums[t]);
                s += "->";
                s += to_string(nums[i]);
            }
            v.push_back(s); //插入数组
        }
        return v;
    }
};

过过过过啦!!!!

第五题:234. 回文链表 - 力扣(Leetcode)

题目接口:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {

    }
};

解题思路:

这道题是一个非常经典的链表面试题,

这道题有很多解法:

1. 把链表的数插进数组,但是这样的空间复杂度是O(N)

2. 递归

3. 我使用的方法:

用快慢指针分离这个链表,

翻转后半链表,

两个链表比较,如果相同,证明是回文链表,不相同证明不是。

代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    bool isPalindrome(ListNode* head) {
        //双指针找中
        ListNode* cur = head;
        ListNode* prev = head;
        while(cur && cur->next) {
            cur = cur->next->next;
            prev = prev->next;
        }

        //翻转链表2
        cur = prev->next;
        prev->next = nullptr;
        while(cur) {
            ListNode* tmp = prev;
            prev = cur;
            cur = cur->next;
            prev->next = tmp;
        }

        //两链表比较
        cur = head;
        ListNode* cur2 = prev;
        while(cur && cur2) {
            if(cur->val != cur2->val) return false;
            cur = cur->next;
            cur2 = cur2->next; 
        }

        return true;
    }
};

过过过过啦!!!!

题量截图:

 

写在最后:

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

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

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

猜你喜欢

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