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 ...
随机推荐
- ubuntu常用系统命令
安装升级 查看软件xxx安装内容 dpkg -L xxx 查找软件库中的软件 apt-cache search 正则表达式 或 aptitude search 软件包 显示系统安装包的统计信息 apt ...
- ajax提交文件file 单个文件上传
转载: https://blog.csdn.net/u012867699/article/details/78357401
- python解决excel工作薄合并处理(openpyxl处理excel2010以上版本)
前段时间使用xlrd.xlwt对文件进行处理(https://www.cnblogs.com/pinpin/p/10287491.html),但是只能处理excel2010以下版本,所以又写了个处理e ...
- java动态数组笔记
动态数组: 在java.lang.reflect包下提供了Array类,包括一系列static方法,通过这些方法可动态的创建数组.给元素赋值.取出元素值等等 //理解数组引用——下面定义的objs数组 ...
- 信息领域热词分析系统--java爬取CSDN中文章标题即链接
package zuoye1; import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.SQLExce ...
- java——注解处理器
Java提供了两种方式来处理注解:第一种是利用运行时反射机制:另一种是使用Java提供的API来处理编译期的注解. 运行时通过反射:仅当定义的注解的@Retention为RUNTIME时,才能够通过运 ...
- git使用笔记-比较分支差异
比如我们有 2 个分支:master, dev,现在想查看这两个 branch 的区别,有以下几种方式: 1.查看 dev 有,而 master 中没有的: git log dev ^master 同 ...
- [转]JavaScriptSerializer中日期序列化
本文转自:http://www.cnblogs.com/songxingzhu/p/3816309.html 直接进入主题: class Student { public int age { get; ...
- linux下Python2.7编译安装PyQt5
---作者吴疆,未经允许,严禁转载,违权必究--- ---欢迎指正,需要源码和文件可站内私信联系--- -----------点击此处链接至博客园原文----------- 功能说明:在ubuntu系 ...
- 【转】Android 中的 Service 全面总结
1.Service的种类 按运行地点分类: 类别 区别 优点 缺点 应用 本地服务(Local) 该服务依附在主进程上, 服务依附在主进程上而不是独立的进程,这样在一定程度上节约了资源,另 ...