Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning
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的更多相关文章
- [LeetCode] 22. Generate Parentheses 生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- leetcode@ [22]Generate Parentheses (递归 + 卡特兰数)
https://leetcode.com/problems/generate-parentheses/ Given n pairs of parentheses, write a function t ...
- LeetCode 22. Generate Parentheses
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- LeetCode(93) Restore IP Addresses
题目 Given a string containing only digits, restore it by returning all possible valid IP address comb ...
- Java [leetcode 22]Generate Parentheses
题目描述: Given n pairs of parentheses, write a function to generate all combinations of well-formed par ...
- LeetCode之“字符串”:Restore IP Addresses
题目链接 题目要求: Given a string containing only digits, restore it by returning all possible valid IP addr ...
- [leetcode]22. Generate Parentheses生成括号
Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...
- [leetcode.com]算法题目 - Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combina ...
- 蜗牛慢慢爬 LeetCode 22. Generate Parentheses [Difficulty: Medium]
题目 Given n pairs of parentheses, write a function to generate all combinations of well-formed parent ...
随机推荐
- codeforces 985C Liebig's Barrels(贪心)
题目 题意: 有n * k块木板,每个木桶由k木板组成,每个木桶的容量定义为它最短的那块木板的长度. 任意两个木桶的容量v1,v2,满足|v1-v2| <= d. 问n个木桶容量的最大的和为多少 ...
- day 007 深浅拷贝
今日内容: 1.字符串操作补充: join # 遍历列表 例: lst = ['汪峰','吴君如','章子怡'] s = '*-/@'.join(lst) prints(s) 结果为汪峰*-/@吴君如 ...
- python3 zip()函数笔记
a=[1,2,3]b=[4,5,6] for A ,B in zip(a,b):#用zip()函数整体打包 print(A,B)
- java——为什么要有接口?和抽象类有什么不一样?
1.接口不是类,为什么? 接口如果是类,那就失去了java引入接口的意义了. java之所以引入接口,就是为了弥补不能多继承的缺点,在java中每个类只能有一个超类,但却可以实现多个接口. 2.接口可 ...
- ORA-12012 ORA-20001 on ORACLE 12C (2420581.1)
Oracle数据库 - 企业版 - 12.2.0.1及更高版本本文档中的信息适用于任何平台. Doc ID 2420581.1 症状 在容器数据库中,警报日志中会显示以下错误: ORA-12012 ...
- Boxes in a Line UVA - 12657 (双向链表)
题目链接:https://vjudge.net/problem/UVA-12657 题目大意:输入n,m 代表有n个盒子 每个盒子最开始按1~n排成一行 m个操作, 1 x y :把盒子x放到y ...
- Unity Scene Screen.resolutions 分辨率列表
Screen.resolutions 分辨率列表(安卓平台试了不能用此方法,最好用宏定义判断一下平台) C# => public static Resolution[] resolutions; ...
- C# IO流 File.Exists,Directory.Exists, File.Create,Directory.CreateDirectory
void Start() { CreateDirectory(); CreateFile(); } //平台的路径(封装起来的一个属性,这不是一个方法) public string path { ge ...
- 性能测试工具LoadRunner04-LR之浏览器打不开
环境:win7+lr11 IE浏览器要在9以下,9以上lr11是调不起来的 火狐浏览器,我用的是28,最新版本的也调不起来 可以调起浏览器但没有事件? 1.把INTERNET高级设置中的“启用第三方浏 ...
- C++计算二叉树的节点数和高度
用struct结构体的写法: /* * description: 计算二叉树的层数和节点数 * writeby: nick * date: 2012-10-23 16:16 * */ #include ...