本篇內(nèi)容介紹了“Java怎么生成括號”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價比魯山網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式魯山網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋魯山地區(qū)。費用合理售后完善,10余年實體公司更值得信賴。
Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n = 3, a solution set is: [ "((()))", "(()())", "(())()", "()(())", "()()()" ]
public class Solution { public ListgenerateParenthesis(int n) { ArrayList result = new ArrayList (); dfs(result, "", n, n); return result; } /* left and right represents the remaining number of ( and ) that need to be added. When left > right, there are more ")" placed than "(". Such cases are wrong and the method stops. */ public void dfs(ArrayList result, String s, int left, int right){ if(left > right) //因為從left開始減一,所以left不可能小于right return; if(left==0&&right==0){ result.add(s); return; } if(left>0){ dfs(result, s+"(", left-1, right); // } if(right>0){ dfs(result, s+")", left, right-1); } } }
“Java怎么生成括號”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!