码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode: 0204 完成的

时间:2020-02-04 10:36:04      阅读:95      评论:0      收藏:0      [点我收藏+]

标签:class   java   深度   amp   bool   int   ++   self   dep   

0204 需要完成的

? 657. 机器人能否返回原点

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

? 1299. 将每个元素替换为右侧最大元素

https://leetcode-cn.com/problems/replace-elements-with-greatest-element-on-right-side/

? 1051 高度检查器

https://leetcode-cn.com/problems/height-checker

? 728 自除数

https://leetcode-cn.com/problems/self-dividing-numbers

? 104 二叉树的最大深度

https://leetcode-cn.com/problems/maximum-depth-of-binary-tree

notes

java switch 语句

switch(expression){
    case value :
       //语句
       break; //可选
    case value :
       //语句
       break; //可选
    //你可以有任意数量的case语句
    default : //可选
       //语句
}

java api: array 直接有 length 属性 ,不必:length()

sol 657

class Solution {
    public boolean judgeCircle(String moves) {
        char [] allMovesSplited = moves.toCharArray();
        int len = allMovesSplited.length;
        int i = 0;
        int x = 0;
        int y = 0;
        while(i < len) {
            switch(allMovesSplited[i]) {
                case 'U':
                    y++;
                    break;
                case 'D':
                    y--;
                    break;
                case 'L':
                    x--;
                    break;
                case 'R':
                    x++;
                    break;
                default:
                    break;
            }
            i++;
        }
        return x==0 && y==0;
    }
}
//or 
class Solution {
    public boolean judgeCircle(String moves) {
        int col = 0, row = 0;
        for(char ch : moves.toCharArray()){
            if(ch == 'U') row++;
            else if(ch == 'D') row--;
            else if(ch == 'L') col--;
            else col++;
        }
        return col == 0 && row == 0;
    }
}

leetcode: 0204 完成的

标签:class   java   深度   amp   bool   int   ++   self   dep   

原文地址:https://www.cnblogs.com/paulkg12/p/12258422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!