L292 Nim游戏
GitHub 账户:LuvnJoae 欢迎关注! https://github.com/LuvnJoae
GitHub 代码链接:https://github.com/LuvnJoae/Java_leetcode
思路与结果
代码
思路1
package Day9_5_15.L292;
public class Solution {
public boolean canWinNim(int n) {
return ( (n % 4) != 0 );
}
}
L905 按奇偶排序数组
思路与结果
代码
思路1
package Day9_5_15.L905;
public class Solution {
public int[] sortArrayByParity(int[] A) {
int[] result = new int[A.length];
int min = 0, max = A.length-1;
for (int i = 0; i < A.length; i++) {
// if ( (A[i] & 1) == 0){
if ( (A[i] % 2) == 0){ // 相与 或 取余两种都可以,复杂度上没什么明显差别。
result[min] = A[i];
min++;
}else {
result[max] = A[i];
max--;
}
}
return result;
}
}
L728 自除数
思路与结果
代码
思路1
package Day9_5_15.L728;
import java.util.ArrayList;
import java.util.List;
public class Solution {
public static List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++){
int ge = i % 10;
int shi = (i % 100) / 10;
int bai = (i % 1000) / 100;
int qian = i / 1000;
if (ge == 0 || (shi == 0 && i > 10) || (bai == 0 && i > 100)|| (qian == 0 && i > 1000)){
continue;
} else if (i>1000 && (i % qian == 0 && i % bai ==0 && i % shi ==0 && i % ge == 0)){
list.add(i);
continue;
} else if (i>100 && i<1000 && (i % bai == 0 && i % shi == 0 && i % ge == 0)){
list.add(i);
continue;
} else if (i>10 && i<100 &&( i % shi ==0 && i % ge == 0) ){
list.add(i);
continue;
} else if (i<10){
list.add(i);
}
}
return list;
}
}
思路2
package Day9_5_15.L728;
import java.util.ArrayList;
import java.util.List;
public class Solution2 {
public List<Integer> selfDividingNumbers(int left, int right){
List<Integer> list = new ArrayList<>();
for (int i = left; i <= right; i++){
if (judge(i)){
list.add(i);
}
}
return list;
}
public boolean judge(int i){
int num = i;
while (i > 0){
int m = i % 10;
if (m == 0){
return false;
}
if (num % m != 0){
return false;
}
i /= 10;
}
return true;
}
}