当前位置:网站首页>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 + " ");
}
}

边栏推荐
- Win11桌面切换快捷键是什么?Win11快速切换桌面的方法
- "Digital security" alert NFT's seven Scams
- 泰雷兹推出解决方案,助力SAP客户控制云端数据
- MySQL isolation level transactions
- MySQL全局锁
- [zeloengine] summary of pit filling of reflection system
- MySQL页锁
- pymongo保存dataframe格式的数据(insert_one, insert_many, 多线程保存)
- mysql 表写锁
- Ice 100g network card fragment message hash problem
猜你喜欢

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

"Digital security" alert NFT's seven Scams

# JWT 图解

Implementation of recommendation system collaborative filtering in spark

【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码

Talk about how to use redis to realize distributed locks?

MQTT X CLI 正式发布:强大易用的 MQTT 5.0 命令行工具

Ml image depth learning and convolution neural network

Recommended collection, which is probably the most comprehensive coding method summary of category type features

EMQX Cloud 更新:日志分析增加更多参数,监控运维更省心
随机推荐
MySQL check whether the table is locked
0x80131500打不开微软商店的解决办法
【故障诊断】基于贝叶斯优化支持向量机的轴承故障诊断附matlab代码
权限管理-角色分配菜单
MySQL教程67-使用DISTINCT过滤重复数据
Win11自带画图软件怎么显示标尺?
MySQL隐式锁
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
今天睡眠质量记录84分
"Digital security" alert NFT's seven Scams
MyBaits
Boomi荣获“多元化最佳首席执行官奖”和“职业成长最佳公司奖”,在大型公司类别中跻身50强
How matlab produces random complex sequences
MySQL metadata lock (MDL)
MySQL global lock
墨天轮高分技术文档分享——数据库安全篇(共48个)
权限管理-删除菜单(递归)
R语言使用gt包和gtExtras包漂亮地显示表格数据:gt_bar_plot函数和gt_plt_bar_pct函数可视化百分比条形图、原始数据的百分比条形、缩放后的数据的百分比条形、指定数据对齐宽度
MySQL table read lock
Upgrade esxi6.7.0 to 7.0u3f (updated on July 12, 2022)