leetcode刷题总结一

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

Single Number面试

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

把全部数求一下异或lua

Maximum Depth of Binary Treespa

求树的最大深度code

递归遍历一遍blog

Same Tree递归

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

两棵树同时遍历一遍element

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

相关文章
相关标签/搜索