LeetCode--No.1041--Robert Bounded in Circle

On an infinite plane, a robot initially stands at (0, 0) and faces north.  The robot can receive one of three instructions:

  • "G": go straight 1 unit;
  • "L": turn 90 degrees to the left;
  • "R": turn 90 degress to the right.

The robot performs the instructions given in order, and repeats them forever.

Return true if and only if there exists a circle in the plane such that the robot never leaves the circle.

 

Example 1:

Input: "GGLLGG"
Output: true
Explanation: 
The robot moves from (0,0) to (0,2), turns 180 degrees, and then returns to (0,0).
When repeating these instructions, the robot remains in the circle of radius 2 centered at the origin.

Example 2:

Input: "GG"
Output: false
Explanation: 
The robot moves north indefinetely.

Example 3:

Input: "GL"
Output: true
Explanation: 
The robot moves from (0, 0) -> (0, 1) -> (-1, 1) -> (-1, 0) -> (0, 0) -> ...

 

Note:

  1. 1 <= instructions.length <= 100
  2. instructions[i] is in {'G', 'L', 'R'}

 

第一次参加竞赛。 2019/05/11 10:30pm 有点意思

新题果然都是看起来很难的

一共四道题,只做出来一道easy, 排名1790/4109, 提交了3次错误答案。
丢人啊哈哈哈,那道题就不粘出来了。第二道题提交了1次,错了。
后来过了时间,改对了
代码写的是真的难看

题目真的不难,思路完全有,并且没什么问题。
只是数组多了就乱了,不简洁,写起来小错误就会更多,变量名随便乱起,自己都不知道该用什么。

数组下标从0开始,与题目里面设计的index对应不上,想不清楚什么时候该减1,什么时候不动。

自己跑test case能力很弱,test case设计不好,corner case考虑不到,这些都是问题。
总体上说,思路不够清晰。其实这两道题都很简单,都应该是一次A过 bug free的那一种。
看来改变coding的习惯还是要时刻注意。加油~!希望下周至少能顺利A出来两道题吧。
 

class Solution {
    public int[] gardenNoAdj(int N, int[][] paths) {
        int[] res = new int[N];
        if (paths == null || paths.length == 0 || paths[0].length == 0){
            for(int i = 0; i < N; i++){
                res[i] = 1;
            }
            return res;
        }
        Map<Integer, int[]> map = new HashMap<>();
        for(int[] path: paths){
            int min = Math.min(path[0], path[1]);
            int max = Math.max(path[0], path[1]);
            if (map.containsKey(max)){
                int[] tmp = map.get(max);
                if (tmp[1] == -1)   tmp[1] = min;
                else   tmp[2] = min;
                map.put(max, tmp);
            }
            else{
                int[] tmp = new int[3];
                tmp[0] = min;
                tmp[1] = -1;
                tmp[2] = -1;
                map.put(max, tmp);
            }
        }

        res[0] = 1;
        for(int i = 1; i < N; i++){
            if (!map.containsKey(i+1))
                res[i] = 1;
            else{
                int[] tmp = map.get(i+1);
                int[] tmp2 = new int[4];
                for(int k = 0; k < 4; k++)
                    tmp2[k] = 0;
                
                if (tmp[0] != -1)   tmp2[res[tmp[0] - 1]-1] = 1;
                if (tmp[1] != -1)   tmp2[res[tmp[1] - 1]-1] = 1;
                if (tmp[2] != -1)   tmp2[res[tmp[2] - 1]-1] = 1;

                if (tmp2[0] == 0)   res[i] = 1;
                else if (tmp2[1] == 0)  res[i] = 2;
                else if (tmp2[2] == 0)  res[i] = 3;
                else res[i] = 4;
            }
        }
        return res;
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值