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 ...
随机推荐
- LINUX下用PHPIZE安装PHP GD扩展
环境:LNMP in centOS 6.4. linux下PHP的扩展可以用phpize的方法,比较简单地进行启用. 以下以PHP-GD2 库安装为例子. sudo yum install php-g ...
- redis安装及应用
Redis安装及主从配置 server2,3,4. 安装 tar zxf redis-4.0.1.tar.gz cd redis-4.0.1 yum install -y gcc make make ...
- Spring Cloud 监控相关
因为最近客户提出想监控Spring Cloud运行状况的需求,所以稍稍做了调研.目前了解的方法如下: Eureka Server启动后可以在根目录路径看到所有注册的Eureka Client状况 各个 ...
- python3 continue和break 区别
for i in range(10): if i==5: continue #跳出当次循环 if i==8: break #跳出整个for循环 print(i)
- Java StringTokenzier
Java中substring方法可以分解字符串,返回的是原字符串的一个子字符串.如果要讲一个字符串分解为一个一个的单词或者标记,StringTokenizer可以帮你. public static v ...
- java——线程的wait()和notify()
这是一个关于生产者和消费者的线程通信的例子: package thread_test; public class PCThread { public static void main(String[] ...
- 用navigator.geolocation.getCurrentPosition在IOS10以上的系统无法定位
昨天老板告诉我代码有Bug(定位失败),于是各种测试最终发现IOS10以上版本手机不能成功(穷,买不起iphone,测试不完全),先贴失败代码: var city =""; nav ...
- android Activity启动过程(二)从ActivityManagerService的startActivity到栈顶Activity的onPause过程
ActivityManagerService.startActivity() ActvityiManagerService.startActivityAsUser() ActivityStackSup ...
- RTT设备与驱动之PIN设备
单片机的PIN有2个基本功能:GPIO和AFIO,其中gpio的常用功能: 1 输入:上拉.下拉.模拟.浮动 2 输出:上拉.下拉.推挽.开漏 3 中断:上升沿.下降沿.双沿.高电平.低电平触发 RT ...
- GridLayout(网格布局)
常用属性: 排列对齐: ①设置组件的排列方式: android:orientation="" vertical(竖直,默认)或者horizontal(水平) ②设置组件的 ...