每日算法--2023.2.27
1.面试题13. 机器人的运动范围
class Solution {
//如果方向是上下左右则考虑深搜,如果方向是下和右则考虑dp
//深度优先遍历
public int[] dx = new int[]{1,0,-1,0};
public int[] dy = new int[]{0,-1,0,1};
public boolean[][] flag;
public int M,N;
public int movingCount(int m, int n, int k) {
M = m;N = n;
flag = new boolean[m][n];
dfs(0,0,k);
int res = 0;
for(int i = 0;i<m;i++){
for(int j = 0;j<n;j++){
if(flag[i][j] == true){
res++;
}
}
}
return res;
}
public void dfs(int i, int j,int k){
int curSum = 0, p = i, q = j;
while(p!=0){
curSum += p%10;
p = p/10;
}
while(q!=0){
curSum += q%10;
q = q/10;
}
if(curSum>k){
return ;
}
flag[i][j] = true;
for(int t = 0;t<4;t++){
int xx = dx[t] + i, yy = dy[t] + j;
if(xx>=0&&xx<M&&yy>=0&&yy<N&&flag[xx][yy] == false){
dfs(xx,yy,k);
}
}
return ;
}
}
class Solution {
//广度优先遍历
class Node{
public int i;
public int j;
Node(int i, int j){
this.i = i;
this.j = j;
}
}
public int movingCount(int m, int n, int k) {
Deque<Node> queue = new LinkedList<>();
queue.addLast(new Node(0,0));
boolean[][] flag = new boolean[m][n];
flag[0][0] = true;
int[] dx = new int[]{1,0,-1,0};
int[] dy = new int[]{0,-1,0,1};
int cnt = 0;
while(!queue.isEmpty()){
Node cur = queue.getFirst();
queue.removeFirst();
cnt++;
int x = cur.i, y = cur.j;
for(int i = 0;i<4;i++){
int xx = dx[i] + x, yy = dy[i] + y;
int curSum = 0, p = xx, q = yy;
while(p!=0){
curSum += p%10;
p = p/10;
}
while(q!=0){
curSum += q%10;
q = q/10;
}
if(xx>=0&&xx<m&&yy>=0&&yy<n&&flag[xx][yy] == false&&curSum<=k){
flag[xx][yy] = true;
queue.addLast(new Node(xx,yy));
}
}
}
return cnt;
}
}
2.面试题45--把数组排成最小的数
class Solution {
//其实就是比大小原则不一样,其他的和快排思路完全一样
public String minNumber(int[] nums) {
String[] strs = new String[nums.length];
for(int i = 0; i < nums.length; i++)
strs[i] = String.valueOf(nums[i]);
quickSort(strs, 0, strs.length - 1);
StringBuilder res = new StringBuilder();
for(String s : strs)
res.append(s);
return res.toString();
}
void quickSort(String[] strs, int l, int r) {
if(l >= r) return;
int i = l, j = r;
String tmp = strs[i];
while(i < j) {
while((strs[j] + strs[l]).compareTo(strs[l] + strs[j]) >= 0 && i < j) j--;
while((strs[i] + strs[l]).compareTo(strs[l] + strs[i]) <= 0 && i < j) i++;
tmp = strs[i];
strs[i] = strs[j];
strs[j] = tmp;
}
strs[i] = strs[l];
strs[l] = tmp;
quickSort(strs, l, i - 1);
quickSort(strs, i + 1, r);
}
}
3.力扣1438--绝对差不超过限制的最长连续子数组
class Solution {
public int longestSubarray(int[] nums, int limit) {
Deque<Integer> min_deque = new LinkedList<>();
Deque<Integer> max_deque = new LinkedList<>();
int n = nums.length, left = 0, right = 0,res = 0;
while(right<n){
while(!min_deque.isEmpty()&&nums[right]<min_deque.getLast()){
min_deque.removeLast();
}
min_deque.addLast(nums[right]);
while(!max_deque.isEmpty()&&nums[right]>max_deque.getLast()){
max_deque.removeLast();
}
max_deque.addLast(nums[right]);
while(!max_deque.isEmpty()&&!min_deque.isEmpty()&&max_deque.getFirst()-min_deque.getFirst() > limit){
if(max_deque.getFirst() == nums[left]){
max_deque.removeFirst();
}
if(min_deque.getFirst() == nums[left]){
min_deque.removeFirst();
}
left++;
}
res = Math.max(res,right-left+1);
right++;
}
return res;
}
}
理想主义的花终将在现实中绽放

浙公网安备 33010602011771号