2021_02_02 LeetCode刷题

1437. 是否所有 1 都至少相隔 k 个元素

简单模拟题。遍历数组判定即可。

class Solution {
public:
    bool kLengthApart(vector<int>& nums, int k) {
        int n = nums.size();
        int prev = -1;
        for (int i = 0; i < n; ++i) {
            if (nums[i] == 1) {
                if (prev != -1 && i - prev - 1 < k)
                    return false;
                prev = i;
            }
        }
        return true;
    }
};

1287. 有序数组中出现次数超过25%的元素

简单模拟题。有序数组,遍历过程中统计当前重复数字的个数是否超过了整个数组的25%即可。

class Solution {
public:
    int findSpecialInteger(vector<int>& arr) {
        int n = arr.size();
        int cur = arr[0], cnt = 0;
        for (int i = 0; i < n; ++i) {
            if (arr[i] == cur) {
                ++cnt;
                if (cnt * 4 > n)
                    return cur;
            }
            else {
                cur = arr[i];
                cnt = 1;
            }
        }
        return -1;
    }
};

1507. 转变日期格式

简单模拟题。使用哈希表映射转换即可。

class Solution {
public:
    string reformatDate(string date) {
        unordered_map<string, string> s2month = {
            {"Jan", "01"},
            {"Feb", "02"},
            {"Mar", "03"},
            {"Apr", "04"},
            {"May", "05"},
            {"Jun", "06"},
            {"Jul", "07"},
            {"Aug", "08"},
            {"Sep", "09"},
            {"Oct", "10"},
            {"Nov", "11"},
            {"Dec", "12"}
        };

        stringstream ss(date);
        string year, month, day;
        ss >> day >> month >> year;
        month = s2month[month];
        day.pop_back();
        day.pop_back();
        if (day.size() == 1)
            day = "0" + day;
        return year + "-" + month + "-" + day;
    }
};

1446. 连续字符

简单模拟题。统计最长的重复子串即可。

class Solution {
public:
    int maxPower(string s) {
        int len1 = s.length(), ans = 0, sum = 0;
        for(int i = 0; i < len1; i++) {
            sum = 1;
            while((i+1) < len1 && s[i+1] == s[i])
                sum += 1,i += 1;
            ans = max(ans, sum);
        }
        return ans;
    }
};

1556. 千位分隔数

简单模拟题。直接按照题目意思实现即可。

class Solution {
public:
    string thousandSeparator(int n) {
        string ans = "";
        if(n <= 999)
            ans = to_string(n);
        else {
            int sum = 0;
            stack<string> p;
            while(n) {
                p.push(to_string(n%10));
                n /= 10;
                sum += 1;
                if(sum == 3 && n != 0) {
                    sum = 0;
                    p.push(".");
                }
            }
            while(!p.empty()) {
                ans = ans + p.top();
                p.pop();
            }
        }
        return ans;
    }
};

1408. 数组中的字符串匹配

简单模拟题。每个字符串查找是否为另一个字符串的子串即可。

class Solution {
public:
    vector<string> stringMatching(vector<string>& words) {
        vector<string> ans;
        int len1 = words.size();
        for(int i = 0; i < len1; i++) {
            for(int j = 0;j < len1; j++) {
                if(i == j)
                    continue;
                if(words[j].find(words[i])!= string::npos) {
                    ans.push_back(words[i]);
                    break;
                }
            }
        }
        return ans;
    }
};

1184. 公交站间的距离

简单模拟题。先统计所有距离的和,然后顺序统计起点和终点的距离,另一个距离就是距离总和减去顺序求出的,最后比较返回较小值即可。

class Solution {
public:
    int distanceBetweenBusStops(vector<int>& distance, int start, int destination) {
        int len1 = distance.size(), sum = 0, sum1 = 0;
        for(int i = 0; i < len1; i++)
            sum += distance[i];
        int temp1 = min(start, destination), temp2 = max(start, destination);
        for(int i = temp1; i < temp2; i++) 
            sum1 += distance[i];
        return min(sum1, sum - sum1);
    }
};

1260. 二维网格迁移

简单模拟题。按照题目意思模拟,将所有元素先放置到一个一维数组中,之后根据移动的次数找到第一个放入矩阵中的元素,之后依次按顺序放入即可。

class Solution {
public:
    vector<vector<int>> shiftGrid(vector<vector<int>>& grid, int k) {
        vector<vector<int>> ans;
        int m = grid.size(), n = grid[0].size();
        k = k % (m * n);
        vector<int> temp;
        for(int i = 0; i < m; i++)
            for(int j = 0; j < n; j++)
                temp.push_back(grid[i][j]);
        int sum = 0;
        vector<int> z;
        for(int i = m * n - k; i < m * n; i++) {
            z.push_back(temp[i]);
            sum += 1;
            if(sum == n) {
                sum = 0;
                ans.push_back(z);
                z.clear();
            }
        }
        for(int i = 0; i < m * n - k; i++) {
            z.push_back(temp[i]);
            sum += 1;
            if(sum == n) {
                sum = 0;
                ans.push_back(z);
                z.clear();
            }
        }
        return ans;
    }
};

1071. 字符串的最大公因子

简单模拟题。最大公因子子串其实要从字符串长度的因数入手,先找到二者长度的所有公因数,然后截取公因数长度的子串作为最大公因串进行判定,最后得到最终的结果。

class Solution {
public:
    string gcdOfStrings(string str1, string str2) {
        vector<int> ans;
        int len1 = str1.length(), len2 = str2.length();
        for(int i = min(len1, len2); i >= 1; i--) {
            if(len1 % i == 0 && len2 % i == 0)
                ans.push_back(i);
        }
        int len3 = ans.size();
        string ans1 = "";
        for(int i = 0; i < len3; i++) {
            string sum = str1.substr(0, ans[i]);
            int z1 = len1 / ans[i], z2 = len2 / ans[i];
            string sum1 = "", sum2 = "";
            while(z1--)
                sum1 = sum1 + sum;
            while(z2--)
                sum2 = sum2 + sum;
            if(sum1 == str1 && sum2 == str2) {
                ans1 = sum;
                break;
            }
        }
        return ans1;
    }
};

937. 重新排列日志文件

简单模拟题。自定义排序规则,重新对于所有的字符串排序即可。 

class Solution {
public:
    static bool cmp(const string &a, const string &b){
        int i = 0, j = 0;
        while(a[i] != ' ') i ++;
        while(b[j] != ' ') j ++;
        string a_content = a.substr(i), b_content = b.substr(j);
        if (a_content != b_content) return a_content < b_content;
        else return a < b;
    }
    vector<string> reorderLogFiles(vector<string>& logs) {
        vector<string> res, alp;
        for(int i = 0, j = 0; i < logs.size(); i ++){
            j = logs[i].find(' ');
            if(logs[i][j + 1] >= '0' && logs[i][j + 1] <= '9') res.push_back(logs[i]);
            else alp.push_back(logs[i]);
        }
        sort(alp.begin(),alp.end(),cmp);
        res.insert(res.begin(),alp.begin(),alp.end());
        return res;
    }
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值