B 属于 A 的一部分也可以,没必要一直匹配到叶子节点
当节点 B 为空:说明树 B 已匹配完成(越过叶子节点),因此返回 true ;
当节点 A 为空:说明已经越过树 A 叶子节点,即匹配失败,返回 false;
安安写法
//Java 安安 2020.3.31
//根节点作为第0行
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
List<List<Integer>> res = new ArrayList<>();
Stack<TreeNode> s1 = new Stack<>(); //存储奇数行
Stack<TreeNode> s2 = new Stack<>(); //存储偶数行
if(root != null) s2.push(root);
while(!s1.empty() || !s2.empty()) {
List<Integer> tmp = new ArrayList<>();
if(!s2.empty()){
while(!s2.empty()){
TreeNode p = s2.pop();
tmp.add(p.val);
if(p.left != null) s1.push(p.left);
if(p.right != null) s1.push(p.right);
}
res.add(tmp);
}
if(!s1.empty()){
tmp = new ArrayList<>();
while(!s1.empty()){
TreeNode p = s1.pop();
tmp.add(p.val);
if(p.right != null) s2.push(p.right);
if(p.left != null) s2.push(p.left);
}
res.add(tmp);
}
}
return res;
}
}
两个 peek() 方法返回的都是 Integer 类型 ,它们的比较不能用 ==,因为 == 用于包装类型(它们都是对象)的比较,比较的是它们的内存地址,解决方法也很简单:(1)改用 equals() 方法,(2)至少把其中一个变量转成 int 型。
//快排思想
class Solution {
public int[] getLeastNumbers(int[] arr, int k) {
if(arr == null || arr.length == 0 || k <= 0) return new int[0];
if(k > arr.length) return arr;
quickSearch(arr, 0, arr.length-1, k);
int[] res = new int[k];
for(int i = 0; i < k; i++){
res[i] = arr[i];
}
//System.out.println("最后:" + Arrays.toString(arr));
return res;
}
public void quickSearch(int[] arr, int left, int right, int k){
int index = quickSort(arr, left, right);
if(index == k) return;
else if(index < k) quickSearch(arr, index+1, right, k);
else quickSearch(arr, left, index-1, k);
}
public int quickSort(int[] arr, int left, int right){
if(left >= right) return left;
int i = left;
int j = right;
int x = arr[left];
while(i < j){
while(i < j && arr[j] >= x){
j--;
}
arr[i] = arr[j];
while(i < j && arr[i] <= x){
i++;
}
arr[j] = arr[i];
}
arr[i] = x;
// quickSort(arr, left, i-1);
// quickSort(arr, i+1, right);
//说明:因为quickSearch已经进行了递归,所以qucikSort就不递归啦
return j;
}
}
安安自己的题解 三种解法
两端都闭的写法(不存在也返回 返回的刚好是插入点的位置)
//anan 2020.8.18
class Solution {
public int[] singleNumbers(int[] nums) {
//System.out.println(Arrays.toString(nums));
Arrays.sort(nums);
//System.out.println(Arrays.toString(nums));
int[] res = new int[2];
int length = nums.length;
int count = 0;
int i = 0;
while(i < length-1){
if(nums[i] == nums[i+1]){
i+=2;
}else{
res[count++] = nums[i];
i+=1;
}
}
if(nums[length-1] != nums[length-2]){
res[count++] = nums[length-1];
}
return res;
}
}
//anan 2020.8.18 二分法+双指针
class Solution {
public int[] twoSum(int[] nums, int target) {
int left = 0;
int right = leftBound(nums, target);
/*
利用二分法缩小范围
nums = [2,7,11,15], target = 9 只需要搜索[2,7,11]
nums = [10,26,30,31,47,60], target = 40 只需要搜索[10,26,30,31,47]
*/
int[] res = new int[2];
while(left < right){
if(nums[left] + nums[right] < target){
left++;
}else if(nums[left] + nums[right] > target){
right--;
}else{
res[0] = nums[left];
res[1] = nums[right];
break;
}
}
return res;
}
//若是数组中有target,返回target第一次出现的位置
//若是数组中没有target,返回target该插入的位置
public int leftBound(int[] nums, int target){
int left = 0;
int right = nums.length-1;
while(left <= right){
int mid = left + (right-left)/2;
if(nums[mid] < target){
left = mid+1;
}else if(nums[mid] > target){
right = mid-1;
}else if(nums[mid] == target){
right = mid-1;
}
}
return (left >= nums.length ? nums.length-1 : left);
}
}
//anan 用stack 定位单词边界 不用API
class Solution {
public String reverseWords(String s) {
int left = 0, right = s.length() - 1;
// 去掉字符串开头的空白字符
while (left <= right && s.charAt(left) == ' ') ++left;
// 去掉字符串末尾的空白字符
while (left <= right && s.charAt(right) == ' ') --right;
int i = left;
int j = left;
Deque<String> stack = new LinkedList<>();
StringBuffer sb = new StringBuffer();
while(i <= j && j <= right){
while(i <= right && s.charAt(i) == ' ') i++;
j = i;
while(j <= right && s.charAt(j) != ' ') j++;
stack.push(s.substring(i, j));
i = j;
}
return String.join(" ", stack);
}
}
class Solution {
public StringBuilder trimSpaces(String s) {
int left = 0, right = s.length() - 1;
// 去掉字符串开头的空白字符
while (left <= right && s.charAt(left) == ' ') ++left;
// 去掉字符串末尾的空白字符
while (left <= right && s.charAt(right) == ' ') --right;
// 将字符串间多余的空白字符去除
StringBuilder sb = new StringBuilder();
while (left <= right) {
char c = s.charAt(left);
if (c != ' ') sb.append(c);
else if (sb.charAt(sb.length() - 1) != ' ') sb.append(c);
++left;
}
return sb;
}
public void reverse(StringBuilder sb, int left, int right) {
while (left < right) {
char tmp = sb.charAt(left);
sb.setCharAt(left++, sb.charAt(right));
sb.setCharAt(right--, tmp);
}
}
public void reverseEachWord(StringBuilder sb) {
int n = sb.length();
int start = 0, end = 0;
while (start < n) {
// 循环至单词的末尾
while (end < n && sb.charAt(end) != ' ') ++end;
// 翻转单词
reverse(sb, start, end - 1);
// 更新start,去找下一个单词
start = end + 1;
++end;
}
}
public String reverseWords(String s) {
StringBuilder sb = trimSpaces(s);
// 翻转字符串
reverse(sb, 0, sb.length() - 1);
// 翻转每个单词
reverseEachWord(sb);
return sb.toString();
}
}
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/reverse-words-in-a-string/solution/fan-zhuan-zi-fu-chuan-li-de-dan-ci-by-leetcode-sol/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。