解法1:根据题意解题
代码:
class Solution{
public List<Integer> selfDividingNumbers(int left, int right) {
ArrayList<Integer> ans = new ArrayList<>();
out:for (int i = left; i <= right; i++) {
int cur = i;
while (cur != 0) {
int t = cur % 10;
if (t == 0 || i % t != 0)
continue out;
cur /= 10;
}
ans.add(i);
}
return ans;
}
}
解法2:打表 + 二分查找
代码:
class Solution {
//打表的方法
static List<Integer> ans = new ArrayList<>();
static{
out:
for (int i = 1; i <= 10000; i++) {
int temp = i;
while (temp > 0) {
int t = temp % 10;
if (t == 0 || i % t != 0) {
continue out;
}
temp /= 10;
}
ans.add(i);
}
}
public List<Integer> selfDividingNumbers(int left, int right) {
List<Integer> list = new ArrayList<>();
int l = 0;
int r = ans.size() - 1;
while (l < r) {//目的:找到[left, right]范围内最小的自除数
int mid = l + r >> 1;
if (ans.get(mid) >= left) r = mid;
else l = mid + 1;
}
while(r < ans.size() && ans.get(r) <= right) list.add(ans.get(r++));
return list;
}
解法3: 打表+哈希表
其中hash[i]代表i元素在list中的下标
class Solution {
static List<Integer> list = new ArrayList<Integer>();
static int[] hash = new int[10010];
static{
for (int i = 1; i <= 10000; i++){
int temp = i;
boolean flag = true;
while (temp > 0 && flag) {
int t = temp % 10;
if (t == 0 || i % t != 0) flag = false;
temp /= 10;
}
if (flag){
list.add(i);
}
hash[i] = list.size() - 1;
}
}
public List<Integer> selfDividingNumbers(int left, int right) {
int idx = list.get(hash[left]) == left ? hash[left] : hash[left] + 1;
List<Integer> ans = new ArrayList<Integer>();
while (idx < list.size() && list.get(idx) <= right) {
ans.add(list.get(idx++));
}
return ans;
}
}
本题总结:
/**
* 总结:
* 1.初步认识List<Integer> ans = new ArrayList<>()的使用,
* 以及打表方法;
* 2.明明早就已经对break和continue有了很明确的了解,却在写题时犯这么愚蠢的错误;
* 3.做题方法依旧很败类,需要进一步的优化;
* 4.很多学过的知识,比如静态代码块,却一直没有实践过。我还愚笨的创了个方法,然后去调用它;
* 5.对二分查找有了进一步的了解用法,以前只知道可以用它来查找确定的某个数,却忽略了它可以去查找一个范围的边界数;
* 6.写题写着写着不小心把l和left给混在一起了,以后必须多加注意;
* 7.今天刚学的哈希表,参考大佬的做法重新敲了一遍。
*/