public class Main4 { static Integer res = Integer.MAX_VALUE; public static void main(String[] args) { int[] n = {1,2,3,4,6,2,5,6}; ArrayList<Integer> list = new ArrayList<>(); f(n,0,0,0); System.out.println(res); } private static void f(int[] n, int idx, int tmp, int tmpr){ if(tmp == n[n.length - 1] && tmpr < res){ res = tmpr; } if(tmp > n[n.length - 1] || idx == n.length - 2){ return; } if(idx + 1 < n.length - 1){ f(n,idx + 1, tmp, tmpr); f(n,idx + 1, tmp += n[idx], tmpr + 1); } } }
点赞 评论

相关推荐

牛客网
牛客企业服务