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

边栏推荐
- leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
- Basic usage of MFC thread afxbeginthread, passing multiple parameters
- MySQL global lock
- 通用测试用例写作规范
- 今天去 OPPO 面试,被问麻了
- MySQL 元数据锁(MDL)
- 意向锁(Intention Lock)
- Win11动态磁贴没了?Win11中恢复动态磁贴的方法
- PageHelper.startPage没有生效问题
- Experimental reproduction of image classification (reasoning only) based on caffe resnet-50 network
猜你喜欢

狂神redis笔记12

Food safety - do you really understand the ubiquitous frozen food?

哪个led显示屏厂家更好

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

Talk about how to use redis to realize distributed locks?
![[server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage](/img/90/51d86111b918eb60761818110cdec4.jpg)
[server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
![[Shakespeare: keep the fun of being a man]](/img/71/6476f2d58255c78ac8f58fbfc6a0c9.png)
[Shakespeare: keep the fun of being a man]

I interviewed 8 companies and got 5 offers in a week. Share my experience

优必选大型仿人服务机器人Walker X的核心技术突破
随机推荐
R语言ggplot2可视化线图(line)、自定义配置标题文本相关内容颜色和图例(legend)颜色相匹配(和分组线图的颜色相匹配、match colors of groups)
Shared lock
02. Limit the parameter props to a list of types
没错,请求DNS服务器还可以使用UDP协议
JWT diagram
Intention lock
Pagehelper.startpage is not effective
MySQL乐观锁
The annualized interest rate of treasury bonds is too low. Is there a financial product with a higher annualized interest rate than the reverse repurchase of treasury bonds?
[JS advanced] JS regular correlation functions and regular objects_ 02
MySQL table write lock
Zhaoqi Kechuang high-quality overseas returnee talent entrepreneurship and innovation service platform, online live broadcast Roadshow
如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?
Boomi荣获“多元化最佳首席执行官奖”和“职业成长最佳公司奖”,在大型公司类别中跻身50强
MySQL tutorial 71-where conditional query data
可验证随机函数 VRF
Product upgrade observation station in June
MATLAB optimization tool manopt installation
Typescript learning 2 - Interface
权限管理-角色分配菜单