```class Solution {
public:
int StrToInt(string str) {
int n = str.size(), s = 1;
long long res = 0;
if(!n) return 0;
if(str[0] == '-') s = -1;
for(int i = (str[0] ==  '-' || str[0] == '+') ? 1 : 0; i < n; ++i){
if(!('0' <= str[i] && str[i] <= '9')) return 0;
res = (res << 1) + (res << 3) + (str[i] & 0xf);//res=res*10+str[i]-'0';
}
return res * s;
}
};```

res = (res << 1) + (res << 3) + (str[i] & 0xf);

str[i] & 0xf：针对字符0-9的，0-9的ascii码值为0x30，0x31，0x32 0x33 ...0x39，因此与0x0f按位与后只保留个位上的书即0x0，0x1，。。。0x9

113. Path Sum II

```public List<List<Integer>> pathSum(TreeNode root, int sum) {
List<List<Integer>> res = new ArrayList<>();
List<Integer> list = new ArrayList<>();
helper(res, list, root, sum);
return res;
}
private void helper(List<List<Integer>> res, List<Integer> list, TreeNode root, int sum) {
if (root == null) return;
if (root.left == null && root.right == null && root.val == sum) {
}
helper(res, list, root.left, sum - root.val);
helper(res, list, root.right, sum - root.val);
list.remove(list.size() - 1);
}```

# java List复制：浅拷贝与深拷贝

list.remove(list.size() - 1);

114. Flatten Binary Tree to Linked List

```private TreeNode prev = null;
public void flatten(TreeNode root) {
if (root == null)
return;
flatten(root.right);
flatten(root.left);
root.right = prev;
root.left = null;
prev = root;
}```

129. Sum Root to Leaf Numbers

```public int sumNumbers(TreeNode root) {
return sum(root, 0);
}

public int sum(TreeNode n, int s){
if (n == null) return 0;
if (n.right == null && n.left == null) return s*10 + n.val;
return sum(n.left, s*10 + n.val) + sum(n.right, s*10 + n.val);
}```

199. Binary Tree Right Side View

```public class Solution {
public List<Integer> rightSideView(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
rightView(root, result, 0);
return result;
}

public void rightView(TreeNode curr, List<Integer> result, int currDepth){
if(curr == null){
return;
}
if(currDepth == result.size()){
}

rightView(curr.right, result, currDepth + 1);
rightView(curr.left, result, currDepth + 1);

}
}```

332. Reconstruct Itinerary

```public List<String> findItinerary(String[][] tickets) {
Map<String, PriorityQueue<String>> targets = new HashMap<>();
for (String[] ticket : tickets)
Stack<String> stack = new Stack<>();
stack.push("JFK");
while (!stack.empty()) {
while (targets.containsKey(stack.peek()) && !targets.get(stack.peek()).isEmpty())
stack.push(targets.get(stack.peek()).poll());
//peek() 方法用于查找在此堆栈顶部的对象,无需从堆栈中取出。
//poll() 和 remove() 都是从队列中取出一个元素，但是 poll() 在获取元素失败的时候会返回空，但是 remove() 失败的时候会抛出异常。
}
return route;
}```

computeIfAbsent： 如果map里没有这个key，那么就按照后面的这个function添加对应的key和value

6ms：

```class Solution {

HashMap<String,PriorityQueue<String>> ticketsMap=new HashMap<>();

public List<String> findItinerary(String[][] tickets) {
if(tickets==null) return result;

//构造hashmap
for(String[] ticket:tickets){
if(!ticketsMap.containsKey(ticket[0])) ticketsMap.put(ticket[0],new PriorityQueue<>());
ticketsMap.get(ticket[0]).offer(ticket[1]);
}

DFS("JFK");
return result;
}

public void DFS(String ticket){
PriorityQueue<String> temp=ticketsMap.get(ticket);
while (temp!=null && !temp.isEmpty()){
DFS(temp.poll());
}
}
}```

101. Symmetric Tree

对称　　　　　　　　　　　　不对称

https://leetcode.com/problems/symmetric-tree/discuss/33089/My-C++-Accepted-code-in-16ms-with-iteration-solution

```public boolean isSymmetric(TreeNode root) {
if(root == null) return true;
while(q.size() > 1){
TreeNode left = q.poll(),
right = q.poll();
//如果都是空就继续
if(left== null&& right == null) continue;
//不全为空就返回false
if(left == null ^ right == null) return false;
if(left.val != right.val) return false;
}
return true;
}```

```public boolean isSymmetric(TreeNode root) {
if(root==null) return true;
return isMirror(root.left,root.right);
}
public boolean isMirror(TreeNode p, TreeNode q) {
if(p==null && q==null) return true;
if(p==null || q==null) return false;
return (p.val==q.val) && isMirror(p.left,q.right) && isMirror(p.right,q.left);
}```

1、在方法体内对参数进行运算，不会影响原有变量的值（基本类型不会改变值，引用类型不会改变引用地址）。

```public class ParamTest {

public static void integerParam(int a,int b){
a += 1;
b += 1;
}

public static void quoteParam(Random x){
x = new Random();
}

public static void main(String[] args) {
int a = 1;
int b = 2;
integerParam(1,2);
System.out.println("a:"+a);
System.out.println("b:"+b);

System.out.println("=======我是分割线======");

Random r = new Random();
System.out.println(r);
quoteParam(r);
System.out.println(r);
}
}  ```

b:2
=======我是分割线======
java.util.Random@5910e440

java.util.Random@5910e440

2、在方法体内对参数的属性进行操作，将改变原有变量的属性值（如集合、数组中的元素）

```public class ParamTest {

public static void integerParam(int a,int b){
a += 1;
b += 1;
}

public static void quoteParam(Random x){
x = new Random();
}

public static void arrayParam(String[] strArray){
strArray[0] = "a";
strArray[1] = "b";

}

public static void main(String[] args) {
int a = 1;
int b = 2;
integerParam(1,2);
System.out.println("a:"+a);
System.out.println("b:"+b);

System.out.println("=======我是分割线======");

Random r = new Random();
System.out.println(r);
quoteParam(r);
System.out.println(r);

System.out.println("========我是分割线=========");

String[] strArray = new String[2];
strArray[0] = "x";
System.out.println(strArray);
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
arrayParam(strArray);
System.out.println(strArray);
for (int i = 0; i < strArray.length; i++) {
System.out.println(strArray[i]);
}
}  ```

a:1
b:2
=======我是分割线======
java.util.Random@5910e440
java.util.Random@5910e440
========我是分割线=========
[Ljava.lang.String;@6267c3bb
x
null
[Ljava.lang.String;@6267c3bb
a

b

417. Pacific Atlantic Water Flow

```public class Solution {
public List<int[]> pacificAtlantic(int[][] matrix) {
if(matrix == null || matrix.length == 0 || matrix[0].length == 0){
return res;
}
int n = matrix.length, m = matrix[0].length;
boolean[][]pacific = new boolean[n][m];
boolean[][]atlantic = new boolean[n][m];
for(int i=0; i<n; i++){
dfs(matrix, pacific, Integer.MIN_VALUE, i, 0);
dfs(matrix, atlantic, Integer.MIN_VALUE, i, m-1);
}
for(int i=0; i<m; i++){
dfs(matrix, pacific, Integer.MIN_VALUE, 0, i);
dfs(matrix, atlantic, Integer.MIN_VALUE, n-1, i);
}
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
if (pacific[i][j] && atlantic[i][j])
return res;
}

int[][]dir = new int[][]{{0,1},{0,-1},{1,0},{-1,0}};

public void dfs(int[][]matrix, boolean[][]visited, int height, int x, int y){
int n = matrix.length, m = matrix[0].length;
if(x<0 || x>=n || y<0 || y>=m || visited[x][y] || matrix[x][y] < height)
return;
visited[x][y] = true;
for(int[]d:dir){
dfs(matrix, visited, matrix[x][y], x+d[0], y+d[1]);
}
}
}```

### 472. Concatenated Words

```public class Solution {
public static List<String> findAllConcatenatedWordsInADict(String[] words) {
List<String> result = new ArrayList<>();
Set<String> preWords = new HashSet<>();
Arrays.sort(words, new Comparator<String>() {
public int compare (String s1, String s2) {
return s1.length() - s2.length();
}
});

for (int i = 0; i < words.length; i++) {
if (canForm(words[i], preWords)) {
}
}

return result;
}

private static boolean canForm(String word, Set<String> dict) {
if (dict.isEmpty()) return false;
boolean[] dp = new boolean[word.length() + 1];
dp[0] = true;
for (int i = 1; i <= word.length(); i++) {
for (int j = 0; j < i; j++) {
if (!dp[j]) continue;
if (dict.contains(word.substring(j, i))) {
dp[i] = true;
break;
}
}
}
return dp[word.length()];
}
}```

491. Increasing Subsequences

Input: [4, 6, 7, 7]
Output: [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7,7], [4,7,7]]

```public List<List<Integer>> findSubsequences(int[] nums) {
List<List<Integer>> list = new ArrayList<>();
SubList(nums, list, new ArrayList<>(), );
for (int i = ; i < list.size(); i++) {
System.out.println(list.get(i));
}

return list;
}

public void SubList(int[] nums, List<List<Integer>> list, List<Integer> temp, int pos) {
if (pos >= nums.length) {
return;
}
//保存访问过的数字，如果已经访问过一次就跳过，避免同一个位置访问多次同一个数字，这样结果就不会出现重复地序列
Set<Integer> used = new HashSet<>();

for (int i = pos; i < nums.length; i++) {

if (used.contains(nums[i]) || (temp.size() >  && temp.get(temp.size() - ) > nums[i])) {
continue;
}
if (temp.size() >= ) {
}

SubList(nums, list, temp, i + );
temp.remove(temp.size() - );
}

}        ```

542. 01 Matrix

# LeetCode Weekly Contest 24 之 542.01 Matrix

```public class Solution {
public int[][] updateMatrix(int[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (matrix[i][j] == 0) {
queue.offer(new int[] {i, j});//把0元素加入队列中，以备波及影响周围元素
}
else {
matrix[i][j] = Integer.MAX_VALUE;//设为最大值，方便求0元素影响值
}
}
}
//代表传播的四个方向
int[][] dirs = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

while (!queue.isEmpty()) {
int[] cell = queue.poll();
for (int[] d : dirs) {
//需要比较的下一位置元素
int r = cell[0] + d[0];
int c = cell[1] + d[1];
//不符合条件的均跳过，找来的比如：0 的周围 还是0的哪些元素，1的周围是1，但不符合影响值更新规则的同样忽略。
if (r < 0 || r >= m || c < 0 || c >= n ||
matrix[r][c] <= matrix[cell[0]][cell[1]] + 1) continue;
//把受0波及的1元素也放入队列中，涟漪是会持续的。
matrix[r][c] = matrix[cell[0]][cell[1]] + 1;
}
}

return matrix;
}
}```

java.util.ArrayList.set(int index, E element) 替换与指定元素在此列表中指定位置的元素。

list.set(0,1)

### java Queue中 remove/poll, add/offer, element/peek区别：是否抛出异常

poll，remove区别：

remove() 和 poll() 方法都是从队列中删除第一个元素。remove() 的行为与 Collection 接口的版本相似，

peek，element区别：

element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似，在队列为空时， element() 抛出一个异常，而 peek() 返回 null

## 79. Word Search

```public boolean exist(char[][] board, String word) {
char[] w = word.toCharArray();
for (int y=0; y<board.length; y++) {
for (int x=0; x<board[y].length; x++) {
if (exist(board, y, x, w, 0)) return true;
}
}
return false;
}

private boolean exist(char[][] board, int y, int x, char[] word, int i) {
if (i == word.length) return true;
if (y<0 || x<0 || y == board.length || x == board[y].length) return false;
if (board[y][x] != word[i]) return false;
board[y][x] ^= 256;
boolean exist = exist(board, y, x+1, word, i+1)
|| exist(board, y, x-1, word, i+1)
|| exist(board, y+1, x, word, i+1)
|| exist(board, y-1, x, word, i+1);
board[y][x] ^= 256;
return exist;
}```

The binary value for 256 is 100000000. Now we have ascii chars upto 255 numbers so their binary value is between - [0000可以0000 - 11111111] (0 - 255).
Now if you do XOR(^) operation between these ascii chars and 256 it will convert all the range above 256 number. Java has 16 bits for char type. So this is possible. And now none of out string chars will match any of these masked chars because one is under 0-255 other is above 256 range.

```public class Solution {
public boolean exist(char[][] board, String word) {
char[] wordArray = word.toCharArray();
int[][] dirs = new int[][]{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

for(int i = 0; i < board.length; i++) {
for(int j = 0; j < board[0].length; j++) {
if(dfs(board, dirs, i, j, wordArray, 0)) return true;
}
}
return false;
}

public boolean dfs(char[][] board, int[][] dirs, int i, int j, char[] word, int start) {
if(start == word.length) return true;
if(i < 0 || j < 0 || i == board.length || j == board[0].length) return false;
if(board[i][j] == '#' || board[i][j] != word[start]) return false;

boolean res = false;
char c = board[i][j];
board[i][j] = '#';          // use '#' to represent this cell is visited

for(int[] dir: dirs) {
int newRow = i + dir[0], newCol = j + dir[1];
res |= dfs(board, dirs, newRow, newCol, word, start + 1);
if(res) return true;    // if successfully find the word, return immediately
}

board[i][j] = c;            // backtracking
return false;
}
}```

## DFS leetcode的更多相关文章

1. leetcode 39 dfs leetcode 40 dfs

leetcode 39 先排序,然后dfs 注意先整全局变量可以减少空间利用 class Solution { vector<vector<int>>ret; vector&l ...

2. DFS - leetcode [深度优先遍历]

最短路径=>BFS    所有路径=>DFS 126. Word Ladder II BFS+DFS: BFS找出下一个有效的word进队 并记录step 更新两个变量:unordered ...

3. (二叉树 递归 DFS) leetcode 100. Same Tree

Given two binary trees, write a function to check if they are the same or not. Two binary trees are ...

4. (二叉树 BFS DFS) leetcode 104. Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...

5. (二叉树 BFS DFS) leetcode 111. Minimum Depth of Binary Tree

Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shor ...

6. (BFS/DFS) leetcode 200. Number of Islands

Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...

7. 回溯法和DFS leetcode Combination Sum

代码: 个人浅薄的认为DFS就是回溯法中的一种,一般想到用DFS我们脑中一般都有一颗解法树,然后去按照深度优先搜索去寻找解.而分支界限法则不算是回溯,无论其是采用队列形式的还是优先队列形式的分支界限法 ...

8. [LeetCode] questions conclustion_BFS, DFS

BFS, DFS 的题目总结. Directed graph: Directed Graph Loop detection and if not have, path to print all pat ...

9. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

## 随机推荐

1. 一 web爬虫，requests请求

requests请求,就是用python的requests模块模拟浏览器请求,返回html源码 模拟浏览器请求有两种,一种是不需要用户登录或者验证的请求,一种是需要用户登录或者验证的请求 一.不需要用 ...

2. idea结合git使用三

1.将本地代码提交到码云上面的步骤 2.先提交到本地Git的仓库,通过commit files 3.然后vcs----->git---->push,将本地仓库代码,推送到码云公司项目mas ...

3. [转载]JAVA获取word表格中数据的方案

上一个项目的开发中需要实现从word中读取表格数据的功能,在JAVA社区搜索了很多资料,终于找到了两个相对最佳的方案,因为也得到了不少网友们的帮助,所以不敢独自享用,在此做一个分享. 两个方案分别是: ...

4. Project Euler 126 - Cuboid layers

这题先是推公式… 狂用不完全归纳+二次回归,最后推出这么一个奇怪的公式 \[f(t,x,y,z)=4(t-1)(x+y+z+t-2)+2(xy+yz+xz)\] 表示长宽高为\(x\).\(y\).\ ...

5. React 实现 Table 的思考

琼玖 1 年前 (写的零零散散, 包括github不怎么样) Table 是最常用展示数据的方式之一,可是一个产品中往往很多非常类似的 Table, 但是我们碰到的情况往往是 Table A 要排序, ...

6. Git介绍及基本操作

Git基本概念 在Git中,我们将需要进行版本控制的文件目录叫做一个仓库(repository),每个仓库可以简单理解成一个目录,这个目录里面的所有文件都通过Git来实现版本管理,Git都能跟踪并记录 ...

7. [EMWIN]关于 GUI_GetPixelIndex 使用的问题

在模拟器上和st单片机上使用以下代码: GUI_COLOR color0,color1; color0 = GUI_GetPixelIndex(rect.x1+1, rect.y0);color1 = ...

8. 解决 Laravel/Lumen 出现 "Please provide a valid cache path" 问题

解决 Laravel/Lumen 出现 "Please provide a valid cache path" 问题 解决 Laravel/Lumen 出现 "Pleas ...

9. 关于for循环中是否需要缓存length值的个人总结

在JS性能优化中,有一个常见的小优化,即 // 不缓存 for (var i = 0; i < arr.length; i++) { ... } // 缓存 var len = arr.leng ...

10. rhel7+apache+c cgi+动态域名实现web访问

1. 申请动态域名/安装no-ip客户端 https://blog.csdn.net/lee244868149/article/details/44095835 2. yum安装httpd 两种方法安 ...