LeetCode经典编程题(七)

1. palindrome-partitioning-ii

Description

Given a string s, partition s such that every substring of the partition is a palindrome.

Return the minimum cuts needed for a palindrome partitioning of s.

For example, given s =“aab”,
Return 1 since the palindrome partitioning[“aa”,“b”]could be produced using 1 cut.

Solution

Dynamic Plan:
dp[i] means the cut number that substring(0, i+1) needs.
for each palindrome string (j, i+1) [ j from 0 to i ]
dp[i] = Math.min (dp[j-1] + 1, dp[i])

Code
import java.util.*;
public class Solution {
    public int minCut(String s) {
        if(s==null||s.length()<=1){
            return 0;
        }
        int[] dp = new int[s.length()];
        dp[0] = 0;
        for(int i = 1; i < s.length(); i++){
            dp[i] = Integer.MAX_VALUE;
            for(int j = 0; j <= i; j++){
                if(isPalindrome(s.substring(j, i+1))){
                    if(j == 0){
                        dp[i] = 0;
                    }else{
                        dp[i] = Math.min(dp[i], dp[j-1]+1);
                    }
                }
            }
        }
        return dp[s.length()-1];
    }
    public boolean isPalindrome(String s){
        for(int i = 0; i < s.length()/2; i++){
            if(s.charAt(i)!=s.charAt(s.length() - i - 1)){
                return false;
            }
        }
        return true;
    }
}

2. palindrome-partitioning

Description

Given a string s, partition s such that every substring of the partition is a palindrome.

Return all possible palindrome partitioning of s.

For example, given s =“aab”,
Return

  [
    ["aa","b"],
    ["a","a","b"]
  ]
Solution

dfs: Use recursive method. If substring(0,i) is palindrome, then we get the partition of the left part of this string, and add it to result.

Code
import java.util.ArrayList;
public class Solution {
    public ArrayList<ArrayList<String>> partition(String s) {
        ArrayList<ArrayList<String>> result = new ArrayList<>();
        if(s==null||s.length()==0){
            return result;
        }
        if(s.length()==1){
            ArrayList<String> str = new ArrayList<>();
            str.add(s);
            result.add(str);
            return result;
        }else{
            for(int i = 1; i <= s.length(); i++){
                String sub = s.substring(0, i);
                if(isPalindrome(sub)){
                    if(i==s.length()){
                        ArrayList<String> str = new ArrayList<>();
                        str.add(sub);
                        result.add(str);
                    }else{
                        ArrayList<ArrayList<String>> leftpart 
                        = partition(s.substring(i));
                        for(int j = 0; j < leftpart.size(); j++){
                            leftpart.get(j).add(0, sub);
                            result.add(leftpart.get(j));
                        }
                    }
                }
            }
            return result;
        }
    }
    
    public boolean isPalindrome(String s){
        for(int i = 0; i < s.length()/2; i++){
            if(s.charAt(i)!=s.charAt(s.length() - i - 1)){
                return false;
            }
        }
        return true;
    }
}

3. surrounded-regions

Description

Given a 2D board containing’X’and’O’, capture all regions surrounded by’X’.
A region is captured by flipping all’O’s into’X’s in that surrounded region .

For example,

X X X X
X O O X
X X O X
X O X X

After running your function, the board should be:

X X X X
X X X X
X X X X
X O X X
Solution

dfs: The ‘O’ which is at the edge of the matrix, and the ‘O’ which is adjacent to it should not be changed. The left ‘O’ s need to be changed as ‘X’.

Code
public class Solution {
    private int cols;
    private int rows;
    public void solve(char[][] board) {
        if(board == null || board.length <=0 || board[0].length <=0){
            return;
        }
        cols = board[0].length;
        rows = board.length;
        for(int j = 0; j < cols; j++){
            dfs(board, 0, j);//first row
            dfs(board, rows-1, j);//last row
        }
        for(int i = 0; i < rows; i++){
            dfs(board, i, 0);
            dfs(board, i, cols-1);
        }
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
                if(board[i][j]=='O'){
                    board[i][j] = 'X';
                }
            }
        }
        for(int i = 0; i < rows; i++){
            for(int j = 0; j < cols; j++){
                if(board[i][j]=='-'){
                    board[i][j] = 'O';
                }
            }
        }
    }
    
    public void dfs(char[][] board, int i, int j){
        if(board[i][j] == 'O'){
            board[i][j] = '-';
            if(i + 1 < rows){
                dfs(board, i + 1, j);
            }
            if(i - 1 >= 0){
                dfs(board, i - 1, j);
            }
            if(j + 1 < cols){
                dfs(board, i, j + 1);
            }
            if(j - 1 >= 0){
                dfs(board, i, j - 1);
            }
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值