# 2022-12-11 leetcode与蓝桥刷题情况

96 篇文章 2 订阅

## 一、leetcode题目( 今天的leetcode是写的周赛题目)

### 测试用例

nums = [4,3,6,16,8,2]

3

4 = 2 * 2.
16 = 4 * 4.

### 算法实现

public int longestSquareStreak(int[] nums) {
int n = nums.length;
Arrays.sort(nums);
HashSet<Integer> set = new HashSet<>();
for(int num : nums){
if(!set.contains(num)) set.add(num);
}
int ans = 0;
for(int i = 0; i < n; i++){
int cur = nums[i];
int max = 0;
while(set.contains(cur)){
max++;
cur *= cur;
}
ans = Math.max(ans, max);
}
return ans == 1 ? -1 : ans;
}


### 测试用例

grid = [[1,2,3],[2,5,7],[3,5,1]], queries = [5,6,2]

[5,8,1]

### dfs算法实现

class Solution {
public int[] maxPoints(int[][] grid, int[] queries) {
boolean[][] matrix = new boolean[grid.length][grid[0].length];
int[] ans = new int[queries.length];
int i = 0;
for(int querie : queries){
ans[i++] = dfs(grid, matrix, 0, 0, querie);
for(boolean[] arr : matrix){
Arrays.fill(arr, false);
}
}
return ans;
}

private int dfs(int[][] grid, boolean[][] matrix, int i, int j, int max){
if(i < 0 || i >= grid.length || j < 0 || j >= grid[0].length || matrix[i][j] == true || grid[i][j] >= max) return 0;
matrix[i][j] = true;
return 1+dfs(grid,matrix,i+1,j,max)+dfs(grid,matrix,i-1,j,max)+dfs(grid,matrix,i,j+1,max)+dfs(grid,matrix,i,j-1,max);
}
}


## 二、蓝桥题目

，如下图所示：

：根的深度是 1

7
1 6 5 4 3 2 1

2

### 4.算法实现

import java.util.Scanner;
// 1:无需package
// 2: 类名必须Main, 不可修改

public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
//在此输入您的代码...
int n = scan.nextInt();
int[] tree = new int[n+1];
for(int i = 1; i <= n; i++){
tree[i] = scan.nextInt();
}
int path = (n + 1) / 2;
int ans = 0;
int max = Integer.MIN_VALUE;
int k = 1;
while(k < path){
int cur = 0;
for(int i = 2*k; i < (k+1)*2; i++){
cur += tree[i];
}
if(cur > max){
max = cur;
ans = k;
}
k++;
}
System.out.println(ans);
scan.close();
}
}

• 0
点赞
• 0
收藏
觉得还不错? 一键收藏
• 0
评论
12-11 88
09-30 353
12-19 103
12-11 500
10-28 5268
06-27 2376
04-14 471
04-14 773
04-14 406
04-14 400

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、付费专栏及课程。