[刷力扣] 1-10题

1. 两数之和

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

2. 两数相加

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        int tis = 0;
        ListNode *head = new ListNode();
        auto p = head;
        auto pre = head;
        for (ListNode *i = l1, *j = l2; i || j;) {
            if (i) tis += i -> val, i = i -> next;
            if (j) tis += j -> val, j = j -> next;
            p->val = tis % 10;
            tis /= 10;
            if (i || j) {
                p->next = new ListNode();
                p = p->next;
            }
        }
        if (tis) {
            p->next = new ListNode();
            p->next->val = tis;
        }
        return head;
    }
};

3. 无重复字符的最长子串

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int ch[128] = {0};
        int l = 0, ans = 0;
        for (int i = 0; i < s.size(); ++ i) {
            while (ch[s[i]]) ch[s[l ++]] --;
            ch[s[i]] ++;
            ans = max(ans, i - l + 1);
        }
        return ans;
    }
};

4. 寻找两个正序数组的中位数

class Solution {
public:
    double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
        int idx = nums1.size() + nums2.size();
        if (idx % 2 == 0) 
            return (get(nums1, nums2, idx / 2) + get(nums1, nums2, idx / 2 + 1)) / 2.0;
        else 
            return get(nums1, nums2, (idx + 1) / 2);
    }
    int get(vector<int> &a, vector<int> &b, int k) {
        int m = a.size(), n = b.size();
        int idx1{}, idx2{};
        while (true) {
            if (idx1 == m) return b[idx2 + k - 1];
            if (idx2 == n) return a[idx1 + k - 1];
            if (k == 1) return min (a[idx1], b[idx2]);
            int nidx1 = min (idx1 + k / 2 - 1, m - 1);
            int nidx2 = min (idx2 + k / 2 - 1, n - 1);
            int aa = a[nidx1];
            int bb = b[nidx2];
            if (aa <= bb) {
                k -= nidx1 - idx1 + 1;
                idx1 = nidx1 + 1;
            } else {
                k -= nidx2 - idx2 + 1;
                idx2 = nidx2 + 1;
            }
        }
    }
};

5. 最长回文子串

class Solution {
public:
    string longestPalindrome(string s) {
        int st = 0, len = 1;
        vector<vector<int>> f(s.size() + 2, vector<int>(s.size() + 2));
        for (int i = 1; i <= s.size(); ++ i) f[i][i] = 1;
        for (int i = 1; i < s.size(); ++ i) {
            if (s[i] == s[i - 1]) {
                f[i][i + 1] = 1;
                if (len < 2) {
                    st = i - 1;
                    len = 2;
                }
            }
        }
        for (int l = 3; l <= s.size(); ++ l) 
        for (int i = 1; i + l - 1 <= s.size(); ++ i) {
            int j = i + l - 1;
            if (s[i - 1] == s[j - 1]) {
                f[i][j] = f[i + 1][j - 1];
                if (f[i][j] && l > len) {
                    st = i - 1;
                    len = l;
                }
            }
        }
        return s.substr(st, len);
    }
};

6. Z 字形变换

class Solution {
public:
    string convert(string s, int numRows) {
        if (numRows == 1) return s;
        vector<string> ve(numRows);
        vector<int> trans(numRows * 2 - 2);
        for (int i = 0; i < numRows; ++ i) 
            trans[i] = i;
        for (int i = numRows, j = 1; i < numRows * 2 - 2; ++ i, ++ j)
            trans[i] = numRows - j - 1;
        int st = 0;
        for (const auto& c : s) {
            ve[trans[st]].push_back(c);
            st = (st + 1) % trans.size();
        }
        string tm;
        for (auto& s : ve) 
            tm += std::move(s);
        return tm;
    }
};

7. 整数反转

class Solution {
public:
    int reverse(int x) {
        if (x > INT_MAX || x <= INT_MIN) return 0;
        unsigned int t = x;
        int f = x >= 0 ? 1 : -1;
        x = x < 0 ? -x : x;
        unsigned int tm = 0;
        while (x) {
            tm += x % 10;
            x /= 10;
            if (x && INT_MAX / 10 + 1 < tm) return 0;
            if (x) tm *= 10;
        }
        if ((f == 1 && tm >= INT_MAX) || (f == -1 && tm > INT_MAX)) return 0;
        else return f * tm;
    }
};

8. 字符串转换整数 (atoi)

class Solution {
public:
    int myAtoi(string s) {
        long long base = 0;
        int f = 1, flag = 1, cnt = 0;
        for (const auto& c : s) {
            if (c == ' ' && flag) continue;
            flag = 0;
            if (c == '-' || c == '+' || (c >= '0' && c <= '9')) {
                if (c == '-') {
                    if (!cnt) f = -1, cnt ++;
                    else break;
                }
                if (c == '+') {
                    if (!cnt) f = 1, cnt ++;
                    else break;
                }
                if (c >= '0' && c <= '9') 
                    base += c - '0', base *= 10, cnt ++;
                if (base / 10 > INT_MAX) {
                    if (f == 1) return INT_MAX;
                    else return INT_MIN;
                } 
            } else break;
        }
        base /= 10;
        if (base * f < INT_MIN) base = INT_MIN;
        else if (base * f > INT_MAX) base = INT_MAX;
        else base = base * f;
        return base;
    }
};

9. 回文数

class Solution {
public:
    bool isPalindrome(int x) {
        if (x < 0) return false;
        int g[12];
        int idx = 0;
        while (x) {
            g[++ idx] = x % 10;
            x /= 10;
        }
        for (int l = 1, r = idx; l < r; ++ l, -- r)
            if (g[l] != g[r]) return false;
        return true;
    }
};

10. 正则表达式匹配

class Solution {
public:
    bool isMatch(string s, string p) {
        vector<vector<int>> f(s.size() + 1, vector<int>(p.size() + 1));
        f[0][0] = 1;
        for (int i = 0; i <= s.size(); ++ i)
        for (int j = 1; j <= p.size(); ++ j) {
            if (i && p[j - 1] == '.') f[i][j] = f[i - 1][j - 1];
            else if (p[j - 1] == '*'){
                f[i][j] = f[i][j - 2];
                if (i && (s[i - 1] == p[j - 2] || p[j - 2] == '.')) 
                    f[i][j] |= f[i - 1][j];
            } else {
                if (i && s[i - 1] == p[j - 1]) 
                    f[i][j] = f[i - 1][j - 1];
            }
        }
        return f[s.size()][p.size()];
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值