首页 > 编程语言 > 详细

LeetCode-657 Robot Return to Origin Solution (with Java)

时间:2020-03-02 16:55:29      阅读:68      评论:0      收藏:0      [点我收藏+]

1. Description:

技术分享图片

2. Examples:

技术分享图片

3.Solutions:

 1 /**
 2  * Created by sheepcore on 2019-02-24
 3  * best    time complexity: O(n)
 4  * average time complexity: O(n)
 5  * worst   time complexity: O(n)
 6  * space complexity: O(4);
 7  */
 8 class Solution {
 9     public boolean judgeCircle(String moves) {
10         int lefts = 0, rights = 0, ups = 0, downs = 0;
11         for (int i = 0; i < moves.length(); i++) {
12             if (moves.charAt(i) == ‘L‘ || moves.charAt(i) == ‘l‘)
13                 lefts++;
14             else if (moves.charAt(i) == ‘R‘ || moves.charAt(i) == ‘r‘)
15                 rights++;
16             else if (moves.charAt(i) == ‘U‘ || moves.charAt(i) == ‘u‘)
17                 ups++;
18             else if (moves.charAt(i) == ‘D‘ || moves.charAt(i) == ‘d‘)
19                 downs++;
20         }
21         if (lefts == rights && ups == downs)
22             return true;
23         else
24             return false;  
25     }
26 }

 

LeetCode-657 Robot Return to Origin Solution (with Java)

原文:https://www.cnblogs.com/sheepcore/p/12396066.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!