树的子结构
给定两棵树,判断第二棵树是不是第一棵树的子树。
规定空树不是任意树的子结构。
想到用将树的结构转为先序,中序,或者后序序列,然后比较序列的包含关系,如果两颗树的同一个序列,比如先序序列,前树包含后树,那么返回true,否则返回false.
代码如下:
1 | /** |
不借用外部结构,只通过树的结点匹配与否来实现,递归版本如下:
1 | public class Solution { |
敲石101下
给定两棵树,判断第二棵树是不是第一棵树的子树。
规定空树不是任意树的子结构。
想到用将树的结构转为先序,中序,或者后序序列,然后比较序列的包含关系,如果两颗树的同一个序列,比如先序序列,前树包含后树,那么返回true,否则返回false.
代码如下:
1 | /** |
不借用外部结构,只通过树的结点匹配与否来实现,递归版本如下:
1 | public class Solution { |
两个有序链表的合并,一开始想到了插入排序的思想,但是算法时间复杂度太大了o(n2),代码如下:
1 | /** |
本地测试用例通过,但是牛客网在线提交失败,后来有看到了一个递归的解法,觉得很是巧妙,自己写了一遍,代码如下:
1 | public class Solution { |
该算法用来计算a的b次方,通过将指数化为2的进制表示法,来降低乘法的运算次数,比如a的9次方,因为9 = 1001(二进制),所以可以写成a^9 = a^(2^0)*a^(2^3);这样可以将算法复杂度由o(n)将为o(logn).
1 | public class Solution { |
在一个二维数组中(每个一维数组的长度相同),每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
more >>Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋
times.
You may assume that the array is non-empty and the majority element always exist in the array.
more >>Evaluate the value of an arithmetic expression in Reverse Polish Notation.
Valid operators are +
, -
, *
, /
. Each operand may be an integer or another expression.
Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
Example:
Given a = 1 and b = 2, return 3.
Credits:
Special thanks to @fujiaozhu for adding this problem and creating all test cases.
tag:
缺失模块。
1、请确保node版本大于6.2
2、在博客根目录(注意不是yilia根目录)执行以下命令:
npm i hexo-generator-json-content --save
3、在根目录_config.yml里添加配置:
jsonContent: meta: false pages: false posts: title: true date: true path: true text: false raw: false content: false slug: false updated: false comments: false link: false permalink: false excerpt: false categories: false tags: true