当前位置:网站首页>Sum arrays with recursion
Sum arrays with recursion
2022-07-25 16:14:00 【GuochaoHN】
Make a note of zj Batch algorithm in advance :
Sum arrays with recursion :
The algorithm will be written immediately , But when running, it always reports stack overflow , I thought there was something wrong with my recursive logic , Has been changing ,
But it's still wrong , It finally got stuck … … The start is GG, I also panicked a lot , As a result, I didn't work out the second problem .
The initial code is as follows :
/** * @author guochao * @date 2022/7/22 */
public class Main {
public static void main(String[] args) {
int[] nums ={
1,2,3};
System.out.println("sum="+getSum(nums, 0));
}
public static int getSum(int[] nums, int i){
if(i >= nums.length) return 0;
int sum = getSum(nums, i++);
int result = nums[i] + sum;
return result;
}
}
result :
Because the interview is a whiteboard , unable debug, After the interview , I'm on my own idea Inside debug, Finally, I found the problem !!!
The problem is i++、i+1、++i.
My code says i++, But the correct way to write it is i+1.
After this lesson , I have a deeper understanding of the self increasing three Chinese Writing , I believe I won't be wrong again .
i+1: Do not change the original i Value , Unless mandatory assignment , such as :i = i + 1;
i++ and ++i Will change i Value , But the order of self increment is different ,i++ It is first used in autoincrement ,++i It is self increasing before using .
How is the use defined here ? I think it can be divided into two points intuitively :
1、 Direct assignment
eg:a = i++;
2、 The formal parameter assignment of the calling function
eg:function(++i);
May have a look , Here are three examples :
/** * @author guochao * @date 2022/7/22 */
public class test {
public static void main(String[] args) {
for(int i=0; i<5; i++){
aa(i++); // Notice the changes in this line !
}
}
public static void aa(int i){
System.out.print(i + " ");
}
}

/** * @author guochao * @date 2022/7/22 */
public class test {
public static void main(String[] args) {
for(int i=0; i<5; i++){
aa(++i); // Notice the changes in this line !
}
}
public static void aa(int i){
System.out.print(i + " ");
}
}

/** * @author guochao * @date 2022/7/22 */
public class test {
public static void main(String[] args) {
for(int i=0; i<5; i++){
aa(i+1); // Notice the changes in this line !
}
}
public static void aa(int i){
System.out.print(i + " ");
}
}

边栏推荐
- Zhaoqi Kechuang high-quality overseas returnee talent entrepreneurship and innovation service platform, online live broadcast Roadshow
- R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
- Ml image depth learning and convolution neural network
- Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
- MySQL显式锁
- 通用测试用例写作规范
- Exploration of 6-wire SPI transmission mode
- Talk about how to use redis to realize distributed locks?
- Recommended collection, which is probably the most comprehensive coding method summary of category type features
- Promise期约
猜你喜欢

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?

通用测试用例写作规范

leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具
![Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]](/img/03/54a2d82a17cd07374dc0aedacd7b11.png)
Leetcode:154. find the minimum value II in the rotation sort array [about the middle and rear positioning dichotomy of the rotation sort array]
![[Shakespeare: keep the fun of being a man]](/img/71/6476f2d58255c78ac8f58fbfc6a0c9.png)
[Shakespeare: keep the fun of being a man]

Win11自带画图软件怎么显示标尺?

Upgrade esxi6.7.0 to 7.0u3f (updated on July 12, 2022)

0x80131500打不开微软商店的解决办法
![[wechat applet] detailed explanation of applet host environment](/img/57/582c07f6e6443f9f139fb1af225ea4.png)
[wechat applet] detailed explanation of applet host environment
随机推荐
0x80131500打不开微软商店的解决办法
pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
阿唐的小帮手
Gap Locks(间隙锁)
mysql 表写锁
【图像隐藏】基于混合 DWT-HD-SVD 的数字图像水印方法技术附matlab代码
C# 模拟抽奖
Zhaoqi Kechuang high-quality overseas returnee talent entrepreneurship and innovation service platform, online live broadcast Roadshow
Endnote add Chinese gbt7714 style how to quote documents in word
MySQL教程66-数据表查询语句
Crazy God redis notes 12
Golang review summary
I interviewed 8 companies and got 5 offers in a week. Share my experience
MySQL全局锁
2w字详解数据湖:概念、特征、架构与案例
MySQL教程71-WHERE 条件查询数据
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
邮件的收发的展现逻辑之收件箱发件箱以及回复断链的问题
02. 将参数props限制在一个类型的列表中
Sword finger offer | number of 1 in binary