当前位置:网站首页>Lotcode dynamic array exercise (724118766)
Lotcode dynamic array exercise (724118766)
2022-07-27 18:22:00 【zh_ Tnis】
LeetCode724:
This is a problem of returning the index , And the sum of all elements on the left of the corresponding element of the index is equal to the sum of all elements on the right , This element cannot participate in the operation .
Ideas :
1、 First, judge whether the array is an empty array , If it is empty, it will return directly to -1.
2、 Define a symbol for the left element and left And the symbol of the right element and right, And the symbol of the sum of elements sum.
3、 Traverse to get the sum value of elements sum The final result of .
4、 Traverse... Again , Start adding elements from the first phase left in . Here's a key idea , That is, the position of the return index is not added . So you need to start with the index of the second element .
5、 After adding elements on the left every time , Remember that the elements on the right are equal to the sum sum subtract ( The left element and the corresponding index element ), Then judge whether the left and right elements are equal , Equal return index .
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:
This is an array topic about Yang Hui triangle . The author gives a number of lines , Let's output the result of Yang Hui triangle with so many lines , It is the sum of the two numbers above each number . For example, if you give five lines, output the following results . We can think so , Each row is equivalent to a linear table , After each row number , That is, a linear table , Continue to add the linear table as an element into a linear table .
Input : 5
Output :
[
[1],
[1,1],
[1,2,1],
[1,3,3,1],
[1,4,6,4,1]
]
Ideas :
1、 According to the figure, we can know that the first and last elements of each line are 1, Other elements are the sum of two adjacent elements above . And the number of elements in each line depends on how many lines it is now .
2、List<Integer> Is an integer class stored in a linear table ,List<List<Integer>> Is a linear table stored in a linear table .
3、 Create a row and column array that is the same as the incoming row , Used to store elements so that they can be put into a linear table .
4、 Use one for loop ,I from 0~ Row number -1, Create a linear table variable every time l, Because how many elements are stored in each line depends on how many lines it is now , So define another for loop ,j from 0~i, The first and last elements of each line are equal to 1, Other elements are equal to line j Positions and j-1 Position and . Just look at the picture .
5、 After saving the elements of each line , Put this linear table l, That is, there are variables l Put the elements in a new linear table .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:
This is a matrix topic , A matrix is required , If all the elements of the positive diagonal are equal, it returns true, Unequal return false.
Below is a thought map of this problem .

Ideas :
1、 From this picture, we can know , There are two paths , One is matrix[0][0] Go right , The other is going down . To the left is the number of columns col Move , Down is the number of rows row Move . Every time we have to judge whether the elements on the diagonal are equal , In fact, each element is added in rows and columns 1.
await a vacancy or job opening :
边栏推荐
- Bubble sorting in JS
- 宣布收购文晔30%股份,大联大意欲何为?
- Glory and Xiaomi reported on the double 11: they all called themselves champions
- @Datetimeformat received less than minutes and seconds, conversion times type exception
- Resolve merge fields in salesforce
- The first PCIe 5.0 SSD master of Jiangsu Huacun: TSMC 12NM process, mass production in 2020
- Deep learning: GCN case
- 解决Reids不能被其他IP访问
- 深度学习:GCN案例
- fragmentTransaction.replace第二个参数报错
猜你喜欢
![[learning notes] the implementation principle of the ordered set Zset in redis - skip table](/img/c6/5d9f48fce1dc1c78b8c7dbcf046fc7.png)
[learning notes] the implementation principle of the ordered set Zset in redis - skip table

Localization within Communities

超实用!阿里P9私藏的Kubernetes学习笔记,看完直呼NB

What every Salesforce developer should know about Dates and Times in Apex

解决Reids不能被其他IP访问

Six relationships of classes -- the difference between dependency and Association

快速获取网站媒体资源方法

深度学习:STGCN学习笔记

CFA exam registration instructions

1. opencv图片基础操作
随机推荐
动态链表3队列的链式存储结构(LinkedQueue实现)
【学习笔记】热点账户问题的解决方案
1. opencv图片基础操作
Deep learning: installation package records
Introduction to ef framework
快速获取网站媒体资源方法
"Who is Huawei" documentary film series landing on BBC: exposing a large number of Ren Zhengfei's unknown experience
fragmentTransaction.replace第二个参数报错
请教大神一个问题 flinkcdc,同步mysql中的datetime字段会变为时间戳 有人遇到过吗
深度学习:安装包记录
Here comes the first 5g SOC of MediaTek! A77+g77+apu3.0, officially released on November 26!
深度学习:GCN图分类案例
What every Salesforce developer should know about Dates and Times in Apex
Multi thread import data and generate error files for redis storage
NVIDIA released the world's smallest edge AI supercomputing: 21tops computing power, power consumption is only 10W!
[MIT 6.S081] Lec 8: Page faults 笔记
美团二面:为什么Redis会有哨兵?
动态链表4单向循环链表(LoopSingle实现)
[MIT 6.S081] Lec 4: Page tables 笔记
Super practical! After reading the kubernetes study notes hidden by Alibaba P9, call NB directly