LeetCode经典基础算法题1

本文主要介绍了LeetCode中的一些经典基础算法题目,包括解题思路、常用技巧和代码实现,旨在帮助初学者提升算法能力。
摘要由CSDN通过智能技术生成

在这里插入图片描述

class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        unordered_map<int, int>hash;
        for(int i = 0; i < nums.size();  i++) {
            int t = target - nums[i];
            if(hash.count(t)) return {i, hash[t]};
            hash[nums[i]] = i;
        }
        return {};
    }
};

在这里插入图片描述

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummy = new ListNode(-1);
        ListNode* pre = dummy;
        int carry = 0;
        while(l1 || l2) {
            int res = carry;
            int a = l1 ? l1->val : 0;
            int b = l2 ? l2->val : 0;
            res += a + b;
            carry = res / 10;
            int t = res % 10;
            pre->next = new ListNode(t);
            pre = pre->next;
            if(l1) l1 = l1->next;
            if(l2) l2 = l2->next;
        }
        if(carry) {
            pre->next = new ListNode(1);            
        }
        return dummy->next;
    }
};

在这里插入图片描述

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        unordered_map<char, int>hash;
        int res = 0;
        int i, j;
        for(i = 0, j = 0; j < s.size(); j ++) {
            if(++ hash[s[j]] > 1) {
                while(i < j) {
                    hash[s[i]] --;
                    i ++;
                    if(hash[s[j]] == 1) break;
                }
            }
            res = max(res, j - i + 1);
        }        
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    string longestPalindrome(string s) {
        int len = 0;
        string res = "";
        for(int i = 0; i < s.size(); i ++) {
            //偶数
            int l = i, r = i + 1;
            while(l >= 0 && r < s.size() && s[l] == s[r]) l --, r ++;
            if(r - l - 1 > len) {
                len = r - l - 1;
                res = s.substr(l + 1, len);
            }
            //奇数
            l = i - 1, r = i + 1;
            while(l >= 0 && r < s.size() && s[l] == s[r]) l --, r ++;
            if(r - l - 1 > len) {
                len = r - l - 1;
                res = s.substr(l + 1, len);
            }
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows == 1) return s;

        vector<string> row(min((int)s.size(), numRows));
        int curRow = 0;
        bool down = false;

        for(auto c : s) {
            row[curRow] += c;
            if(curRow == 0 || curRow == numRows - 1) down = !down;
            curRow += down ? 1 : -1;
        }
        string res;
        for(auto c : row) {
            res += c;
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int reverse(int x) {
        int res = 0;
        int pop = 0;
        while(x) {
            pop = (x % 10);
            x /= 10;
            if(res > INT_MAX / 10) return 0;
            if(res < INT_MIN / 10) return 0;
            res = res * 10 + pop;
        }
        return res;
    }
};

在这里插入图片描述

class Solution {
public:
    int myAtoi(string str) {
        int i = 0;
        long long res = 0;
        int flag = 1;
        while(i < str.size() && str[i] == ' ') i ++;
        if(str[i] == '-') flag = -1, i ++;
        else if(str[i] == '+') flag = 1, i ++;
        while(i < str.size() && str[i] >= '0' && str[i] <= '9') {
            res = res * 10 + str[i ++] - '0';
            if(res >= 2147483648) {
                if(flag == 1) return INT_MAX;
                else return INT_MIN;
            }
        }
        return res * flag;
    }
};

在这里插入图片描述

class Solution {
public:
    bool isPalindrome(int x) {
        int xx = x;
        long long p = 0;
        if(x < 0 || (x % 10 == 0 && x != 0)) return false;
        while(x) {
            p = p * 10 + x % 10;
            x /= 10;
        }
        return p == xx;
    }
};

在这里插入图片描述

class Solution {
public:
    vector<vector<int>>f;
    int n, m;
    bool isMatch(string s, string p) {
        n = s.size();
        m = p.size();
        f = vector<vector<int>>(n + 1, vector<int>(m + 1, -1));
        return dp(0, 0, s, p);
    }

    bool dp(int x, int y, string s, string p) {
        if(f[x][y] != - 1) return f[x][y];
        if(y == m) return f[x][y] = x == n;
        bool first_match = x < n && (s[x] == p[y] || p[y] == '.');
        bool res;
        if(y + 1 < m && p[y + 1] == '*') {
            res = dp(x, y + 2, s, p) || first_match && dp(x + 1, y, s, p);
        } else {
            res = first_match && dp(x + 1, y + 1, s, p);
        }
        return f[x][y] = res;
    }
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值