Title 22, brackets generation

I, entitled 1

Here Insert Picture Description

Second, the idea

First, the algorithm, after obtaining the results of removing incorrect, to get the final solution. After all, not the best, or the best time to get the results directly to avoid incorrect.

Then say the following three different ideas algorithms.

Which method 1 and method 2, the idea is similar, but when iteration, each cycle of the unit are not the same, Unit 1 is half brackets law, law is the entire unit 3 brackets, but not to such a point as to cause whole different results. F3 plus iteration loop, doomed to run out.

F2 is to get a bunch of strings, which determine eligibility.

Third, the code

import java.util.ArrayList;
import java.util.List;

public class T0022 {

    public static void main(String[] args) {
        System.out.println( generateParenthesis(3) );
    }
	//法1
    //来自大佬的最优解
    private static List<String> result = new ArrayList<String>();
    public static void generate(String item,int left,int right ) {
        //左括号和右括号满足上述条件的前提下都为n个,添加这个答案
        if(left == 0 && right == 0) {
            result.add(item);
            return;
        }
        //左括号的个数小于n 才能继续放左括号
        if(left > 0) {
            generate(item+"(",left-1,right);
        }
        //左括号个数必须大于右括号的放置个数 才能继续放右括号
        if(left < right) {
            generate(item+")",left,right-1);
        }
    }
    public static List<String> generateParenthesis(int n) {
        /**
         *左括号个数必须大于右括号的放置个数 才能继续放右括号
         *左括号的个数小于n 才能继续放左括号
         *左括号和右括号满足上述条件的前提下都为n个,添加这个答案
         */
        List<String> res = new ArrayList<>();
        generate("",n,n);
        return result;
    }

/*
	//法2
    //非最优解,但是勉强通过
    public static List<String> generateParenthesis(int n) {

        List<String> result = new ArrayList<>();

        //在通过循环得到不重复的长度为2n的二进制10串,和括号串对应,之后判断是否是合格的括号串
        for (int i = (int)Math.pow(2,2*n-1); i < Math.pow(2,2*n); i+=2 ){
            //将十进制的i转化为二进制的串,之后通过replaceAll方法转化为括号串
            String tmp = Integer.toString(i, 2).replaceAll("1", "(").replaceAll("0", ")");

            //通过判断将合法的存储在结果集中
            if ( isValid(tmp) )
                result.add( tmp );
        }

        return result;
    }

    //根据之前的一道题更改的判断算法,判断输入的括号串是否是符合规定的
    public static boolean isValid(String s) {

        int stoke = 0;
        for ( char i : s.toCharArray() ){
            if ( i == '(' ){
                stoke++;
            }else {
                if (stoke == 0 )
                    return false;

                if ( i == ')'  ){
                    stoke--;
                }else
                    return false;
            }
        }
        return stoke==0;
    }
*/

/*
 	//法3
    //此法超时
    private static List<String> result = new ArrayList<String>();
    public static List<String> generateParenthesis(int n) {

        //以“()”为一个单位,进行循环,最后对每一个得到的结果进行判断是否重复
        donext( "()", n-1 );
        return result;
    }

    public static void donext(String str, int len){        
        //在迭代结束后
        if ( len == 0 ){
            //如果在结果集中不重复就将其添加在结果集中
            if ( !result.contains(str) )
                result.add(str);
        //还可以继续进行循环   
        }else{
            
            //进行循环,将“()”插入不同的位置,之后进行下一次的迭代
            for( int i = 0; i < str.length(); i++ ){
                String tmp = str.substring(0, i )+"()"+str.substring(i);
                donext( tmp, len-1 );
            }
        }
    }
*/
}

  1. Source: stay button (LeetCode)
    link: https: //leetcode-cn.com/problems/generate-parentheses
    copyrighted by deduction from all networks. Commercial reprint please contact the authorized official, non-commercial reprint please indicate the source. ↩︎

Published 48 original articles · won praise 1 · views 844

Guess you like

Origin blog.csdn.net/weixin_45980031/article/details/104278447