当前位置:网站首页>用递归进行数组求和
用递归进行数组求和
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 + " ");
}
}

边栏推荐
- Redis distributed lock, it's really impossible without it
- ML - Speech - Introduction to speech processing
- MySQL explicit lock
- Zhaoqi Kechuang high-quality overseas returnee talent entrepreneurship and innovation service platform, online live broadcast Roadshow
- 百奥赛图与LiberoThera共同开发全人GPCR抗体药物取得里程碑式进展
- 意向锁(Intention Lock)
- 电阻电路的等效变化(Ⅱ)
- 一文入门Redis
- mysql 表读锁
- [server data recovery] data recovery cases of raid information loss caused by unexpected power failure of HP EVA server storage
猜你喜欢

# JWT 图解

Equivalent change of resistance circuit (Ⅱ)

tkinter模块高级操作(一)—— 透明按钮、透明文本框、自定义按钮及自定义文本框

Understand "average load"
![[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training](/img/d4/bcc577f320a893c7006177993b2e7a.png)
[IJCAI 2022] parameter efficient large model sparse training method, which greatly reduces the resources required for sparse training
![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]

面试8家公司,1周拿了5个offer,分享一下自己的心得

Circulaindicator component, which makes the indicator style more diversified

Crazy God redis notes 12

推荐收藏,这或许是最全的类别型特征的编码方法总结
随机推荐
Alibaba's internal "100 billion level concurrent system architecture design notes" are all inclusive, too comprehensive
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源
MySQL教程67-使用DISTINCT过滤重复数据
面试突击:为什么 TCP 需要 3 次握手?
General test case writing specification
食品安全丨无处不在的冷冻食品,你真的了解吗?
Zhaoqi Kechuang high-level innovation and Entrepreneurship Talent Service Platform at home and abroad, mass entrepreneurship and innovation achievement transformation platform
ML - Speech - traditional speech model
MySQL table read lock
Geogle colab notes 1-- run the.Py file on the cloud hard disk of Geogle
ServletConfig 类和ServletContext 类
行云管家V6.5.1/2/3系列版本发布:数据库OpenAPI能力持续强化
权限管理-角色分配菜单
Gap Locks(间隙锁)
Equivalent change of resistance circuit (Ⅱ)
Beyond compare 4 realizes class file comparison [latest]
华为2023届提前批预热开始!左 神的程序代码面试指南终派上用场
leetcode:528. 按权重随机选择【普通随机失效 + 要用前缀和二分】
Wavelet transform --dwt2 and wavedec2
Why is preparestatement better and safer?