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;
}
}
}

最新文章

  1. Servlet3.0的注解
  2. Android Studio导入项目慢的问题
  3. webkit浏览器css设置滚动条
  4. c中的函数
  5. [转载] google mock CheatSheet
  6. 安装package.js
  7. 2016/09/21 Java关键字final
  8. HW6.3
  9. cocos2dx lua学习笔记 &amp;lt;一&amp;gt; quick 3.5定义本身C++类是必然lua
  10. HttpClient(联网)
  11. 卡尔曼滤波(Kalman Filter)
  12. web框架之Flask
  13. 什么是vue
  14. 全志A33编译环境搭建
  15. robot_framework环境搭建
  16. JAVA字符串的常见处理和操作
  17. win 8.1 Your PC needs to be repaired修复过程
  18. 今天才知道原来我还没弄清楚js中全局变量和局部变量的定义...
  19. 往github提交代码流程
  20. maven私服 Nexus2.x.x私服安装配置

热门文章

  1. Photoshop在网页设计中的应用与方法
  2. NFS网络储存系统
  3. day 007 深浅拷贝
  4. 数据层——ImageData层
  5. my19_mysql 多线程备份恢复工具mydumper
  6. 分治法 - Divide and Conquer
  7. 转 使用SwingBench 对Oracle RAC DB性能 压力测试
  8. java多线程-创建线程
  9. Spring Boot 实现ErrorController接口处理404、500等错误页面
  10. vue router路由(三)