Java for LeetCode 077 Combinations

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

For example,
If n = 4 and k = 2, a solution is:

[
  [2,4],
  [3,4],
  [2,3],
  [1,2],
  [1,3],
  [1,4],
]

解题思路:

DFS,JAVA实现如下:

	public static void main(String args[]) {
		List<List<Integer>> list = combine(5, 2);
		System.out.println(list.size());
		for (List<Integer> alist : list)
			System.out.println(alist + "*");
	}

	static public List<List<Integer>> combine(int n, int k) {
		List<List<Integer>> list = new ArrayList<List<Integer>>();
		if (n <= 0 || k > n)
			return list;
		dfs(list, n, k, 0);
		return list;
	}

	static List<Integer> alist = new ArrayList<Integer>();

	static void dfs(List<List<Integer>> list, int n, int k, int depth) {
		if (depth >= k) {
			list.add(new ArrayList<Integer>(alist));
			return;
		}
		if (depth == 0)
			for (int i = 1; i <= n - k + 1; i++) {
				alist.add(i);
				dfs(list, n, k, depth + 1);
				alist.remove(alist.size() - 1);
			}
		else
			for (int i = 1; i <= n - alist.get(alist.size() - 1) + k - depth - 1; i++) {
				alist.add(alist.get(alist.size() - 1) + i);
				dfs(list, n, k, depth + 1);
				alist.remove(alist.size() - 1);
			}
	}
时间: 2024-08-10 19:15:43

Java for LeetCode 077 Combinations的相关文章

Java for LeetCode 216 Combination Sum III

Find all possible combinations of k numbers that add up to a number n, given that only numbers from 1 to 9 can be used and each combination should be a unique set of numbers. Ensure that numbers within the set are sorted in ascending order. Example 1

LeetCode: Letter Combinations of a Phone Number [018]

[题目] Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

077 Combinations

077 Combinations 这道题纯dfs, 但是在 Line 13处做了个剪枝 从84ms 提高到68 ms 1 class Solution: 2 def __init__(self): 3 self.ans = [] 4 5 def combine(self, n, k): 6 self.help(n, k, 0, []) 7 return self.ans 8 9 def help(self, n, k, l, cur): 10 if k == 0: 11 self.ans.app

Java for LeetCode 128 Longest Consecutive Sequence

Given an unsorted array of integers, find the length of the longest consecutive elements sequence. For example, Given [100, 4, 200, 1, 3, 2], The longest consecutive elements sequence is [1, 2, 3, 4]. Return its length: 4. Your algorithm should run i

Java for LeetCode 098 Validate Binary Search Tree

Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as follows: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys

Java for LeetCode 057 Insert Interval

Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and merge

Java for LeetCode 059 Spiral Matrix II

Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For example, Given n = 3, You should return the following matrix: [ [ 1, 2, 3 ], [ 8, 9, 4 ], [ 7, 6, 5 ]] 解题思路: 参考Java for LeetCode 054 Spiral Matrix,修改下

Java for LeetCode 107 Binary Tree Level Order Traversal II

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order trave

Java for LeetCode 108 Convert Sorted Array to Binary Search Tree

Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题思路: 首先要理解,什么叫做height balanced BST Java for LeetCode 110 Balanced Binary Tree,然后就十分容易了,JAVA实现如下: public TreeNode sortedArrayToBST(int[] nums) { return