We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
组合问题
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. Example: Input: n = 4, k = 2 Output: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n.
Example:
Input: n = 4, k = 2 Output: [ [2,4], [3,4], [2,3], [1,2], [1,3], [1,4], ]
class Solution(object): def combine(self, n, k): # init first combination nums = list(range(1, k + 1)) + [n + 1] output, j = [], 0 while j < k: # add current combination output.append(nums[:k]) # increase first nums[j] by one # if nums[j] + 1 != nums[j + 1] j = 0 while j < k and nums[j + 1] == nums[j] + 1: nums[j] = j + 1 j += 1 nums[j] += 1 return output
The text was updated successfully, but these errors were encountered:
No branches or pull requests
组合问题
The text was updated successfully, but these errors were encountered: