LeetCode Hot100 自用

1. 两数之和

cpp 复制代码
class Solution {
public:
    vector<int> twoSum(vector<int>& nums, int target) {
        map<int, int> mp;
        int sz = nums.size();
        for(int i = 0; i < sz; i++) mp[target - nums[i]] = i+1;
        for(int i = 0; i < sz; i++) if(mp[nums[i]] && mp[nums[i]]-1 != i) return {mp[nums[i]]-1, i};
        return {};
    }
};

5. 最长回文子串

cpp 复制代码
class Solution {
public:
    string longestPalindrome(string s) {
    int sz = s.size();
    for(int len = sz; len >= 1; len--)
    {
    	int c = (len - 1) / 2;
        if(len % 2 != 0) 
        {
            for(; c + len / 2 < sz; c++)
            {
                int d = 1;
                for(; d <= len / 2; d++)
                {
                    if(s[c-d] != s[c+d]) break;
                }
                if(d > len / 2) return s.substr(c-(d-1), len);
            }
        }
        else
        {
            for(; c + len / 2 < sz; c++)
            {
                int t = c;
                for(; c - t <= len / 2 - 1; t--)
                {
                    if(s[t] != s[2 * c - t + 1]) break;
                }
                if(c - t > len / 2 - 1) return s.substr((t+1), len);
            }

        }
    }
    
    return s.substr(0,1);
    }
};

6. Z 字形变换

cpp 复制代码
class Solution {
public:
    string convert(string s, int numRows) {
        int r = numRows;
        string ss[1010];

        int sz = s.size();
        int rx = -1; int d = 1;
        for(int i = 0; i < sz; i++)
        {
            rx += d;
            ss[rx] += s[i];
            if(r == 1) {d = 0;}
            else if(rx == r-1) {d = -1;}
            else if(rx == 0) {d = 1;}
        }

        string ans;
        for(int i = 0; i < r; i++) ans += ss[i];
        return ans;
    }
};

7. 整数反转

(一开始没看到不许用int64的要求^^)

cpp 复制代码
using ll = long long;
class Solution {
public:
    int reverse(int x) {
        ll maxx = (1ll << 31) - 1;
        ll minn = -maxx - 1;
        
        if(x == 0) return 0;
        int sign = x < 0 ? -1 : 1;
        ll xx = x;
        xx = abs(xx);
        string s = to_string(xx);
        ::reverse(s.begin(), s.end());
        int notz = 0;
        while(s[notz] == '0') notz++;
        s = s.substr(notz);
        if(sign < 0) s = "-" + s;
        xx = stoll(s);
        if(xx > maxx || xx < minn) xx = 0;
        return (int)xx;
    }
};

(正解√)

cpp 复制代码
class Solution {
public:
    int reverse(int x) {

        if(x == 0) return 0;

        uint mx = ((uint)1 << 31) - 1;
        uint mn = 1 << 31;
        string sx = to_string(mx);
        string sn = to_string(mn);

        string s = to_string(x);
        ::reverse(s.begin(), s.end());
        int notz = 0;
        while(s[notz] == '0') notz++;
        s = s.substr(notz);

        int ans = 0;
        if(s.back() == '-')
        {
            s.pop_back();
            if(s.size() > sn.size()) return 0;
            else if(s.size() == sn.size() && s > sn) return 0;
            s = "-" + s;
        }
        else
        {
            if(s.size() > sx.size()) return 0;
            else if(s.size() == sx.size() && s > sx) return 0;
        }

        ans = stoi(s);
        return ans;
    }
};
相关推荐
还是码字踏实3 小时前
算法题种类与解题思路全面指南:基于LeetCode Hot 100与牛客Top 101
算法·leetcode
Victory_orsh4 小时前
“自然搞懂”深度学习(基于Pytorch架构)——010203
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
CoovallyAIHub4 小时前
突破360°跟踪极限!OmniTrack++:全景MOT新范式,HOTA指标狂飙43%
深度学习·算法·计算机视觉
得物技术4 小时前
得物管理类目配置线上化:从业务痛点到技术实现
后端·算法·数据分析
CoovallyAIHub5 小时前
首个大规模、跨模态医学影像编辑数据集,Med-Banana-50K数据集专为医学AI打造(附数据集地址)
深度学习·算法·计算机视觉
熬了夜的程序员5 小时前
【LeetCode】101. 对称二叉树
算法·leetcode·链表·职场和发展·矩阵
却道天凉_好个秋5 小时前
目标检测算法与原理(二):Tensorflow实现迁移学习
算法·目标检测·tensorflow
柳鲲鹏6 小时前
RGB转换为NV12,查表式算法
linux·c语言·算法