当前位置:网站首页>LeetCode240+312+394
LeetCode240+312+394
2022-08-01 07:02:00 【Want to enter Ali's little chicken】
Thoughts
Start from the last element of each line to determine whether to go down or to the left.
Code
class Solution {public boolean searchMatrix(int[][] matrix, int target) {int row = 0;int col = matrix[0].length-1;while(row=0){if(matrix[row][col] == target){return true;}else if(matrix[row][col] Thoughts
The key to this question is to treat the balloon at a certain position as the last balloon to burst.First puncture the balloons on the left and right sides of it, and then puncture the balloon at the current position.
1. Define dp array
dp[i][j]: Indicates the maximum value after the balloon is burst from the start of the i subscript to the end of the j subscript.
2. Initialize the dp array
dp[i][i] needs to be initialized, others do not need to be initialized, dp[i][i] is the value when the length is 1.Because the calculation starts from the element with the subscript 0, the calculation of dp[0][0] is inconvenient, so the new array newNums is used instead of the new array.On the basis of the old array, two elements with a value of 1 are added to both the head and the tail.Then let i initialize the dp array starting from 1.
3. Determine the recursive formula
Let the subscript of the last punctured balloon be k, and then traverse from i->j. The result of each traversal is compared with the previous result and the maximum value is obtained.
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. Determine the traversal order
It can be traversed in positive order.
5. Print dp array
dp[1][nums.length] is the final result.Because we rebuilt an array newNums, the subscripts of the head and tail of the array are +1 respectively.
Code
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];}}Thoughts
Because this question can be nested within parentheses, it needs to use a stack to implement it, and because there are numbers and characters, it needs to use two stacks to implement it.One for numbers and one for characters.Then traverse the string to judge.
If it is a number, calculate the value of the number and put it into the corresponding stack. If it is a character, splicing the characters together, and when '[' is encountered, put it into the stack; if it is ']', put the character in the stack.The character is taken out and spliced with the current character to get a new string.
Code
class Solution {public String decodeString(String s) {if(s == null && s.length() == 0){return s;}Stack count = new Stack<>();Stack 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 边栏推荐
- 问下 mysql向pg同步多个表的话 有什么好的方案吗?
- 如何使用Photoshop合成星轨照片,夜空星轨照片后期处理方法
- 戴尔PowerEdge服务器R450 RAID配置步骤
- Golang:go模版引擎的使用
- 我三本学历,五面阿里,被面试官“供”着出来了,拿了33*15的Offer
- Dell PowerEdge Server R450 RAID Configuration Steps
- Create, modify and delete tables
- dbeaver连接MySQL数据库及错误Connection refusedconnect处理
- 湖仓一体电商项目(一):项目背景和架构介绍
- 响应式织梦模板园林景观类网站
猜你喜欢

Offer刷题——1

matlab simulink 粒子群优化模糊pid控制的电机泵

Sound Signal Processing Fundamental Frequency Detection and Time-Frequency Analysis

仿牛客网讨论社区项目—项目总结及项目常见面试题

曲柄滑块机构运动分析和参数优化

Three aspects of Ali: How to solve the problem of MQ message loss, duplication and backlog?

Dell PowerEdge Server R450 RAID Configuration Steps

插入排序—直接插入排序和希尔排序

"By sharing" northwestern university life service | | bytes a second interview on three sides by HR

Vim简介
随机推荐
CSP-S2019兴奋不已
【一句话攻略】彻底理解JS中的回调(Callback)函数
爬虫框架 Scrapy 详解
图像基本操作的其他内容
curl (7) Failed connect to localhost8080; Connection refused
LeetCode Question of the Day (309. Best Time to Buy and Sell Stock with Cooldown)
R语言使用gt包和gtExtras包优雅地、漂亮地显示表格数据:gtExtras包的pad_fn函数与gt::fmt函数一起用于填充包含数值的特定列、对数据列的数值进行十进制对齐(从小数点对齐)
2022杭电多校第二场1011 DOS Card(线段树)
从零开始—仿牛客网讨论社区项目(一)
Dart 异常详解
NIO programming
MySQL row locks and gap locks
mysql中添加字段的相关问题
【FiddlerScript】利用FiddlerScript抓包保利威下载
爆肝3万字,最硬核丨Mysql 知识体系、命令全集 【建议收藏 】
小白的0基础教程SQL: 关系数据库概述 02
LeetCode 0149. Maximum number of points on a line
Golang:go获取url和表单属性值
表的创建、修改与删除
湖仓一体电商项目(一):项目背景和架构介绍