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 ...
随机推荐
- POJ:2976 Dropping tests(二分+最大化平均值)
Description In a certain course, you take n tests. If you get ai out of bi questions correct on test ...
- 上传文件插件dropzone的实例
html: <div class="field"> <div id="file" class="dropzone"> ...
- lnmp 架构
Mysql安装 tar zxf mysql-boost-5.7.17.tar.gz yum install -y gcc gcc-c++ yum install cmake-2.8.12.2-4.el ...
- 随性练习:excel中文字和链接存到html文件
这是一个简单的练习,主要是将excel中文字和链接存到html文件中,并且可通过点击文字直通链接 excel格式如下图示,我这里得excel是07版的,所以用到xlrd模块 代码: import xl ...
- Mysql 游标的定义与使用方式
创建游标: 首先在MySql中创建一张数据表: CREATE TABLE IF NOT EXISTS `store` ( `id` int(11) NOT NULL AUTO_INCREMENT, ...
- rails 里js 在production 只合并不压缩等问题,以及assets pipeline 加载js 在指定页面上
因为刚学rails,试着做了一个小系统操作微信公共帐号, 之后部署的时候遇见了一个问题,整套系统在互联网端访问,非常的慢,而在手机端访问,10s后才会有响应, 打开chrome的调试工具,发现appl ...
- swing线程机制
在介绍swing线程机制之前,先介绍一些背景概念. 背景概念 同步与异步: 同步是指程序在发起请求后开始处理事件并等待处理的结果或等待请求执行完毕,在此之前程序被阻塞(block)直到请求完成 ...
- Linux大文件快速处理小方法
背景 工作中使用MapReduce任务导出一批含有路径的文件,共计行数300W+,需要检测文件是否在对应的服务器中存在,而文件所在的服务器并非hadoop集群的服务器,因此打算采用bash脚本进行.具 ...
- 修改mysql表的字符集
ALTER TABLE logtest CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci; 修改数据库字符集: 代码如下: ALTER DAT ...
- .NET面试题2
常见面试题目: 1. 值类型和引用类型的区别? 2. 结构和类的区别? 3. delegate是引用类型还是值类型?enum.int[]和string呢? 4. 堆和栈的区别? 5. 什么情况下会在堆 ...