由于是二分查找树,根据根的不同来迭代。
附上代码。一次AC开始一直没想明白。哎。
class Solution {
public:
int numTrees(int n) {
// Note: The Solution object is instantiated only once and is reused by each test case.
if(n == 1)return 1;
if(n == 2)return 2;
if(n == ……
哎哟我擦。用递归让我AC了还不算我过,题目前面没打勾,对我这种有强迫症的人来说,好痛苦。回头想想别的办法。
class Solution {
public:
vector ans;
vector preorderTraversal(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance……
序列化二叉树。。。。
一次AC附上代码:
class Solution {
public:
vector<vector<int> > levelOrder(TreeNode *root) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vec……
这个题让我对二叉树的序列化有一个新的认识啦。
1
2 3
4 5 6
像这个树表示成:1 2 3 4 # 5 6
意义就是
1 | 2 3 | 4 # 5 6
分别是第一 第二 第三层。
这样的话只需要每次一层一层的枚举就好。
注意越界的问题,附上代码:
class Solution {
public:
void connect(TreeLinkNode *root……
两次二分,一次AC,注意越界的问题。
using namespace std;
class Solution {
public:
bool searchMatrix(vector<vector<int> > &matrix, int target) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test……
这个题,判断一个单链表是否存在闭环。不允许开辟额外的空间。
开始确实不知道怎么解,就去看别人的解法了。
大意是,两个指针,一个前进一步,另一个前进两步,如果到了NULL这两个指针还没出现相等的情况,欧克没有闭环,如果出现相等,就有闭环。
附上代码。个人觉得对指针和链表的操作我真心烂
class Solution {
publ……
注意k从0开始算。
附上代码:
class Solution {
public:
vector<int> getRow(int rowIndex) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
rowIndex++;
vector<int> ans(rowIndex……
输出螺旋增长的矩阵,两次AC注意n=0的情况
class Solution {
public:
vector<vector<int> > generateMatrix(int n) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
vector……
简单的数组或者vector操作,一次AC
找找上下行的关系。
附上代码:
class Solution {
public:
vector<vector<int> > generate(int numRows) {
// Note: The Solution object is instantiated only once and is reused by each test case.
vector<vector<int>> ans(numRows)……
简单DP一次AC,附上代码:
class Solution {
public:
int minimumTotal(vector<vector<int> > &triangle) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
for(int i = triangle.size()……