当前位置:网站首页>力扣292周赛题解
力扣292周赛题解
2022-06-30 04:45:00 【轩辕龙儿】
力扣292周赛地址:周赛地址
第一题:字符串中最大的 3 位相同数字
力扣原题链接:
单个题解:
题解:
这题是要找最大的3个相同数并且3个数是相连的,因为数字的话只有0~9这10个数字,找最大的,那我就从999开始,然后依次888、777。。。000,只要字符串中存在,那就是它了。
java代码:
public String largestGoodInteger(String num) {
String str;
for (int i = 9; i >= 0; i--) {
str = "" + i + i + i;
if (num.contains(str)) {
return str;
}
}
return "";
}
第二题:统计值等于子树平均值的节点数
力扣原题链接:
单个题解:
题解:
这题的思路:
先深度遍历树,统计出每个节点包含的节点数,并将其放入队列中
再深度遍历一次树,这次计算出每个节点的元素和,并从队列中取到该节点的节点数,然后求平均值做判断
java代码:
class Solution {
public int averageOfSubtree(TreeNode root) {
counts(root);
sums(root);
return count;
}
Queue<Integer> queue = new LinkedList<>();
int count = 0;
private int counts(TreeNode root) {
if (root == null) {
return 0;
}
int cnt = counts(root.left) + counts(root.right) + 1;
queue.add(cnt);
return cnt;
}
private int sums(TreeNode root) {
if (root == null) {
return 0;
}
int sum = root.val;
sum += sums(root.left);
sum += sums(root.right);
if (sum / queue.poll() == root.val) {
count++;
}
return sum;
}
}
第三题:统计打字方案数
力扣原题链接:
单个题解:
题解:
这题标的是中等题,个人觉得解题方法有点取巧,怎么取巧尼,因为重复的数最多4个,我完全可以嵌套3层if来处理,当然我也是这么干的。只要遍历一遍就可以了。
在遍历到索引i时,有如下情况:
当前数字不和前面的组合,自己单独成一个新的
索引
i的种数 = 索引i-1的种数当前数字与前一个相等,那么该数字的组合就有两种情况
直接和前一个数字凑一起
索引
i的种数=索引i-2的种数索引
i的数字和索引i-2的数字也相等,这也有2种情况把索引
i,i-1,i-2都凑一起索引
i的种数=索引i-3的种数索引
i是7或者9,最多可以连续4个,把索引i,i-1,i-2,i-3都凑一起索引
i的种数=索引i-4的种数
同时,为了保证数据没有超过int的最大值,这里对于每一次的结果都对109+7取余
java代码:
class Solution {
public int countTexts(String pressedKeys) {
int[] cnts = new int[pressedKeys.length() + 1];
cnts[0] = 1;
cnts[1] = 1;
int mod = 1000000007;
for (int i = 1; i < pressedKeys.length(); i++) {
cnts[i + 1] = cnts[i];
if (pressdKeys.charAt(i) == pressedKeys.charAt(i - 1)) {
cnts[i + 1] += cnts[i - 1];
cnts[i + 1] %= mod;
if (i > 1 && pressedKeys.charAt(i) == pressedKeys.charAt(i - 2)) {
cnts[i + 1] += cnts[i - 2];
cnts[i + 1] %= mod;
if (i > 2 && pressedKeys.charAt(i) == pressedKeys.charAt(i - 3) && (pressedKeys.charAt(i) == '7' || pressedKeys.charAt(i) == '
cnts[i + 1] += cnts[i - 3];
cnts[i + 1] %= mod;
}
}
}
}
return cnts[pressedKeys.length()];
}
}
第四题:检查是否有合法括号字符串路径
力扣原题链接:
单个题解:
题解:
从左上角到右下角,依次路过,下一个坐标一定是该坐标的右侧或者下侧的坐标,同时玩吗记录下路过到该坐标时未配对的’'('的个数,如果是负数则这条路不对旧不用继续下去了。然后一直到右下角的时候,如果个数为1,则满足条件
java代码:
class Solution {
public boolean hasValidPath(char[][] grid) {
xl = grid.length;
yl = grid[0].length;
use = new boolean[xl][yl][xl * yl];
if ((xl + yl) % 2 == 0 || grid[0][0] == ')' || grid[xl - 1][yl -
return false;
}
dfs(grid, 0, 0, 0);
return bl;
}
int xl;
int yl;
boolean bl = false;
boolean[][][] use;
private void dfs(char[][] grid, int x, int y, int cnt) {
if (x >= xl || y >= yl || cnt > xl - x + yl - y - 1) {
return;
}
if (x == xl - 1 && y == yl - 1) {
bl = cnt == 1;
}
if (use[x][y][cnt]) {
return;
}
use[x][y][cnt] = true;
cnt += grid[x][y] == '(' ? 1 : -1;
if (cnt < 0) {
return;
}
if (!bl) {
dfs(grid, x + 1, y, cnt);
}
if (!bl) {
dfs(grid, x, y + 1, cnt);
}
}
}
边栏推荐
- SSL universal domain name certificate
- Winter vacation parent-child tour, these new york attractions are not only fun but also knowledge
- [UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example
- 深度学习------不同方法实现Inception-10
- 基于servlet+jsp+mysql实现的工资管理系统【源码+数据库】
- Salary management system based on servlet+jsp+mysql [source code + database]
- Network layer protocol hardware
- Mongodb learning
- Unity enables simple music visualization
- 小C的数组(array)
猜你喜欢
![Salary management system based on servlet+jsp+mysql [source code + database]](/img/4a/6015cf17f4297691e97b48a5592fc5.png)
Salary management system based on servlet+jsp+mysql [source code + database]

【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV

Connect to the database and run node JS running database shows that the database is missing
![[FPGA] IIC读写EEPROM 的实现](/img/6a/36e9355058a90d98cffafcbaa1930b.png)
[FPGA] IIC读写EEPROM 的实现

Learning about signals

Bean创建流程 与 lazy-init 延迟加载机制原理

OneNote production schedule

Window10 jar double click to run without response

Redis implements SMS login function (II) redis implements login function

基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
随机推荐
Difference between request forwarding and redirection
Circle center technology, very anxious?
Enlist soldiers and generals, draw small programs, multi-threaded display time
史上最全的Redis基础+进阶项目实战总结笔记
圆心科技,很焦虑?
基于SSM框架茶叶商城系统【项目源码+数据库脚本+报告】
为什么win10开热点后电脑没有网络?
Qos(Quality of Service)
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
The role of break
Free travel recommendation in Bangkok: introduction to the Mekong River in Bangkok
What to do when the alicloud SSL certificate expires
Connect to the database and run node JS running database shows that the database is missing
brew安装nvm报nvm command not found解决方案
Fair lock and unfair lock
Input / output and interrupt technology -- microcomputer Chapter 6 learning notes
FortiGate firewall and Aruze cloud tunnel interruption
How to use div boxes to simulate line triangles
[UAV] kinematic analysis from single propeller to four rotor UAV
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example