思路:直接两层循环,当和小于所给的值bound,就添加到哈希表中,最后转换为list
class Solution {
public List<Integer> powerfulIntegers(int x, int y, int bound) {
Set<Integer> set = new HashSet<>();
for(int i = 1;i < bound; i *= x){
for(int j = 1;j < bound; j *= y){
if(i+j <= bound)
set.add(i + j);
if(y == 1) break;
}
if(x == 1) break;
}
return new ArrayList<>(set);
}
}
思路: 先计数,然后求他们的最大公约数,如果最大公约数等于1,直接返回false,否则符合要求
class Solution {
public boolean hasGroupsSizeX(int[] deck) {
int[] count = new int[10000];
for (int num: deck) {
count[num]++;
}
int x = 0;
for(int cnt: count) {
if (cnt > 0) {
x = gcd(x, cnt);
if (x == 1) {
return false;
}
}
}
return true;
}
private int gcd (int a, int b) {
return b == 0? a: gcd(b, a % b);
}
}
思路:统计求余之后各余数的个数 ,如果对应的个数不匹配,直接返回false, 余数中0的个数必须是偶数
class Solution {
public boolean canArrange(int[] arr, int k) {
int[] mod = new int[k];
for (int num : arr)
mod[(num % k + k) % k]++;
for (int i = 1; i <= k / 2; ++i)
if (mod[i] != mod[k - i])
return false;
return mod[0] % 2 == 0;
}
}