鱼C论坛

 找回密码
 立即注册
查看: 2393|回复: 1

[学习笔记] leetcode 22. Generate Parentheses 谷歌面试题

[复制链接]
发表于 2019-8-17 02:18:14 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Seawolf 于 2019-8-17 14:21 编辑

这是一道Google的面试题

  1. Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses.

  2. For example, given n = 3, a solution set is:

  3. [
  4.   "((()))",
  5.   "(()())",
  6.   "(())()",
  7.   "()(())",
  8.   "()()()"
  9. ]
复制代码


首先想到的是用hashset 来解决这个问题

  1. import java.util.*;

  2. public class Solution {
  3.        
  4.         HashSet<String> solutionHashSet;
  5.        
  6.         public List<String> generateParenthesis(int n){
  7.                
  8.                 solutionHashSet = new HashSet<>();
  9.                
  10.                 generateParenthesis("", n, n);
  11.                
  12.                 Object[] o = solutionHashSet.toArray();
  13.                 List solutionSet = new ArrayList();
  14.                
  15.                 for(int i = 0; i< o.length; i++) {
  16.                        
  17.                         solutionSet.add(o[i]);
  18.                 }
  19.                
  20.                 return solutionSet;
  21.         }
  22.        
  23.         public void generateParenthesis(String s, int o, int c) {
  24.                
  25.                 if(o == 0 && c == 0) {
  26.                        
  27.                         solutionHashSet.add(s);
  28.                         return;
  29.                 }
  30.                
  31.                 if(o == 0) {
  32.                        
  33.                         generateParenthesis(s+ ")", o , c-1);
  34.                 }
  35.                
  36.                 if( c< 0 || o < 0 || o > c) {
  37.                        
  38.                         return;
  39.                 }
  40.                
  41.                 if(c == 0) {
  42.                        
  43.                         generateParenthesis(s+ "(", o -1 , c);
  44.                 }
  45.                
  46.                 if(o < c) {
  47.                        
  48.                         generateParenthesis(s+ "(", o -1 , c);
  49.                         generateParenthesis(s+ ")", o , c-1);
  50.                 }
  51.                
  52.         }

  53. }

复制代码


但是这个code的efficiency 太差了,需要改进

68357654_718373608633570_1757655094611935232_n.png

  1. import java.util.*;

  2. class Solution {

  3.    
  4.     public void dfs(int left, int right, String str, ArrayList array) {
  5.            
  6.             if(left == 0 && right == 0) {
  7.                    
  8.                     array.add(str);
  9.                 return;
  10.             }
  11.             
  12.             if(left < right) {
  13.                    
  14.                     dfs(left, right-1, str + ")",array);
  15.             }
  16.             
  17.             if(left >0) {
  18.                    
  19.                     dfs(left-1,right,str+"(",array);
  20.             }
  21.             
  22.         }

  23.     public ArrayList<String> generateParenthesis(int n) {
  24.         
  25.         ArrayList<String> result = new ArrayList<String>();
  26.         
  27.         String re = "";
  28.         
  29.         dfs(n,n,re,result);
  30.         
  31.         return result;
  32.         
  33.     }
  34. }
复制代码


修改以后的efficiency 上升的不少。

67953911_1255243104657424_1671800974697562112_n.png

评分

参与人数 1荣誉 +2 鱼币 +3 贡献 +3 收起 理由
小甲鱼 + 2 + 3 + 3 鱼C有你更精彩^_^

查看全部评分

本帖被以下淘专辑推荐:

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-8-17 08:15:21 From FishC Mobile | 显示全部楼层
学习一下,补充知识
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-5-13 18:23

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表