醒醒神,爱奇艺笔试真题

时间:2022-07-24
本文章向大家介绍醒醒神,爱奇艺笔试真题,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

醒醒神,爱奇艺笔试真题

刷题多总结,莫盲目。

1.导语

今天看到leetcode的每日一题,点了进去发现刚好是爱奇艺真题的一个简单版,于是总结出这一篇文章。

总共两道题,第一道每日一题的题目,第二道爱奇艺笔试真题。

657. 机器人能否返回原点

https://leetcode-cn.com/problems/robot-return-to-origin

1496. 判断路径是否相交

https://leetcode-cn.com/problems/path-crossing/

2.机器人能否返回原点

能否回原点,只需要方向移动后是否还是原来位置即可,所以移动方向模拟即可。

class Solution {
public:
    bool judgeCircle(string moves) {
      int x = 0, y = 0;
      for (auto& m : moves) {
        if (m == 'R') x++;
        else if (m == 'L') x--;
        else if (m == 'U') y++;
        else if (m == 'D') y--;
      }
      return x==0 && y==0;
    }
};

3.判断路径是否相交

这道题比前面难一点点,相交不就是前面那道题回到“原点”,所有只需要记录每个点坐标是否重复出现即可。

#include <string>
#include <iostream>
#include <map>
using namespace std;
class Solution {
public:
    bool isPathCrossing(string path) {
      map<pair<int,int>, int> um; // 坐标:出现次数
      int x = 0, y = 0;
      um[{x,y}]++;
      for (auto& c : path) {
        if (c == 'W') x--;
        else if (c == 'E') x++;
        else if (c == 'N') y++;
        else if (c == 'S') y--;
        if (um.count({x,y}) > 0) return true;
        um[{x,y}]++;
      }
      return false;
    }
};

总结:刷题多总结,莫盲目。

期待您的留言,我们一起探讨算法之美。