醒醒神,爱奇艺笔试真题

醒醒神,爱奇艺笔试真题

刷题多总结,莫盲目。

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;
    }
};

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

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

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值