In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces LeetCode how to solve the combination problem, the article is very detailed, has a certain reference value, interested friends must read it!
Title
Given two integers n and k, return 1. The combination of all possible k numbers in n.
Example: input: n = 4, k = 2 output: [[2List temp 4], [3 int 4], [2jue 3], [1jue 2], [1d3], [1d4],] code class Solution {List temp = new ArrayList (); List ans = new ArrayList (); public List combine (int n, int k) {dfs (1, n, k); return ans } public void dfs (int cur, int n, int k) {/ / pruning: if the length of temp plus the length of interval [cur, n] is less than k, it is impossible to construct temp if (temp.size () + (n-cur + 1) < k) {return of length k. } / / record the legal answer if (temp.size () = = k) {ans.add (new ArrayList (temp)); return;} / / consider selecting the current location temp.add (cur); dfs (cur + 1, n, k); temp.remove (temp.size ()-1) / / consider not selecting the current location dfs (cur + 1, n, k);}}
The above is all the content of the article "how to solve the combination problem in LeetCode". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.