当前位置:网站首页>LeetCode240+312+394
LeetCode240+312+394
2022-08-01 06:43:00 【想进阿里的小菜鸡】
思路
从每行的最后一个元素开始判断是向下进行还是向左进行。
代码
class Solution {
public boolean searchMatrix(int[][] matrix, int target) {
int row = 0;
int col = matrix[0].length-1;
while(row<matrix.length && col >=0){
if(matrix[row][col] == target){
return true;
}else if(matrix[row][col]<target){
row++;
}else{
col--;
}
}
return false;
}
}
思路
本题关键就是把某个位置的气球当做最后戳破的气球。先将其左右两边的气球都戳破再戳当前位置的气球。
1.定义dp数组
dp[i][j]:表示从i下标开始到j下标结束,戳破气球后的最大值。
2.初始化dp数组
dp[i][i]需要初始化,其他的不需要初始化,dp[i][i]就是长度为1的时候的取值。因为要从下标为0的元素开始计算,dp[0][0]计算不方便,所以就用新的数组newNums来代替新的数组。在旧的数组的基础上,在其头尾都增加两个值为1的元素。然后让i从1开始初试化dp数组。
3.确定递推公式
要让最后一个戳破的气球的下标是k,然后从i->j进行遍历,每次遍历的结果都和之前的结果比较取最大值。
res[i][j] = Math.max(res[i][j],newNums[i-1]*newNums[k]*newNums[j+1]+res[i][k-1]+res[k+1][j]);
4.确定遍历顺序
按照正序遍历即可。
5.打印dp数组
dp[1][nums.length]就是最后的结果。因为我们重新重建了一个数组newNums,所以数组头尾下标各自+1了。
代码
class Solution {
public int maxCoins(int[] nums) {
int[] newNums = new int[nums.length+2];
for(int i =1;i<=nums.length;i++){
newNums[i]=nums[i-1];
}
newNums[0] = 1;
newNums[nums.length+1]=1;
int[][] res = new int[nums.length+2][nums.length+2];
for(int i = 1;i<=nums.length;i++){
res[i][i]=newNums[i-1]*newNums[i]*newNums[i+1];
}
for(int length = 2;length<=nums.length;length++){
for(int i = 1;i+length-1<=nums.length;i++){
int j = i+length-1;
for(int k = i;k<=j;k++){
res[i][j] = Math.max(res[i][j],newNums[i-1]*newNums[k]*newNums[j+1]+res[i][k-1]+res[k+1][j]);
}
}
}
return res[1][nums.length];
}
}
思路
因为该题可以括号里面嵌套括号,所以需要使用栈来实现,又因为有数字和字符,所以需要用两个栈来实现。一个用来放数字一个用来放字符。然后遍历字符串进行判断。
如果是数字就计算数字的值放入对应的栈中,如果是字符就把字符拼接起来,遇到‘[’再放入栈中;如果是‘]’,就将字符栈中的字符取出来和当前字符拼接,得到新的字符串。
代码
class Solution {
public String decodeString(String s) {
if(s == null && s.length() == 0){
return s;
}
Stack<Integer> count = new Stack<>();
Stack<String> stringStack = new Stack<>();
String res = "";
int index = 0;
while(index < s.length()){
if(Character.isDigit(s.charAt(index))){
int temp = 0;
while(Character.isDigit(s.charAt(index))){
temp = temp*10+(s.charAt(index)-'0');
index++;
}
count.push(temp);
}else if(s.charAt(index) == '['){
stringStack.push(res);
res = "";
index++;
}else if(s.charAt(index) == ']'){
StringBuilder tempString = new StringBuilder(stringStack.pop());
int time =count.pop();
for(int i = 0;i<time;i++){
tempString.append(res);
}
index++;
res = tempString.toString();
}else{
res+=s.charAt(index);
index++;
}
}
return res;
}
}
边栏推荐
猜你喜欢
阿里三面:MQ 消息丢失、重复、积压问题,该如何解决?
first unique character in characters
Hunan institute of technology in 2022 ACM training sixth week antithesis
Zero-code website development tool: WordPress
NIO编程
Dbeaver connect the MySQL database and error Connection refusedconnect processing
Detailed explanation of the crawler framework Scrapy
MySQL row locks and gap locks
Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)
WebSocket实现聊天功能
随机推荐
企业员工人事管理系统(数据库课设)
The Bean's life cycle
七、MFC序列化机制和序列化类对象
Detailed explanation of the crawler framework Scrapy
NUMPY
【音视频】srs直播平台搭建
从零开始—仿牛客网讨论社区项目(一)
导致锁表的原因及解决方法
Information system project managers must recite the work of the core test site (56) Configuration Control Board (CCB)
Guest brush SQL - 2
The BP neural network based on MATLAB voice characteristic signal classification
Create, modify and delete tables
Bean的生命周期
奇葩问题 npm install 报错 gyp ERR
【视觉SLAM十四讲】第一章理论详解
Dart exception details
用位运算为你的程序加速
Classwork (7) - #598. remainder operation (mod)
特别数的和
2022.7.26 模拟赛