1 Star 1 Fork 2

wangjili/Algorithm

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
LeastNumExtend.java 846 Bytes
一键复制 编辑 原始数据 按行查看 历史
RosinaWang 提交于 2017-09-05 17:20 +08:00 . first commit
package codingInterviewGuide.part4dp;
/**
* Created by Dell on 2017-08-10.
*/
public class LeastNumExtend {
public static int getNum(int[] arr,int aim){
return voilence(arr,0,aim);
}
public static int voilence(int[] arr,int index,int aim){
if(aim==0){return 0;}
if(aim<0||index==arr.length){
return -1;
}
int count=0,temp1=0,temp2=0;
//有两种情况,分别递归
temp1=voilence(arr,index+1,aim);
temp2=voilence(arr,index+1,aim-arr[index]);
temp2=temp2==-1?temp2:temp2+1;
if(temp2!=-1&&temp1!=-1){
return Math.min(temp1,temp2);
}
//两者都等于-1,或其中一个等于-1
if(temp1==-1&&temp2==-1){
return -1;
}else{
return temp1==-1?temp2:temp1;
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/vuvu/Algorithm.git
git@gitee.com:vuvu/Algorithm.git
vuvu
Algorithm
Algorithm
master

搜索帮助