leetcode解题思路分析(四十二)355 - 361 题

男娘i 2022-12-07 12:28 228阅读 0赞
  1. 设计推特
    设计一个简化版的推特(Twitter),可以让用户实现发送推文,关注/取消关注其他用户,能够看见关注人(包括自己)的最近十条推文。

    int global_Time = 0;//发表推文的时间 全局的
    //推文类
    class Tweet {
    public:

    1. int id;
    2. int time;
    3. Tweet *next;
    4. Tweet(int id) {
    5. this->id = id;
    6. this->time = global_Time++;
    7. next = nullptr;
    8. }

    };

    //用户类
    class User {
    public:

    1. int id;
    2. Tweet *tweet; //该用户发送的推文 是个链表
    3. unordered_set<int> follows; //该用户关注的用户
    4. User(int id) {
    5. this->id = id;
    6. tweet = nullptr;
    7. }
    8. void follow(int followeeId) {
    9. //不能关注自己
    10. if (followeeId == id) return;
    11. follows.insert(followeeId);
    12. }
    13. void unfollow(int followeeId) {
    14. //没有关注 或者 不能取关自己
    15. if (!follows.count(followeeId) || followeeId == id) return;
    16. follows.erase(followeeId);
    17. }
    18. void post(int tweetId) {
    19. Tweet *newTweet = new Tweet(tweetId);
    20. //链表 采用头插法 新发表插在前面
    21. newTweet->next = tweet;
    22. //新的链表
    23. tweet = newTweet;
    24. }

    };

    class Twitter {
    private:

    1. unordered_map<int, User*> user_map; //所有的用户
    2. bool contain(int id) {
    3. return user_map.find(id) != user_map.end();
    4. }

    public:

    1. Twitter() {
    2. user_map.clear();
    3. }
    4. void postTweet(int userId, int tweetId) {
    5. if (!contain(userId)) {
    6. user_map[userId] = new User(userId);
    7. }
    8. //用户 发表 推文 面向对象
    9. user_map[userId]->post(tweetId);
    10. }
    11. vector<int> getNewsFeed(int userId) {
    12. //用户不存在
    13. if (!contain(userId)) return { };
    14. struct cmp {
    15. bool operator()(const Tweet *a, const Tweet *b) {
    16. return a->time < b->time;
    17. }
    18. };
    19. //构造大顶堆 时间最大的排在最上面
    20. priority_queue<Tweet*, vector<Tweet*>, cmp> q;
    21. //自己的推文链表
    22. if (user_map[userId]->tweet) {
    23. q.push(user_map[userId]->tweet);
    24. }
    25. //关注的推文链表
    26. for (int followeeId : user_map[userId]->follows) {
    27. if (!contain(followeeId)) {
    28. continue;
    29. }
    30. Tweet *head = user_map[followeeId]->tweet;
    31. if (head == nullptr) continue;
    32. q.push(head);
    33. }
    34. vector<int> rs;
    35. while (!q.empty()) {
    36. Tweet *t = q.top();
    37. q.pop();
    38. rs.push_back(t->id);
    39. if (rs.size() == 10) return rs;
    40. if (t->next) {
    41. q.push(t->next);
    42. }
    43. }
    44. return rs;
    45. }
    46. // 用户followerId 关注 用户followeeId.
    47. void follow(int followerId, int followeeId) {
    48. if (!contain(followerId)) {
    49. user_map[followerId] = new User(followerId);
    50. }
    51. //面向对象
    52. user_map[followerId]->follow(followeeId);
    53. }
    54. // 用户followerId 取关 用户followeeId.
    55. void unfollow(int followerId, int followeeId) {
    56. if (!contain(followerId)) return;
    57. //面向对象
    58. user_map[followerId]->unfollow(followeeId);
    59. }

    };

  2. 计算各个位数不同的数字个数
    给定一个非负整数 n,计算各位数字都不同的数字 x 的个数,其中 0 ≤ x < 10n 。

dp[i]表示有i位数的满足条件的数字一共有多少,那么dp[i+1]只能在这些数字添加非重复的数字。前面已经占用了i位数字了,那么只剩10-i位了。 注意要取dp[1] = 9, 表示第一位不能取0。

  1. class Solution
  2. {
  3. public:
  4. int countNumbersWithUniqueDigits(int n)
  5. {
  6. if (n == 0) return 1;
  7. if (n == 1) return 10;
  8. int dp = 9, sum = 10;
  9. for (int i = 2; i <= min(n, 10); i++)
  10. {
  11. dp = dp * (11 - i);
  12. sum += dp;
  13. }
  14. return sum;
  15. }
  16. };
  1. 矩形区域不超过K的最大数值和
    给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和。

本题由于列的数量远远小于行的数量,因此应该转化思维,固定起始列,不断增大 终止列号,然后计算起始列和终止列之间的某行的累计和,从而解决问题

  1. class Solution {
  2. public:
  3. int maxSumSubmatrix(vector<vector<int>>& matrix, int k) {
  4. int m = matrix.size();
  5. if(m == 0 || matrix[0].size() == 0) return 0;
  6. int n = matrix[0].size();
  7. // i 代表矩形的起始行
  8. int ans = INT_MIN;
  9. // 起始列
  10. for(int i = 0; i < n; i++){
  11. // 终止列
  12. vector<int> rowCums(m, 0);
  13. for(int j = i; j < n; j++){
  14. set<int> pres;
  15. int preSum = 0;
  16. for(int r = 0; r < m; r++){
  17. rowCums[r] += matrix[r][j];
  18. preSum += rowCums[r];
  19. if(preSum <= k) ans = max(ans, preSum);
  20. auto it = pres.lower_bound(preSum - k);
  21. if(it != pres.end() ){ ans = max(ans, preSum - *it); }
  22. pres.insert(preSum);
  23. }
  24. }
  25. }
  26. return ans;
  27. }
  28. };
  1. 水壶问题
    有两个容量分别为 x升 和 y升 的水壶以及无限多的水。请判断能否通过使用这两个水壶,从而可以得到恰好 z升 的水?

我们可以认为每次操作只会给水的总量带来 x 或者 y 的变化量。因此我们的目标可以改写成:找到一对整数 a, ba,b,使得ax+by=z。而贝祖定理告诉我们,ax+by=zax+by=z 有解当且仅当 zz 是 x, yx,y 的最大公约数的倍数。因此我们只需要找到 x, yx,y 的最大公约数并判断 zz 是否是它的倍数即可。

  1. class Solution {
  2. public:
  3. bool canMeasureWater(int x, int y, int z) {
  4. if (x + y < z) return false;
  5. if (x == 0 || y == 0) return z == 0 || x + y == z;
  6. return z % gcd(x, y) == 0;
  7. }
  8. };
  1. 有效的完全平方数
    给定一个正整数 num,编写一个函数,如果 num 是一个完全平方数,则返回 True,否则返回 False。

很简单的一道题,最简单的做法是遍历到1/2的位置,一路检查。更优的做法是判断是否由1357一系列基数构成。因为(n+1)2-n2=2n+1

  1. class Solution {
  2. public:
  3. bool isPerfectSquare(int num) {
  4. if (num == 1) return true;
  5. for (int i = 1; i <= num / 2; i++)
  6. {
  7. int tmp = num / i;
  8. int remain = num % i;
  9. if (i > tmp)
  10. return false;
  11. else if (i == tmp && remain == 0)
  12. return true;
  13. }
  14. return false;
  15. }
  16. };
  17. class Solution
  18. {
  19. public:
  20. bool isPerfectSquare(int num)
  21. {
  22. int num1 = 1;
  23. while(num > 0)
  24. {
  25. num -= num1;
  26. num1 += 2;
  27. }
  28. return num == 0;
  29. }
  30. };
  1. 最大整数子集
    给出一个由无重复的正整数组成的集合,找出其中最大的整除子集,子集中任意一对 (Si,Sj) 都要满足:Si % Sj = 0 或 Sj % Si = 0。

本题的解法建立在数学推导上:对现有集合加入一个新元素,如果是最大元素的倍数或者最小元素的约数则加入后依然可以保证这个性质,知道了这个就可以用动态规划求解

  1. class Solution {
  2. public:
  3. static const int N = 1e3 + 5;
  4. int dp[N], ans_len = 1, ans_index = 0;
  5. std::vector<int> ans;
  6. void print(int index, vector<int>& nums) {
  7. if (dp[index] == 1) {
  8. ans.push_back(nums[index]);
  9. return;
  10. }
  11. for (int j = 0; j < index; ++j) {
  12. if (nums[index] % nums[j] == 0 && dp[j] + 1 == dp[index]) {
  13. ans.push_back(nums[index]);
  14. print(j, nums);
  15. break;
  16. }
  17. }
  18. }
  19. vector<int> largestDivisibleSubset(vector<int>& nums) {
  20. if (nums.empty()) return { };
  21. std::sort(nums.begin(), nums.end());
  22. dp[0] = 1;
  23. int n = nums.size();
  24. for (int i = 1; i < n; ++i) {
  25. dp[i] = 1;
  26. for (int j = 0; j < i; ++j) {
  27. if (nums[i] % nums[j] == 0) dp[i] = std::max(dp[i], dp[j] + 1);
  28. }
  29. if (dp[i] > ans_len) {
  30. ans_len = dp[i];
  31. ans_index = i;
  32. }
  33. }
  34. print(ans_index, nums);
  35. return ans;
  36. }
  37. };
  1. 两整数之和
    不使用运算符 + 和 - ​​​​​​​,计算两整数 ​​​​​​​a 、b ​​​​​​​之和。

a ^ b可以得到两数相加不进位的加法结果
(a & b) << 1可以得到两数相加产生的进位

  1. class Solution {
  2. public:
  3. int getSum(int a, int b) {
  4. while (b) {
  5. auto carry = ((unsigned int)(a & b)) << 1;
  6. a ^= b;
  7. b = carry;
  8. }
  9. return a;
  10. }
  11. };

发表评论

表情:
评论列表 (有 0 条评论,228人围观)

还没有评论,来说两句吧...

相关阅读