当前位置:网站首页>LootCode动态数组练习(724,118,766)
LootCode动态数组练习(724,118,766)
2022-07-27 16:08:00 【zh_Tnis】
LeetCode724:
这是一个返回索引的问题,并且是该索引对应元素左边所有元素之和等于右边所有元素之和,该元素不能参与运算。
思路:
1、先对该数组判断是否为一个空数组,空的话直接返回-1。
2、定义一个左边元素和的符号left和右边元素和的符号right,还有元素总和的符号sum。
3、遍历获得元素总和值sum的最后结果。
4、再次遍历,使元素开始从第一个相加进left中。这里有个关键想法,就是返回索引的位置是不相加进去的。所以需要从第二个元素的索引开始。
5、每次左边加完元素后,记得右边元素等于总和sum减去(左边元素和对应索引元素),然后判断左右元素是否相等,相等返回索引即可了。
class Solution {
public int pivotIndex(int[] nums) {
if(nums==null){ //1
return -1;
}
int left=0; //2
int right=0;
int sum=0;
for(int num:nums){ //3
sum+=num;
}
for(int i=0;i<nums.length;i++){ //4
if(i==0){
left=0;
}else{
left+=nums[i-1];
}
right=sum-left-nums[i]; //5
if(left==right){
return i;
}
}
return -1;
}
}LeetCode118:
这是一道关于杨辉三角的数组题目。出题人给定一个行数,让我们输出那么多行数的杨辉三角的结果,就是每个数对应上边两个数相加之和。比如给了五行那么输出下边这样的结果。我们可以这么想,每一行相当于一个线性表,每得到一行数后,也就是一个线性表,将该线性表当成一个元素继续加入一个线性表中。
输入: 5
输出:
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
思路:
1、根据该图可以知道每行第一个和最后一个元素都为1,其他元素为上方临近两个元素的和。并且每行元素存几个和现在为多少行有关。
2、List<Integer>是线性表中存一个整形类,List<List<Integer>>是线性表中存一个线性表。
3、创建一个和传入行相同的行列数组,用来存储元素便于放入线性表中。
4、使用一个for循环,I从0~行数-1,每次都创建一个线性表变量l,因为每行存多少个元素和现在为多少行有关,所以再定义一个for循环,j从0~i,每行第一个和最后一个元素等于1,其他元素等于上一行第j个位置和j-1位置的和。看图就知道了。
5、每行的元素存好后,将该线性表l,也就是存在变量l中的元素放入新的一个线性表就完成了。list.add(l)。
class Solution {
public List<List<Integer>> generate(int numRows) {
ArrayList<List<Integer>> list = new ArrayList<List<Integer>>();
int[][] arr = new int[numRows][numRows];
for(int i = 0;i<numRows;i++) {
ArrayList<Integer> l = new ArrayList<Integer>();
for(int j = 0;j<=i;j++) {
if(j == 0 || j == i) {
arr[i][j] = 1;
}else {
arr[i][j] = arr[i - 1][j] + arr[i - 1][j - 1];
}
l.add(arr[i][j]);
}
list.add(l);
}
return list;
}
}LeetCode766:
这是一道矩阵题目,要求给定一个矩阵,只有正对角线的所有元素相等那么返回true,不相等返回false。
下边是这道题的一个思路图。

思路:
1、由这张图可以知道,有两个路径,一个是matrix[0][0]位置向右走,另一个是向下走。向左是列数col移动,向下是行数row移动。每次都要判断正对角线上的元素是否相等,其实每个元素都是行列各加1。
待补:
边栏推荐
- Bug records using distributed framework WCF
- [introduction to database system (Wang Shan)] Chapter 4 - Database Security
- jpa连接数据库password字段BLOB
- [learning notes] Lombok's @builder annotation
- org.apache.catalina.core.StandardContext.startInternal Context [] startup failed due to previous err
- MySQL adds users and grants query only permission
- 1542. 找出最长的超赞子字符串 哈希+状态压缩
- Salesforce certified sharing and visibility Designer (su20) certification examination summary
- Golang Chan implements mutual exclusion
- 小米CC9 Pro拆解:后置五摄成本是骁龙855数倍!
猜你喜欢

【Codeforces】 A. Computer Game

Knowledge dry goods: basic storage service novice Experience Camp

Resolve merge fields in salesforce

浅论分布式训练中的recompute机制

Original direct selling MOS tube knl42150 2.8a/1500v applicable photovoltaic inverter can provide samples

Machine learning: IOU of concept understanding

同心向前,Google Play 十周年啦!

vue使用keep-alive实现页面缓存
![[MCU] 2.3 CPU of AT89S52](/img/4c/7b9d235bf8a919339d75a7ec95789f.png)
[MCU] 2.3 CPU of AT89S52

Convolutional neural network -- SSD thesis translation
随机推荐
Know things by learning | build a real-time anti plug-in mechanism from 0 to 1 to supplement the offensive and defensive power of mobile games in multiple dimensions
[Southwest University] information sharing of postgraduate entrance examination and re examination
Local development using LWC in salesforce
[user article] examples of P4 consolidation practice guide disassemble resolve
多线程实现循环
Salesforce runs all test classes and gets coverage reports
【学习笔记】热点账户问题的解决方案
In the fourth week of July, Yidun business risk control focused on the supreme law to regulate app's forcible request for personal information
The latest advanced interview questions for big factories are necessary
Learn from what you know | Yidun self-developed text real-time clustering technology, and wipe out the same kind of harmful content in social networks
Personal understanding of convolution calculation process of convolution neural network
Exciting collection of new features released by salesforce
【学习笔记】数据库中锁的分类
Prevent SQL injection
[MCU] 2.3 CPU of AT89S52
Golang Chan implements mutual exclusion
Golang worker pool
荣耀、小米发双十一战报:都称自己是冠军
面试常见问题一二
hutool- 数字计算