leetcode刷题总结一

大四狗找工作,要刷题了,leetcode上面题目比较适合面试算法类题目,也不纯粹为了蒙题,锻炼一下面试类型的思维

Single Number

有N个数,其中只有一个数出现了一次,其他都是两次,找出那个数

把所有数求一下异或

Maximum Depth of Binary Tree

求树的最大深度

递归遍历一遍

Same Tree

给两个树的根节点,看两棵树是否相同

两棵树同时遍历一遍

Reverse Integer

输出这个数倒过来的数

注意负数情况,模拟一下即可

Best Time to Buy and Sell Stock II

模拟买东西卖东西赚钱

模拟即可

Unique Binary Search Trees

给定N个节点,问有多少种不同的二叉树

卡特兰数经典案例,C(2n,n) / (n+1)

Linked List Cycle

判断一个链表是否含有环

从head出发,一个一次一步,一个一次两步,若相交则有环,否则走到头就结束说明没有

Binary Tree Inorder Traversal

中序遍历

Binary Tree Preorder Traversal

前序遍历

Populating Next Right Pointers in Each Node

把每层的节点按从左到右顺序链接起来,最后一个节点next指向null

void connect(TreeLinkNode *root) {
        if (root == NULL) return;
        connect(root->left);
        connect(root->right);
        TreeLinkNode *l = root->left;
        TreeLinkNode *r = root->right;
        while (l) {
            l->next = r;
            l = l->right;
            r = r->left;
        }
    }

Remove Duplicates from Sorted List

删除相同数值的节点链表,已排好序

扫一遍,跟前一个相同就删了那个节点

Search Insert Position

给出target,求target应该插入的array的index

二分即可

Climbing Stairs

经典的fbi数列

Single Number II

single numberI加强版,one,two,three分别代表出现的次数

 int singleNumber(int A[], int n) {
        int one , two , three;
        one = two = three = 0;
        for (int i = 0;i < n;i ++) {
            two |= one&A[i];
            one ^= A[i];
            three = one & two;
            one &= ~three;
            two &= ~three;
        }
        return one;
    }

Maximum Subarray

经典动态规划,求最长连续序列和

Remove Element

去掉array中的element元素

不用额外资源,跟尾资源swap

Merge Two Sorted Lists

合并两个有序链表

Balanced Binary Tree

Convert Sorted Array to Binary Search Tree

Remove Duplicates from Sorted Array

Swap Nodes in Pairs

Symmetric Tree

Merge Sorted Array

Sort Colors

Plus One

Permutations

Minimum Path Sum

Container With Most Water

Best Time to Buy and Sell Stock

Linked List Cycle II

Set Matrix Zeroes

Path Sum

Remove Nth Node From End of List

Sum Root to Leaf Numbers

Minimum Depth of Binary Tree

Length of Last Word

Palindrome Number

Valid Parentheses

Jump Game

Triangle

Validate Binary Search Tree

Pow(x, n)

Next Permutation

Jump Game II

Sqrt(x)

Best Time to Buy and Sell Stock III

Rotate List

Reorder List

Evaluate Reverse Polish Notation

Two Sum

Reverse Words in a String

原文地址:https://www.cnblogs.com/Wiki-ki/p/3980188.html