目录
- notes
- java
switch
语句 - java api: array 直接有
length
属性 ,不必:length()
- sol 657
- java
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;
}
}
原文地址:https://www.cnblogs.com/paulkg12/p/12258422.html
时间: 2024-11-08 19:36:46