2021_02_01 LeetCode刷题

1736. 替换隐藏数字得到的最晚时间

简单模拟题。就一个个判定即可。

class Solution {
public:
    string maximumTime(string time) {
        if(time[0] == '?') {
            if(time[1] == '?')
                time[0] = '2', time[1] = '3';
            else {
                if(time[1] >= '0' && time[1] <= '3')
                    time[0] = '2';
                else 
                    time[0] = '1';
            }
        } else {
            if(time[0] == '0') {
                if(time[1] == '?')
                    time[1] = '9';
            }
            if(time[0] == '1') {
                if(time[1] == '?')
                    time[1] = '9';
            }
            if(time[0] == '2') {
                if(time[1] == '?')
                    time[1] = '3';
            }
        }

        if(time[3] == '?') {
            if(time[4] == '?')
                time[3] = '5', time[4] = '9';
            else
                time[3] = '5';
        } else {
            if(time[4] == '?')
                time[4] = '9';
        }
        return time;
    }
};

1346. 检查整数及其两倍数是否存在

简单模拟题。使用哈希表解决。

class Solution {
public:
    bool checkIfExist(vector<int>& arr) {
        int cnt[4001] = {0};
        int* hash_set = cnt + 2000;
        for (int n : arr)
            ++hash_set[n];
        for (int n : arr)
            if (n != 0 && hash_set[2 * n] >= 1)
                return true;
            else if (n == 0 && hash_set[2 * n] >= 2)
                return true;
        return false;
    }
};

1576. 替换所有的问号

简单模拟题。判定前后是否为问号,同时保证前后字符与其不相同即可。

class Solution {
public:
    string modifyString(string s) {
        int len1 = s.length();
        if(len1 == 1)
            s = "a";
        else {
            for(int i = 0; i < len1 ; i++) {
                if(s[i] == '?') {
                    if(i == 0) {
                        if(s[i + 1] == '?')
                            s[i] = 'a';
                        else {
                            if(s[i + 1] == 'z')
                                s[i] = 'y';
                            else
                                s[i] = s[i + 1] + 1;
                        }
                    } else {
                        if(i == len1 - 1) {
                            if(s[i - 1] == 'z')
                                s[i] = 'y';
                            else
                                s[i] = s[i - 1] + 1;
                        } else {
                            if(s[i+1] == '?') {
                                if(s[i - 1] == 'z')
                                    s[i] = 'y';
                                else
                                    s[i] = s[i - 1] + 1;
                            } else {
                                for(char j = 'a'; j <= 'z'; j++) {
                                    if(s[i-1] != j && s[i + 1] != j) {
                                        s[i] = j;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return s;
    }
};

929. 独特的电子邮件地址

简单模拟题。以@为分隔符,之前的所有字符判定.和+,之后的则全部记录,然后去重保存不同种类的个数即可。

class Solution {
public:
    int numUniqueEmails(vector<string>& emails) {
        set<string> p;
        int len1 = emails.size();
        for(int i = 0; i < len1; i++) {
            int len2 = emails[i].length();
            string tempz = "";
            for(int j = 0; j < len2 ; j++) {
                if(emails[i][j] == '.')
                    continue;
                if(emails[i][j] == '+') {
                    while(j < len2 && emails[i][j]!='@')
                        j++;
                    while(j < len2) {
                        tempz = tempz + emails[i][j];
                        j += 1;
                    }
                    p.insert(tempz);
                    break;
                }
                if(emails[i][j] == '@') {
                    while(j < len2) {
                        tempz = tempz + emails[i][j];
                        j += 1;
                    }
                    p.insert(tempz);
                    break;
                }
                tempz += emails[i][j];
            }
            
        }
        return p.size();
    }
};

1441. 用栈操作构建数组

简单模拟题。从1开始模拟输入,遍历数组,判定和当前需要放进的数字是否相同,如果不相同,说明有push和pop操作,否则就是单纯的push操作。

class Solution {
public:
    vector<string> buildArray(vector<int>& target, int n) {
        vector<string> ans;
        int len1 = target.size(), sum = 1;
        for(int i = 0;i < len1; ) {
            if(target[i] == sum) {
                ans.push_back("Push");
                sum += 1;
                i += 1;
            } else {
                ans.push_back("Push");
                ans.push_back("Pop");
                sum += 1;
            }
        }
        return ans;
    }
};

1694. 重新格式化电话号码

简单模拟题。先记录数字个数,之后每三个存储,过程中记得判定当前还剩下几个字符,当剩下2,3,4时直接操作即可。

class Solution {
public:
    string reformatNumber(string number) {
        int len1 = number.length();
        string temp = "";
        for(int i = 0;i < len1; i++) {
            if(number[i] >= '0' && number[i] <= '9')
                temp += number[i];
        }
        string ans = "";
        int len2 = temp.length();
        if(len2 == 2 || len2 == 3)
            ans = temp;
        else {
            if(len2 == 4)
                ans = ans + temp[0] + temp[1] + "-" + temp[2] + temp[3];
            else {
                for(int i = 0; i < len2; i+=3) {
                    if((len2 - i) <= 4) {
                        if((len2 - i) == 2)
                            ans = ans + temp[i] + temp[i + 1];
                        if((len2 - i) == 3)
                            ans = ans + temp[i] + temp[i + 1] + temp[i + 2];
                        if((len2 - i) == 4)
                            ans = ans + temp[i] + temp[i + 1] + "-" + temp[i + 2] + temp[i + 3];
                        break;
                    } else
                        ans = ans + temp[i] + temp[i+1] + temp[i+2] + "-";
                }
            }
        }
        return ans;
    }
};

1608. 特殊数组的特征值

简单模拟题。直接遍历排序后的数组,记录根据下标获得当前大于当前数字的个数,然后判定特征值是否存在。

class Solution {
public:
    int specialArray(vector<int>& nums) {
        sort(nums.begin(),nums.end());
        int len1 = nums.size(), ans = -1;
        for(int i = 0; i < len1; i++) {
            int sum = len1 - i;
            if(i == 0) {
                if(sum <= nums[i]) {
                    ans = sum;
                    break;
                } 
            } else {
                if(sum <= nums[i] && sum > nums[i-1]) {
                    ans = sum;
                    break;
                }
            }
        }
        return ans;
    }
};

1394. 找出数组中的幸运数

简单模拟题。哈希表,存放查找。

class Solution {
public:
    int findLucky(vector<int>& arr) {
        map<int, int> p;
        int len1 = arr.size();
        for(int i = 0; i < len1; i++)
            p[arr[i]] += 1;
        int max1 = -1;
        for(map<int, int>::iterator it1 = p.begin(); it1 != p.end(); it1++) {
            if(it1 -> second == it1 -> first)
                max1 = max(max1, it1->first);
        }
        return max1;
    }
};

1399. 统计最大组的数目

简单模拟题。哈希表记录存放数字的和,之后对哈希表排序即可返回最终的结果。

class Solution {
public:
    int countLargestGroup(int n) {
        unordered_map<int, int> hashMap;
        int maxValue = 0;
        for (int i = 1; i <= n; ++i) {
            int key = 0, i0 = i;
            while (i0) {
                key += i0 % 10;
                i0 /= 10;
            }
            ++hashMap[key];
            maxValue = max(maxValue, hashMap[key]);
        }
        int count = 0;
        for (auto& kvpair: hashMap) {
            if (kvpair.second == maxValue) {
                ++count;
            }
        }
        return count;
    }
};

1337. 矩阵中战斗力最弱的 K 行

简单模拟题。存放两个变量,之后重写排序规则,即可得出答案。

class Solution {
public:
    vector<int> kWeakestRows(vector<vector<int>>& mat, int k) {
        int n = mat.size();
        vector<pair<int, int>> power;
        for (int i = 0; i < n; ++i) {
            int sum = accumulate(mat[i].begin(), mat[i].end(), 0);
            power.emplace_back(sum, i);
        }
        sort(power.begin(), power.end());
        vector<int> ans;
        for (int i = 0; i < k; ++i) {
            ans.push_back(power[i].second);
        }
        return ans;
    }
};

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值