backtracking and invariant during generating the parathese

righjt > left  (open bracket and cloase barckst)

class Solution {
//["((()))","(()())","(())()","()(())","()()()","())(()"] wrong case --> change right > left the numebr of bracket is the invariant
List<String> res = new ArrayList<>();
public List<String> generateParenthesis(int n) {
//back((new StringBuilder()).append('('),2*n, 1, n, n);
back((new StringBuilder()), 2*n, 0, n, n);
return res;
}
void back(StringBuilder temp, int n, int pos, int left, int right){//pos start from 1
if(pos >= n){
//temp.append(")"); // problem from here
System.out.println(pos);
res.add(temp.toString());
return;
}
if(left > 0 ){
temp.append("(");
back(temp,n, pos+1, left-1, right);
temp.setLength(temp.length()-1);
}
if(right > left ){
temp.append(")");
back(temp, n, pos+1, left, right-1);
temp.setLength(temp.length()-1);
} }
}

Restore IP Addresses

//insert element into the string

class Solution {
//invariant rule: each number are
// use the immuniateble of String
List<String> res = new ArrayList<String>();
public List<String> restoreIpAddresses(String s) {
back(0, s, new String(), 0);
return res;
} void back(int next, String s, String str , int num){ //num: there are only three dots.
if(num == 3){
//if(next==s.length()) return;
if(!valid(s.substring(next, s.length()))) return;
res.add(str+s.substring(next, s.length()));
return;
}
//for each step, move one digit or two or three
for(int i = 1; i <=3; i++ ){
//check string
if(next+i > s.length()) continue;
String sub = s.substring(next, next+i);//
if(valid(sub)){
back(next+i, s, str+sub+'.', num+1);
}
}
}
boolean valid(String sub){
if(sub.length() == 0 || sub.length()>=4) return false;
if(sub.charAt(0) == '0') {
//System.out.println(sub.equals("0"));
return sub.equals("0"); // not check '0' weired
}
int num = Integer.parseInt(sub);
if(num >255 || num <0) return false;
else return true;
}
}

//core idea: move one step or 2 step or three based on the question (0 - 255) also append . and substring

use string instead stringBuilder  (immuatable)

131. Palindrome Partitioning

class Solution {
//check palindrome, divide into small problems:
List<List<String>> res = new ArrayList<List<String>>();
public List<List<String>> partition(String s) {
back(s, new ArrayList<String>());
return res;
}
void back(String s, List<String> list){
if(s.length()==0){
List<String> temp = new ArrayList<>(list);
res.add(temp);
return ;
} for(int i = 0; i<s.length(); i++){//divide s into su and sub
String su = s.substring(0, i+1);
String sub = s.substring(i+1, s.length());
if(isPalindrome(su)){
list.add(su);
back(sub,list);
list.remove(list.size()-1);
} }
}
boolean isPalindrome(String su){
if(su.length()==0){
return true;
}else {
int i =0 , j = su.length()-1;
while(i<j){
if(su.charAt(i) != su.charAt(j)) return false;
i++; j--;
}
return true;
}
}
}

Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning的更多相关文章

  1. [LeetCode] 22. Generate Parentheses 生成括号

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

  2. leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)

    https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...

  3. LeetCode 22. Generate Parentheses

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

  4. LeetCode(93) Restore IP Addresses

    题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...

  5. Java [leetcode 22]Generate Parentheses

    题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...

  6. LeetCode之“字符串”:Restore IP Addresses

    题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...

  7. [leetcode]22. Generate Parentheses生成括号

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

  8. [leetcode.com]算法题目 - Restore IP Addresses

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  9. 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]

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

随机推荐

  1. IIS Express被局域网访问

    在 文件夹 C:\Users\administrator\Documents\IISExpress\config 下面 applicationhost.config 文件里 找到相应的项目 如 < ...

  2. Mysql5.7.20源码编译安装

    一.下载源码包 1.1 下载mysql源码包 MySQL源码,网址为:https://dev.mysql.com/downloads/mysql/ : 1.2 下载boost 下载网址为:http:/ ...

  3. Quartz中Cron详解

    Quartz中的cron跟Linux系统的cron定义不太一样(Linux从分开始) 特殊字符: * 用来表示包含一个范围内的任意值. 例如, 分钟位置的“*” 表示 “每分钟”. ?  当不特定指代 ...

  4. IDEA中如何添加jar包

    File -> Project Structure -> Libraries -> + -> jar -> 选择classes

  5. 查询多列得到map与查询得到po对象

    import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; i ...

  6. 服务器报nginx: [warn] conflicting server name "blog.xueyi.com" on 0.0.0.0:80, ignored

    nginx: [warn] conflicting server name "blog.xueyi.com" on 0.0.0.0:80, ignored 修改nginx配置参数后 ...

  7. U盘中病毒了

    往U盘里拷东西的时候突然发现一个后缀名为exe图标却是文件夹的图标的文件.大概二三百K 按类型排序之后发现好几个这样的文件,大小都是一模一样的,名字分别跟我U盘里原先的文件夹对应,原本的文件夹都被设置 ...

  8. from表单,图片预览,和表单提交

    <form> <input id="file" class="topsub-file" type="file" name= ...

  9. ES6数组新增方法总结

    关于数组中forEach() .map().filter().reduce().some().every()的总结 let arr = [1, 2, 3, 4, 5] // forEach遍历数组 a ...

  10. Zookeeper---初识

    1.Zookeeper是  Apache开源  的 分布式应用程序   服务治理: 在分布式环境中 协调和管理服务 是一个复杂的过程: ZooKeeper通过其简单的架构和API解决了这个问题: Zo ...