给定一个二叉树,它的每个结点都存放一个 0-9 的数字,每条从根到叶子节点的路径都代表一个数字。
例如,从根到叶子节点路径 \(1->2->3\) 代表数字 \(123\)。
计算从根到叶子节点生成的所有数字之和。
说明: 叶子节点是指没有子节点的节点。
示例 1:
输入: \([1,2,3]\)
1
/ \
2 3
输出: 25
解释:
从根到叶子节点路径 \(1->2\) 代表数字 \(12\).
从根到叶子节点路径 \(1->3\) 代表数字 \(13\).
因此,数字总和 = \(12 + 13 = 25\).
示例 2:
输入:\([4,9,0,5,1]\)
4
/ \
9 0
/ \
5 1
输出: \(1026\)
解释:
从根到叶子节点路径 \(4->9->5\) 代表数字 \(495\).
从根到叶子节点路径 \(4->9->1\) 代表数字 \(491\).
从根到叶子节点路径 \(4->0\) 代表数字 \(40\).
因此,数字总和 = \(495 + 491 + 40 = 1026\).
整体难度不大,使用深度优先搜索算法递归即可
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private int result = 0;
public int sumNumbers(TreeNode root) {
dfs(root,0);
return result;
}
private void dfs(TreeNode root, int curr) {
if (root == null) {
return;
}
//需要注意的是这里要乘10再加。
curr = curr *10 + root.val;
if(root.left ==null && root.right == null) {
result += curr;
return;
}
//递归左子树
bfs(root.left, curr);
//递归右子树
bfs(root.right,curr);
}
}
]]>翻转一棵二叉树。
示例:
输入:
4
/ \
2 7
/ \ / \
1 3 6 9
输出:
4
/ \
7 2
/ \ / \
9 6 3 1
很简单的递归调用即可。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode invertTree(TreeNode root) {
if (root ==null) {
return root;
}
invert(root);
return root;
}
private void invert(TreeNode root) {
if (root ==null) {
return;
}
TreeNode temp = root.left;
root.left = root.right;
root.right = temp;
invert(root.left);
invert(root.right);
}
}
这道题是HomeBrew的作者 Max Howell 的一条推特中的题。
]]>示例:
输入: \([1,null,2,3]\)
1
\
2
/
3
输出: \([1,3,2]\)
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
经典的算法题,没什么可说的。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
mid(root, result);
return result;
}
private void mid(TreeNode root, List<Integer> result) {
if (root ==null) {
return;
}
//先递归左子树
mid(root.left,result);
//递归完成后加入本节点
result.add(root.val);
//再递归右子树
mid(root.right, result);
}
}
构建一个栈,只要有左子树就一直压栈,直到左子树为空,出栈后将结果加入到result,然后将当前指针指向当前节点的右子树,进行下一轮压栈处理。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new LinkedList<>();
Stack<TreeNode> stack = new Stack<>();
while (root != null || !stack.empty()) {
//左子树一直压栈
while(root != null) {
stack.push(root);
root = root.left;
}
//出栈
root = stack.pop();
result.add(root.val);
//切换到右子树进行下一轮
root = root.right;
}
return result;
}
}
]]>给定字符串 \(S\),找出最长重复子串的长度。如果不存在重复子串就返回 \(0\)。
示例 1:
输入:\("abcd"\)
输出:\(0\)
**解释:**没有重复子串。
示例 2:
输入:\("abbaba"\)
输出:\(2\)
**解释:**最长的重复子串为 \("ab"\) 和 \("ba"\),每个出现 \(2\) 次。
示例 3:
输入:\("aabcaabdaab"\)
输出:\(3\)
**解释:**最长的重复子串为 \("aab"\),出现 \(3\) 次。
示例 4:
输入:\("aaaaa"\)
输出:\(4\)
**解释:**最长的重复子串为 \("aaaa"\),出现 \(2\) 次。
提示:
\(O(n^3)\)的解决方案。三层循环,最里面一层判断是否为重复子串。
class Solution {
public int longestRepeatingSubstring(String str) {
int len = str.length();
int res = 0;
// 大循环
for (int i = 0; i < len; i++) {
// 控制相同字符串第二串的起始索引,注意是i+1的。
for (int j = i + 1; j < len; j++) {
// 本层循环里面的重复字符串长度
int currLen = 0;
for (int k = 0; j + k < len; k++) {
// 第三个变量向前推进,直到不相等为止
if (str.charAt(i + k) == str.charAt(j + k)) {
currLen++;
//判断长度与保存的长度大小
res = Math.max(res, currLen);
} else {
//不符合直接跳出该层循环
break;
}
}
}
}
return res;
}
}
设\(f(i,j)\)为\(S[i:i+k]与S[j:j+k]\)重复字符串的长度,并且\(S[i] == S[j]\),那么会有
\(f(i,j) = f(i - 1, j - 1) + 1\)。
换句话说就是,如果\(S[i]==S[j]\),那么\(i-1\)与\(j-1\)原有的重复字符串长度需要\(+1\)。
这一考虑的空间复杂度为\(O(N)\),时间复杂度为\(O(N^2)\)
class Solution {
public int longestRepeatingSubstring(String str) {
int len = str.length();
char[] chars = str.toCharArray();
int res = 0;
//第一层没什么特殊的,直接循环即可,需要注意的一点是跳出条件为len-res,因为当i超过len-res时,已经绝对不可能再出现大于res的结果了。直接跳出即可。
for (int i = 0; i < len - res; i++) {
int curr = 0;
//注意这里j要从i+1开始计算,因为重复字符串肯定不能从相同起始索引比较起
for (int j = i + 1, k = 0; j < len - res + curr; j++, k++) {
// 注意这里比较的是k与j,
if (chars[k] == chars[j]) {
curr++;
res = Math.max(res, curr);
} else {
curr = 0;
}
}
}
return res;
}
}
]]>请你来实现一个 \(atoi\) 函数,使其能将字符串转换成整数。
首先,该函数会根据需要丢弃无用的开头空格字符,直到寻找到第一个非空格的字符为止。
当我们寻找到的第一个非空字符为正或者负号时,则将该符号与之后面尽可能多的连续数字组合起来,作为该整数的正负号;假如第一个非空字符是数字,则直接将其与之后连续的数字字符组合起来,形成整数。
该字符串除了有效的整数部分之后也可能会存在多余的字符,这些字符可以被忽略,它们对于函数不应该造成影响。
注意:假如该字符串中的第一个非空格字符不是一个有效整数字符、字符串为空或字符串仅包含空白字符时,则你的函数不需要进行转换。
在任何情况下,若函数不能进行有效的转换时,请返回 0。
说明:
假设我们的环境只能存储 32 位大小的有符号整数,那么其数值范围为 \([−2^{31}, 2^{31} − 1]\)。如果数值超过这个范围,qing返回 INT_MAX (\(2^{31} − 1\)) 或 INT_MIN (\(−2^{31}\)) 。
注意看说明,首先我们可以排除一些特殊情况,字符串为空或者长度为0,直接返回0即可。
接下来注意到需要丢弃掉开头的空格字符串。那么可以先用一个while循环,来处理应该跳过的字符数量,在接下来的循环中作为初始索引进行循环。这里还需要注意的一点是可能字符串全是空格,这也是个跳出点。不要进入后面的循环了。
接下来,判断初始索引的第一个字符是什么,如果是'+'(没错,用例中真有+1这种奇葩字符,要当成正数1来处理)或'-',还需要一个特殊处理,设置一个boolean值,代表正负号,并且索引值前进1,如果不是数字,那么需要跳出循环处理。
接下来拼接数字,拼接比较简单,假设\(num\)为当前索引指代的数字,无非就是\(result = result * 10 + num\)。但是,有可能溢出,所以需要考虑如何处理溢出,我们知道Integer.MAX_VALUE = \(2^{31}-1\) 即\(2147483647\), 那么如何才会溢出呢?必然是result > MAX_VALUE/10
或者 result == MAX_VALUE && num>7
时才会溢出,那么溢出后,判断返回值的正负,直接返回对应的值就可以了。
跳出循环后直接返回result即为结果。
class Solution {
public int myAtoi(String str) {
//处理特殊情况
if (str == null || str.length() == 0) {
return 0;
}
int i = 0;
// 排除前面空格
while(i < str.length() && str.charAt(i) == ' ') {
i++;
}
// 处理字符串全为空格的情况
if (i == str.length()) {
return 0;
}
// 当接下来的第一个字符为正负号或者在0~9之间时,进入循环
if (str.charAt(i) == '-' || str.charAt(i) == '+' || (str.charAt(i)>='0' && str.charAt(i) <= '9')) {
// 未溢出时的返回值
int result = 0;
// 正负的判断
boolean negative = str.charAt(i) == '-';
// 判断溢出的情况
int overflowCtl = Integer.MAX_VALUE / 10;
// 如果为正负号,索引需要前进一步
if (str.charAt(i) == '-' || str.charAt(i) == '+') i++;
//开始循环
while (i < str.length()) {
// 如果是数字
if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
//获得当前的数字
int num = str.charAt(i++) - '0';
// 如果溢出,那么返回最大最小值
if (result > overflowCtl || (result == overflowCtl && num>7)) {
if (negative) {
// 如果是负数,返回最小值
return Integer.MIN_VALUE;
} else {
// 否则返回最大值
return Integer.MAX_VALUE;
}
}
// 没溢出 直接累加
result = result * 10 + num;
} else {
// 非数字,直接跳出
break;
}
}
// 注意正负号就可以了
return negative? -result : result;
}
// 兜底策略
return 0;
}
}
]]>给定一个二叉树,返回它的 前序 遍历。
示例:
输入: \([1,null,2,3]\)
1
\
2
/
3
输出: \([1,2,3]\)
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
没什么可说的。直接上代码吧。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) {
return result;
}
preorder(root);
return result;
}
public void preorder(TreeNode root) {
if (root == null) {
return;
}
//先将root.val加入result
result.add(root.val);
//递归左子树
preorder(root.left);
//递归右子树
preorder(root.right);
}
}
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> preorderTraversal(TreeNode root) {
if (root == null) {
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
TreeNode node = stack.pop();
result.add(node.val);
//需要注意这里right要先压栈 其他的基本无压力。
if (node.right !=null) {
stack.push(node.right);
}
if (node.left !=null) {
stack.push(node.left);
}
}
return result;
}
}
]]>数组 \(A\) 是 \([0, 1, ..., N - 1]\) 的一种排列,\(N\) 是数组 \(A\) 的长度。全局倒置指的是 \(i,j\) 满足 \(0 <= i < j < N\) 并且 \([i] > A[j]\) ,局部倒置指的是 \(i\) 满足 \(0 <= i < N\) 并且 \(A[i] > A[i+1]\) 。
当数组 \(A\) 中全局倒置的数量等于局部倒置的数量时,返回 \(true\) 。
示例 1:
输入: \(A = [1,0,2]\)
输出: \(true\)
解释: 有 1 个全局倒置,和 1 个局部倒置。
示例 2:
输入: \(A = [1,2,0]\)
输出: false
解释: 有 2 个全局倒置,和 1 个局部倒置。
注意:
一开始想法比较简单,既然是求逆序对的数量,那么双层for循环一个\(O(n^2)\)的解法就完了。但是第三项注意事项中说了时间限制已经减少,所以毫无意外的超时了。
此时考虑逆序对的性质,全局倒置一定是局部倒置。
对一个\([0,1,...,A.length - 1]\)的排序数组而言,索引\(i\),如果满足\(abs(A[i]-i) <=1\),那么,说明\(i\)只是左移或者右移了一位(或者没变),全局倒置与局部倒置至少同时加一(或者保持不变),仍然满足条件;如果\(abs(A[i] - i) >= 2\),那么,说明\(A[i]\)至少左移或者右移了两位,意味着右边至少有两个比自己小的数(或者左边必然有来2个比自己大且不相邻的数,其中至少一个不相邻)局部(必然<)全局所以造成一定不等,此时不满足条件,直接返回false。
class Solution {
public boolean isIdealPermutation(int[] A) {
if (A.length <3) {
return true;
}
for (int i = 0; i < A.length; i++) {
if (Math.abs(A[i] - i) >= 2) {
return false;
}
}
return true;
}
}
]]>给定两个数组,编写一个函数来计算它们的交集。
示例 1:
输入: nums1 = \([1,2,2,1]\), nums2 = \([2,2]\)
输出: \([2,2]\)
示例 2:
输入: nums1 = \([4,9,5]\), nums2 = \([9,4,9,8,4]\)
输出: [4,9]
说明:
进阶:
问题比较简单。可以遍历两个数组,获得数字出现的频次,然后对比两个频次即可。或者可以直接排序两个数组,然后使用两个指针分别遍历数组即可。
频次法
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if(nums1.length > nums2.length) {
return intersect(nums2, nums1);
}
//这里使用hashmap,因为数字是不定长的。
Map<Integer, Integer> map1 = new HashMap<>(nums1.length);
for (Integer i: nums1) {
Integer count = map1.get(i);
if (count == null) {
map1.put(i, 1);
} else {
map1.put(i, count+1);
}
}
Map<Integer, Integer> map2 = new HashMap<>(nums2.length);
for (Integer i: nums2) {
Integer count = map2.get(i);
if (count == null) {
map2.put(i, 1);
} else {
map2.put(i, count+1);
}
}
Iterator iter = map1.entrySet().iterator();
List<Integer> result = new LinkedList();
//遍历两个频次map即可,这里使用小的那个map遍历。可以减少查询次数
while(iter.hasNext()) {
Map.Entry<Integer, Integer> entry = (Map.Entry) iter.next();
Integer key = entry.getKey();
Integer val1 = entry.getValue();
Integer val2 = map2.get(key);
if(val2 != null) {
int count = Math.min(val1, val2);
while (count-- > 0) {
result.add(key);
}
}
}
//最终生成结果,因为没有用从List到int[]的方便转换方法。
int[] res = new int[result.size()];
for(int m=0;m<res.length;m++)
{
res[m]=result.get(m);
}
return res;
}
}
排序遍历法
class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if(nums1.length > nums2.length) {
return intersect(nums2, nums1);
}
List<Integer> result = new LinkedList();
//排序两个数组
Arrays.sort(nums1);
Arrays.sort(nums2);
//遍历两个数组
for (int i = 0,j=0; i<nums1.length && j<nums2.length;) {
//相等则加入result
if (nums1[i] == nums2[j]) {
result.add(nums1[i]);
i++;
j++;
} else if (nums1[i] < nums2[j]){
// 这里注意只有其中一个往前推进。
i++;
} else {
j++;
}
}
int[] res = new int[result.size()];
for(int m=0;m<res.length;m++)
{
res[m]=result.get(m);
}
return res;
}
}
]]>给定仅有小写字母组成的字符串数组 A,返回列表中的每个字符串中都显示的全部字符(包括重复字符)组成的列表。例如,如果一个字符在每个字符串中出现 3 次,但不是 4 次,则需要在最终答案中包含该字符 3 次。
你可以按任意顺序返回答案。
示例 1:
输入:\(["bella","label","roller"]\)
输出:\(["e","l","l"]\)
示例 2:
输入:\(["cool","lock","cook"]\)
输出:\(["c","o"]\)
提示:
首先需要将每一个字符串进行字母的数量统计。然后遍历数量数组,获取每个字符的最小出现次数,即为答案。
数量统计有多种做法,二维数组、hashmap。此题中使用了二维数组。
class Solution {
public List<String> commonChars(String[] A) {
//生成一个二维数组用于保存词频
int[][] array = new int[A.length][26];
//词频统计
for (int i = 0; i < A.length; i++) {
String s = A[i];
for (char c : s.toCharArray()) {
array[i][c - 97]++;
}
}
List<String> result = new LinkedList<>();
for (int i = 0; i < 26; i++) {
if(array[0][i]==0) {
continue;
}
//得到第一组词频
int count = array[0][i];
for (int j = 1; j < A.length; j++) {
if (array[j][i] == 0){
count = 0;
break;
}
count = Math.min(count, array[j][i]);
}
//如果词频大于0,那么加入结果中
if (count > 0) {
char c = (char) (97 + i);
String s = Character.toString(c);
while (count-- >0) {
result.add(s);
}
}
}
return result;
}
}
]]>给定一个二进制数组, 计算其中最大连续1的个数。
示例 1:
输入: \([1,1,0,1,1,1]\)
输出: \(3\)
解释: 开头的两位和最后的三位都是连续\(1\),所以最大连续\(1\)的个数是 \(3\).
注意:
此题比较简单,一次遍历即可完成。直接上代码。
class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
int result = 0;
int curr = 0;
for(int i: nums) {
if (i == 0) {
curr = 0;
} else{
curr++;
result = Math.max(result, curr);
}
}
return result;
}
}
]]>最大树定义:一个树,其中每个节点的值都大于其子树中的任何其他值。
给出最大树的根节点 \(root\)。
就像之前的问题那样,给定的树是从表 \(A(root = Construct(A))\)递归地使用下述 \(Construct(A)\) 例程构造的:
假设 \(B\) 是 \(A\) 的副本,并附加值 \(val\)。保证 \(B\) 中的值是不同的。
返回 \(Construct(B)\)。
示例 1:
输入: \(root = [4,1,3,null,null,2], val = 5\)
输出: \([5,4,null,1,3,null,null,2]\)
解释: \(A = [1,4,2,3], B = [1,4,2,3,5]\)
示例 2:
输入:\(root = [5,2,4,null,1], val = 3\)
输出:\([5,2,4,null,1,null,3]\)
解释:\(A = [2,1,5,4], B = [2,1,5,4,3]\)
示例 3:
输入:\(root = [5,2,3,null,1], val = 4\)
输出:\([5,2,4,null,1,3]\)
解释:\(A = [2,1,5,3], B = [2,1,5,3,4]\)
这道题本身不难,难得是理解题意,一开始我还在想示例2中的3放在5前面似乎也并无不可。后来才发现,其实\(val\)是追加在\(A\)末尾的,而不是随便放在哪个位置的。既然这样,考虑一下\(val\)与\(root\)的大小关系,一共有三种情况:
递归完成后返回root即可。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode insertIntoMaxTree(TreeNode root, int val) {
// 情况1
if (root == null) {
return new TreeNode(val);
}
// 情况2
if (root.val < val) {
TreeNode node = new TreeNode(val);
node.left = root;
return node;
}
// 情况3
root.right = insertIntoMaxTree(root.right,val);
//最终递归返回
return root;
}
}
]]>给定一个不含重复元素的整数数组。一个以此数组构建的最大二叉树定义如下:
Example 1:
输入: \([3,2,1,6,0,5]\)
输入: 返回下面这棵树的根节点:
6
/ \
3 5
\ /
2 0
\
1
注意:
通过定义,我们发现,可以通过遍历获取到此次遍历的根结点,然后再次分别遍历根节点左边的子数组获取此次遍历的根节点的左儿子,遍历根节点右边的子数组获取根节点的右儿子。需要注意的点是儿子是否存在有两种情况,需要在遍历子数组的时候处理好。
整体没有太大难度,边界条件处理好即可。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode constructMaximumBinaryTree(int[] nums) {
return treefy(nums, 0, nums.length-1);
}
private TreeNode treefy(int[] nums, int start, int end){
//当start==end时,表示为叶节点了,可以生成叶节点跳出了
if(start == end) {
return new TreeNode(nums[start]);
}
//当start>end时,表示叶节点为空,返回null
if (start>end) {
return null;
}
//遍历start与end之间的元素,找到最大值,生成节点。
int maxIndex = start;
for(int i = start; i <= end; i++) {
if (nums[i] > nums[maxIndex]) {
maxIndex = i;
}
}
//找到最大值后,递归调用左儿子与右儿子
TreeNode node = new TreeNode(nums[maxIndex]);
node.left = treefy(nums, start, maxIndex - 1);
node.right = treefy(nums,maxIndex + 1, end);
return node;
}
}
]]>在二维平面上,有一个机器人从原点**(0, 0)开始。给出它的移动顺序,判断这个机器人在完成移动后是否在(0, 0)** 处结束。
移动顺序由字符串表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。
**注意:**机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。
示例 1:
输入: "UD"
**输出:**true
**解释:**机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。
示例 2:
输入: "LL"
输出: false
**解释:**机器人向左移动两次。它最终位于原点的左侧,距原点有两次 “移动” 的距离。我们返回 false,因为它在移动结束时没有返回原点。
问题看似复杂,其实就是上下的步数相等,左右的步数相等,那么必然回到原点。所以设置4个int。遍历moves,对应++即可。
class Solution {
public boolean judgeCircle(String moves) {
//实际上就是上下数量一致,左右数量一致就可以回到原点
int u=0;
int d=0;
int l=0;
int r=0;
char[] array = moves.toCharArray();
for (char c : array) {
switch(c) {
case 'U': u++;break;
case 'D': d++;break;
case 'L': l++;break;
case 'R': r++;break;
}
}
return u == d && l == r;
}
}
]]>编写一个程序,找到两个单链表相交的起始节点。
如下面的两个链表:
在节点\(c1\)开始相交。
示例 1:
输入: intersectVal = 8, listA = \([4,1,8,4,5]\), listB = \([5,0,1,8,4,5]\), skipA = 2, skipB = 3
**输出:**Reference of the node with value = 8
**输入解释:**相交节点的值为 \(8 \)(注意,如果两个列表相交则不能为 \(0\))。从各自的表头开始算起,链表 A 为 \([4,1,8,4,5]\),链表 B 为 \([5,0,1,8,4,5]\)。在 A 中,相交节点前有\(2\)个节点;在 B 中,相交节点前有\(3\)个节点。
示例 2:
**输入:**intersectVal = 2, listA = \([0,9,1,2,4]\), listB = \([3,2,4]\), skipA = 3, skipB = 1
**输出:**Reference of the node with value = 2
**输入解释:**相交节点的值为 \(2\) (注意,如果两个列表相交则不能为 \(0\))。从各自的表头开始算起,链表 A 为 \([0,9,1,2,4]\),链表 B 为 \([3,2,4]\)。在 A 中,相交节点前有 \(3\) 个节点;在 B 中,相交节点前有 \(1\) 个节点。
示例 3:
**输入:**intersectVal = 0, listA = \([2,6,4]\), listB = \([1,5]\), skipA = 3, skipB = 2
**输出:**null
**输入解释:**从各自的表头开始算起,链表 A 为 \([2,6,4]\),链表 B 为 \([1,5]\)。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
**解释:**这两个链表不相交,因此返回 null。
注意:
注意到这样一个事实,如果两个链表相交,那么在相交节点前面的两个分叉上,正好相差这两个链表长度之差的元素个数。因此,首先遍历两个链表,得到链表长度,然后长度更长的链表先往前走长度之差的步数,然后后面两个链表向前推进到相等即为结果。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
//首先遍历两个链表查询长度
int l1 = 0;
ListNode h1 = headA;
while (h1 != null) {
h1 = h1.next;
l1++;
}
int l2 = 0;
ListNode h2 = headB;
while (h2 != null) {
h2 = h2.next;
l2++;
}
//长度更长的那个需要先走几步,直到两个长度相等时才同时前进。
h1 = headA;
h2 = headB;
int dif = l1-l2;
if (dif > 0) {
while(dif-- >0) {
h1 = h1.next;
}
} else if (dif < 0) {
while(dif++ < 0) {
h2 = h2.next;
}
}
int len = Math.min(l1, l2);
while (h1 != h2){
h1 = h1.next;
h2 = h2.next;
}
return h1;
}
}
]]>假设Andy和Doris想在晚餐时选择一家餐厅,并且他们都有一个表示最喜爱餐厅的列表,每个餐厅的名字用字符串表示。
你需要帮助他们用最少的索引和找出他们共同喜爱的餐厅。 如果答案不止一个,则输出所有答案并且不考虑顺序。 你可以假设总是存在一个答案。
示例 1:
输入:
\(["Shogun", "Tapioca Express", "Burger King", "KFC"]\)
\(["Piatti", "The Grill at Torrey Pines", "Hungry Hunter Steakhouse", "Shogun"]\)
输出: \(["Shogun"]\)
解释: 他们唯一共同喜爱的餐厅是\("Shogun"\)。
示例 2:
输入:
\(["Shogun", "Tapioca Express", "Burger King", "KFC"]\)
\(["KFC", "Shogun", "Burger King"]\)
输出: \(["Shogun"]\)
解释: 他们共同喜爱且具有最小索引和的餐厅是\("Shogun"\),它有最小的索引和1(0+1)。
提示:
直接看上去就是两个数组求并集的一个延伸,那么直接使用双层循环+特殊退出条件即可完成。时间复杂度\(O(m*n)\),其中\(m、n\)为两个数组的长度。
class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
// 最小索引和
int minIndex = Integer.MAX_VALUE;
List<String> result = new LinkedList<>();
// 第一层循环,可以依靠minIndex来缩短部分循环
for(int i = 0;i < list1.length && i <= minIndex; i++) {
// 第二层循环,依靠i与minIndex可以缩短部分循环
for(int j = 0;j < list2.length && i + j <= minIndex; j++) {
// 如果两个元素相等,才进入后面的判断
if (list1[i].equals(list2[j])) {
// 如果i+j<minIndex,说明找到了更小的索引和,需要重置result,并且 缩小minIndex
if (i + j < minIndex) {
minIndex = i + j;
result.clear();
result.add(list1[i]);
} else if (i + j == minIndex) {
result.add(list1[i]);
}
}
}
}
return result.toArray(new String[0]);
}
}
解法1时间复杂度较高,能不能通过另外的手段降低呢?同时发现提示中有个数组中没有重复元素的前提,考虑java中的HashMap(插入、查询的时间复杂度可以认为是\(O(1)\)的)来保存其中一个列表的字符串与索引值。另一个数组再次遍历一下查找HashMap中是否存在相同的关系就可以了。两个循环非嵌套,所以可以认为时间复杂度为\(O(m+n)\)的。
另外,为了减少空间复杂度与降低HashMap的hash冲突,考虑对两个数组中较小的那个放入HashMap中。同时,更极端的情况下需要考虑HashMap的rehash对时间复杂度与空间复杂度的影响。
class Solution {
public String[] findRestaurant(String[] list1, String[] list2) {
int l1 = list1.length;
int l2 = list2.length;
//找到规模更小的数组进行hash化
if (l1 > l2) {
return findRestaurant(list2, list1);
}
//极端情况下需要考虑rehash
Map<String, Integer> map = new HashMap<>(l1);
for(int i = 0;i < l1; i++) {
map.put(list1[i],i);
}
int minIndex = Integer.MAX_VALUE;
List<String> result = new LinkedList<>();
//遍历第二个数组,这里可以用j<=minIndex来提前跳出循环,因为大于的必然不在结果中
for(int j = 0;j < l2 && j <= minIndex; j++) {
Integer index = map.get(list2[j]);
if (index != null) {
if (index + j < minIndex) {
minIndex = index + j;
result.clear();
result.add(list2[j]);
} else if (index + j == minIndex){
result.add(list2[j]);
}
}
}
return result.toArray(new String[0]);
}
}
]]>给定一个二叉树,返回它的 后序 遍历。
示例:
输入: [1,null,2,3]
1
\
2
/
3
输出: [3,2,1]
进阶: 递归算法很简单,你可以通过迭代算法完成吗?
递归方法很简单,不再赘述。迭代方法一般考虑用双栈法。java的LinkedList提供了add(0,value)的方法,可以直接使用这个作为第二个栈与返回值。
递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) {
return result;
}
dfs(root);
return result;
}
private void dfs(TreeNode root) {
if (root == null) {
return;
}
dfs(root.left);
dfs(root.right);
result.add(root.val);
}
}
迭代
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> postorderTraversal(TreeNode root) {
if (root == null) {
return result;
}
Stack<TreeNode> stack = new Stack<>();
stack.push(root);
while(!stack.empty()) {
TreeNode node = stack.pop();
//直接add(0,val);
result.add(0, node.val);
//注意这里是左先入栈
if (node.left !=null){
stack.push(node.left);
}
//然后是右入栈
if(node.right !=null) {
stack.push(node.right);
}
}
return result;
}
}
]]>给定一个 N 叉树,返回其节点值的后序遍历。
例如,给定一个 \(3叉树\):
返回其后序遍历: \([5,6,3,2,4,1]\).
**说明: **递归法很简单,你可以使用迭代法完成此题吗?
与589. N叉树的前序遍历基本一样,只是把前序变成后续即可。
但是迭代的后序遍历比较难。这里考虑用双栈法处理。同时,联想到java的List接口提供一个add(int index, E val)的方法,可以当作栈使用,所以,其实第二个栈可以直接用result代替。
递归
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> postorder(Node root) {
if (root == null) {
return result;
}
dfs(root);
return result;
}
private void dfs(Node root) {
if (root == null) {
return;
}
//优先去遍历子树而不是root本身
if (root.children !=null) {
for(Node node:root.children) {
dfs(node);
}
}
result.add(root.val);
}
}
递归
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> postorder(Node root) {
if (root == null) {
return result;
}
Stack<Node> stack = new Stack<>();
stack.add(root);
while(!stack.empty()) {
Node node = stack.pop();
//直接将当前val写入result第一个元素即可。
result.add(0, node.val);
//这里要注意。是node.children。一开始写成了root.children...
//而且与前序遍历不同,这里是从左往右入栈,如果不能想清楚可以在纸上画一下。
List<Node> children = node.children;
if (children != null) {
stack.addAll(children);
}
}
return result;
}
}
]]>给定一个 \(N\) 叉树,返回其节点值的前序遍历。
例如,给定一个 \(3叉树\) :
返回其前序遍历: \([1,3,5,6,2,4]\)。
说明: 递归法很简单,你可以使用迭代法完成此题吗?
如果忽略说明的话,使用递归确实非常简单。与二叉树的前序遍历一样,唯一需要注意的一点是儿子是个list。递归转迭代也只是用栈来缓存
下面给出递归与迭代的不同代码
递归方式
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> preorder(Node root) {
if (root == null) {
return result;
}
dfs(root);
return result;
}
private void dfs(Node root) {
if (root == null) {
return;
}
result.add(root.val);
//唯一需要注意的就是这里的对儿子的处理
if (root.children !=null) {
for(Node node:root.children) {
dfs(node);
}
}
}
}
迭代方式
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
private List<Integer> result = new LinkedList<>();
public List<Integer> preorder(Node root) {
if (root == null) {
return result;
}
//使用栈来存储
Stack<Node> stack = new Stack<>();
stack.push(root);
while (!stack.isEmpty()) {
Node node = stack.pop();
result.add(node.val);
if (node.children !=null) {
List<Node> children = node.children;
//这里需要注意的一点是,压栈的顺序,要从最右开始压
for(int i=children.size() - 1;i >= 0;i--){
stack.push(children.get(i));
}
}
}
return result;
}
}
]]>给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。
例如,给定一个 3叉树 :
返回其层序遍历:
[
[1],
[3,2,4],
[5,6]
]
说明:
树的深度不会超过 1000。
树的节点总数不会超过 5000。
既然是层序遍历,那么自然而然的考虑用队列处理。只需要处理好边界条件就可以了。没难度
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> children;
public Node() {}
public Node(int _val,List<Node> _children) {
val = _val;
children = _children;
}
};
*/
class Solution {
private List<List<Integer>> results = new LinkedList<>();
public List<List<Integer>> levelOrder(Node root) {
//如果root为空,直接返回
if (root == null) {
return results;
}
//创建一个deque。
Deque<Node> queue = new LinkedList<>();
//root入队
queue.offer(root);
while(!queue.isEmpty()) {
//每次大循环开始时的队列长度为当前层的元素数量。
int length = queue.size();
List<Integer> result = new ArrayList<>(length);
while (length-- >0) {
//出队
Node node = queue.pop();
//设置val
result.add(node.val);
//将儿子入队
queue.addAll(node.children);
}
results.add(result);
}
return results;
}
}
]]>给定一个二叉树,判断它是否是高度平衡的二叉树。
本题中,一棵高度平衡二叉树定义为:
一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1。
示例 1:
给定二叉树\([3,9,20,null,null,15,7]\)
3
/ \
9 20
/ \
15 7
返回\(true\)
示例 2:
给定二叉树 \([1,2,2,3,3,null,null,4,4]\)
1
/ \
2 2
/ \
3 3
/ \
4 4
返回\(false\)
深度优先搜索,首先获取左右子树的深度,判断相差是否小于1,然后递归左右子树即可,需要注意的一点是获取左右子树深度的时候需要+1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null) {
return true;
}
//获取左子树的深度
int left = dfs(root.left);
//获取右子树的深度
int right = dfs(root.right);
//判断左右子树深度差 递归调用左右子树。
return Math.abs(left-right) <=1 && isBalanced(root.left) && isBalanced(root.right);
}
//获取树的深度
private int dfs(TreeNode root) {
if(root == null) {
return 0;
}
int left = dfs(root.left);
int right = dfs(root.right);
//左右深度+1的最大值。
return Math.max(left, right) + 1;
}
}
]]>