摘要:用的思想,加上題目的特殊意思,來解決問題。如果個數(shù)字,有種結(jié)果。這里對原題多加了一個輸出的要求,代碼如下。也是為了去重,兩個分解的解法是對稱的,乘法對稱的重點就是
用combination的思想,加上題目的特殊意思,來解決問題。
526 Beautiful Arrangement
Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position (1 ≤ i ≤ N) in this array: 1.The number at the ith position is divisible by i. 2.i is divisible by the number at the ith position. 如果3個數(shù)字,有3種結(jié)果。 [1, 2, 3] [2, 1, 3] [3, 2, 1] 3 這里對Leetcode原題多加了一個輸出的要求,代碼如下。
public Solution{ int count = 0; public int countArrangement(int N) { if (N == 0) return 0; helper(N, 1, new int[N + 1], new ArrayList()); return count; } private void helper(int N, int pos, int[] used, List list) { if (pos > N) { count++; System.out.println(list.toString()); return; } for (int i = 1; i <= N; i++) { if (used[i] == 0 && (i % pos == 0 || pos % i == 0)) { used[i] = 1; list.add(i); helper(N, pos + 1, used, list); list.remove(list.size()-1); used[i] = 0; } } } }
254 Factor Combinations
比如給出的數(shù)字是12,質(zhì)因數(shù)分解的結(jié)果如下: [ [2, 6], [2, 2, 3], [3, 4] ]
public class Solution { public List> getFactors(int n) { List
> res = new ArrayList
>(); if(n <= 3) return res; dfs(n, res, new ArrayList
(), -1); return res; } public void dfs(int n, List > res, List
path, int lower){ // 和一般combination不同的是,每一步都要導(dǎo)入到結(jié)果中。 if(lower != -1){ path.add(n); res.add(new ArrayList<>(path)); path.remove(path.size()-1); } // lower 是為了解決重復(fù)分解的問題,比如12 = 2*2*3 = 3*2*2,但是后一個是重復(fù)的, 所以分解之后factor不能變小。 // upper 也是為了去重,12 = 3*4 = 4*3, 兩個分解的解法是對稱的,乘法對稱的重點就是sqrt(n). int upper = (int) Math.sqrt(n); for(int i=Math.max(2, lower); i<= upper; i++){ if(n%i == 0){ path.add(i); dfs(n/i, res, path, i); path.remove(path.size()-1); } } } }
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/66950.html
找出string里的單詞。 186. Reverse Words in a String II, 434. Number of Segments in a String combination類型題 77. Combinations 39. Combination Sum 40. Combination Sum II 216. Combination Sum III 494. Target S...
Problem Suppose you have N integers from 1 to N. We define a beautiful arrangement as an array that is constructed by these N numbers successfully if one of the following is true for the ith position ...
Problem Numbers can be regarded as product of its factors. For example, 8 = 2 x 2 x 2; = 2 x 4.Write a function that takes an integer n and return all possible combinations of its factors. Note: You ...
摘要:需求在里創(chuàng)建,然后通過代碼消費。創(chuàng)建一個新的基于這個標準的創(chuàng)建一個因為我是在當(dāng)前系統(tǒng)上的里調(diào)用當(dāng)前系統(tǒng)提供的,所以選擇當(dāng)然這個的也是需要在這個地方自己創(chuàng)建一個的可以維護成給該創(chuàng)建的維護的將該的下載到本地?;谇耙徊絼?chuàng)建的創(chuàng)建一個。 需求:在C4C UI里創(chuàng)建web service(maintain ticket),然后通過ABSL代碼消費。1. 創(chuàng)建一個新的Communication ...
閱讀 3615·2021-11-15 11:38
閱讀 2812·2021-11-11 16:55
閱讀 2565·2021-11-08 13:22
閱讀 2640·2021-11-02 14:45
閱讀 1324·2021-09-28 09:35
閱讀 2605·2021-09-10 10:50
閱讀 475·2019-08-30 15:44
閱讀 2787·2019-08-29 17:06