304. 二维区域和检索 - 矩阵不可变
题目
给定一个二维矩阵,计算其子矩形范围内元素的总和,该子矩阵的左上角为 (row1, col1) ,右下角为 (row2, col2)。
上图子矩阵左上角 (row1, col1) = (2, 1) ,右下角(row2, col2) = (4, 3),该子矩形内元素的总和为 8。
示例:
给定 matrix = [
[3, 0, 1, 4, 2],
[5, 6, 3, 2, 1],
[1, 2, 0, 1, 5],
[4, 1, 0, 1, 7],
[1, 0, 3, 0, 5]
]
sumRegion(2, 1, 4, 3) -> 8
sumRegion(1, 1, 2, 2) -> 11
sumRegion(1, 2, 2, 4) -> 12
code
class NumMatrix {
int[][] dp;
int m;int n;
public NumMatrix(int[][] matrix) {
sumRegion(matrix);
}
public int sumRegion(int r1, int c1, int r2, int c2) {
return dp[r2+1][c2+1]+dp[r1][c1]-dp[r1][c2+1]-dp[r2+1][c1];
}
public int sumRegion(int[][] matrix){
m=matrix.length;
if(m==0) return 0;
n=matrix[0].length;
dp=new int[m+1][n+1];
for(int i=1;i<=m;i++){
for(int j=1;j<=n;j++){
dp[i][j]=dp[i-1][j]+dp[i][j-1]-dp[i-1][j-1]+matrix[i-1][j-1];
}
}
return 1;
}
}
/**
* Your NumMatrix object will be instantiated and called as such:
* NumMatrix obj = new NumMatrix(matrix);
* int param_1 = obj.sumRegion(row1,col1,row2,col2);
*/
572. 另一个树的子树
题目
给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树。s 的一个子树包括 s 的一个节点和这个节点的所有子孙。s 也可以看做它自身的一棵子树。
示例 1:
给定的树 s:
3
/ \
4 5
/
1 2
给定的树 t:
4
/
1 2
代码
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubtree(TreeNode s, TreeNode t) {
if(s==null && t==null) return true;
if(s==null || t==null) return false;
if(s.val==t.val && isSub(s,t)) return true;
return isSubtree(s.left,t) || isSubtree(s.right,t);
}
public boolean isSub(TreeNode s,TreeNode t){
if(s==null && t==null) return true;
if(s==null || t==null) return false;
if(s.val!=t.val) return false;
return isSub(s.left,t.left) && isSub(s.right,t.right);
}
}
1286. 字母组合迭代器
题目
请你设计一个迭代器类,包括以下内容:
一个构造函数,输入参数包括:一个 有序且字符唯一 的字符串 characters(该字符串只包含小写英文字母)和一个数字 combinationLength 。
函数 next() ,按 字典序 返回长度为 combinationLength 的下一个字母组合。
函数 hasNext() ,只有存在长度为 combinationLength 的下一个字母组合时,才返回 True;否则,返回 False。
示例:
CombinationIterator iterator = new CombinationIterator("abc", 2); // 创建迭代器 iterator
iterator.next(); // 返回 "ab"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "ac"
iterator.hasNext(); // 返回 true
iterator.next(); // 返回 "bc"
iterator.hasNext(); // 返回 false
code
class CombinationIterator {
List<String> list;
int k=0;
public CombinationIterator(String characters, int combinationLength) {
list=new ArrayList<>();
boolean[] visited=new boolean[characters.length()];
getList(characters,combinationLength,0,visited,"");
}
public String next() {
String res=list.get(k);
k++;
return res;
}
public boolean hasNext() {
return k<list.size();
}
public void getList(String characters,int combinationLength,int index,boolean[] visited,String cur){
if(combinationLength==cur.length()){
list.add(cur);
return;
}
for(int i=index;i<characters.length();i++){
if(visited[i]) continue;
visited[i]=true;
getList(characters,combinationLength,i+1,visited,cur+characters.charAt(i));
visited[i]=false;
}
}
}
/**
* Your CombinationIterator object will be instantiated and called as such:
* CombinationIterator obj = new CombinationIterator(characters, combinationLength);
* String param_1 = obj.next();
* boolean param_2 = obj.hasNext();
*/