当前位置:网站首页>用递归进行数组求和
用递归进行数组求和
2022-07-25 15:56:00 【GuochaoHN】
记录一下zj提前批算法一面:
用递归进行数组求和:
算法马上就写出来了,但是运行的时候一直报栈溢出,我以为是我的递归逻辑出问题了,就一直在改,
但是还是报错,最终卡住了… … 开局就GG,自己也慌得一批,导致我第二道题也没做出来。
最初写的代码如下:
/** * @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;
}
}
结果:
由于面试的时候是白板,无法debug,面试结束后,我自己在idea里面debug,最终才发现了问题!!!
问题出在i++、i+1、++i。
我的代码中写的是i++,但是正确写法是i+1。
经过这次教训,我对于自增的三中写法有了更深刻的认识,相信下一次不会再错了。
i+1:不改变原来i的值,除非强制赋值,比如:i = i + 1;
i++和++i都会改变i的值,但是自增的顺序不同,i++是先使用在自增,++i是先自增再使用。
这里使用是如何定义的呢?我觉得直观来说可以分为两点:
1、直接赋值
eg:a = i++;
2、调用函数的形参赋值
eg:function(++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++); // 注意这行的改变!
}
}
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); // 注意这行的改变!
}
}
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); // 注意这行的改变!
}
}
public static void aa(int i){
System.out.print(i + " ");
}
}

边栏推荐
- Visual studio 2022 view class diagram
- 物理防火墙是什么?有什么作用?
- 「数字安全」警惕 NFT的七大骗局
- 权限管理-角色分配菜单
- 墨天轮高分技术文档分享——数据库安全篇(共48个)
- Exclusive lock
- Mysql读写锁
- I interviewed 8 companies and got 5 offers in a week. Share my experience
- July 25th, 2022 Daily: Microsoft proposed CodeT: a new SOTA for code generation, with 20 points of performance improvement
- 华为2023届提前批预热开始!左 神的程序代码面试指南终派上用场
猜你喜欢

Alibaba's internal "100 billion level concurrent system architecture design notes" are all inclusive, too comprehensive

Circulaindicator component, which makes the indicator style more diversified

Pytoch learning notes -- Summary of common functions 3

Product upgrade observation station in June

Introduction to redis

Waterfall flow layout
![[Shakespeare: keep the fun of being a man]](/img/71/6476f2d58255c78ac8f58fbfc6a0c9.png)
[Shakespeare: keep the fun of being a man]

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

How matlab saves all the data after running

General test case writing specification
随机推荐
Data system partition design - Request Routing
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
Matlab -- CVX optimization kit installation
Data system partition design - partition and secondary index
百奥赛图与LiberoThera共同开发全人GPCR抗体药物取得里程碑式进展
墨天轮高分技术文档分享——数据库安全篇(共48个)
Pytoch learning notes -- seresnet50 construction
"Digital security" alert NFT's seven Scams
【莎士比亚:保持做人的乐趣】
MySQL全局锁
Storage structure of cross linked list
mysql 隔离级别事务
Pytoch learning notes advanced_ CNN (using perception_module) implements MNIST dataset classification - (comments and results)
[JS advanced] JS regular correlation functions and regular objects_ 02
Activity review | July 6 Anyuan AI X machine heart series lecture No. 2 | MIT professor Max tegmark shares "symbiotic evolution of human and AI"
Reasons for data format conversion when matlab reads the displayed image
How matlab produces random complex sequences
面试8家公司,1周拿了5个offer,分享一下自己的心得
MySQL check whether the table is locked
Shared lock