zoukankan      html  css  js  c++  java
  • LeetCode Combinations

    Given two integers n and k, return all possible combinations of k numbers out of 1,2,...,n.

    import java.util.ArrayList;
    import java.util.List;
    
    class Solution {
        public List<List<Integer>> combine(int n, int k) {
            return combine(1, n + 1, k);
        }
    
        public List<List<Integer>> combine(int low, int upper, int k) {
            List<List<Integer>> result = new ArrayList<List<Integer>>();
            if (k == 1) {
                for (int i = low; i < upper; i++) {
                    List<Integer> r = new ArrayList<Integer>();
                    r.add(i);
                    result.add(r);
                }
                return result;
            }
            for (int i = low; i < upper; i++) {
                List<List<Integer>> r = combine(i + 1, upper, k - 1);
                for (List<Integer> a : r) {
                    a.add(0, i);
                }
                result.addAll(r);
            }
            return result;
        }
    
        public static void main(String[] args) {
            Solution s = new Solution();
            List<List<Integer>> lists = s.combine(4, 2);
            for(List res :lists){
                System.out.println(res);
            }
        }
    };

     题目二:给定一个数组arr[]和一个整数k,在数组中选择元素组成一个长度为K的数组,打印最终的子数组

    Example:arr={6,34,5,65}, K=2;

    最终子数组:{6,34},{6,5},{6,65},{34,5},{34,65},{5,65}

    参考代码:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Main {
        public static void main(String[] args) {
            int[] arr = {6, 34, 5, 65};
            int k = 5;
            // List<List<Integer>> lists = combine(1, 5, 2);
            List<List<Integer>> lists = combine2(arr, 0, arr.length, 2);
            for (List res : lists) {
                System.out.println(res);
            }
        }
    
        public static List<List<Integer>> combine2(int[] arr, int low, int upper, int k) {
    
            List<List<Integer>> res = new ArrayList<List<Integer>>();
    
            if (k == 1) {
                for (int i = low; i < upper; i++) {
                    List<Integer> list = new ArrayList<>();
                    list.add(arr[i]);
                    res.add(list);
                }
                return res;
            }
    
            for (int i = low; i < upper; i++) {
                List<List<Integer>> r = combine2(arr, i + 1, upper, k - 1);
                for (List<Integer> l : r) {
                    l.add(0, arr[i]);
                }
                res.addAll(r);
            }
            return res;
        }
    
    }
  • 相关阅读:
    FFmpeg入门,简单播放器
    Linux系统编译Win32版本adb
    检测目标程序ELF bit是32还是64
    Swift编程资料全集
    Swift编程资料总结
    cocos2d-html5学习之三-为sprite添加触摸事件
    Cocos2d-html5学习笔记二
    cocos2d-x学习笔记一
    NSViewAnimation进行视图和窗口动画
    Cocoa中NSAnimation动画简介
  • 原文地址:https://www.cnblogs.com/googlemeoften/p/5850063.html
Copyright © 2011-2022 走看看