1. TwoSum

  https://www.cnblogs.com/zhacai/p/10429120.html  easy

2. Add Two Numbers

  https://www.cnblogs.com/zhacai/p/10429155.html  easy

15.3Sum

  https://www.cnblogs.com/zhacai/p/10579514.html medium set

20.Valid Parentheses

  https://www.cnblogs.com/zhacai/p/10429168.html  easy

21.Merge Two Sorted Lists

  https://www.cnblogs.com/zhacai/p/10429234.html  easy  

22.Generate Parentheses

  https://www.cnblogs.com/zhacai/p/10599156.html medium DFS 剪枝

49.Group Anagrams

  https://www.cnblogs.com/zhacai/p/10576638.html medium 哈希 map

53.Maximum Subarray 

  https://www.cnblogs.com/zhacai/p/10429247.html  easy

70.Climbing Stairs

  https://www.cnblogs.com/zhacai/p/10429253.html  easy

72.Edit Distance

  https://www.cnblogs.com/zhacai/p/10661858.html hard dp

79.Word Search

  https://www.cnblogs.com/zhacai/p/10641454.html medium DFS

94. Binary Tree Inorder Traversal

  https://www.cnblogs.com/zhacai/p/10435799.html  medium 树的中序遍历<递归><迭代(栈)>

98.Validate Binary Search Tree

  https://www.cnblogs.com/zhacai/p/10592337.html medium 二叉搜索树 中序遍历 递归

102.Binary Tree Level Order Traversal

  https://www.cnblogs.com/zhacai/p/10598674.html medium 树 BFS

104.Maxinum Depth of Binary Tree  

  https://www.cnblogs.com/zhacai/p/10429258.html  easy 树DFS

121. Best Time to Buy and Sell Stock

  https://www.cnblogs.com/zhacai/p/10429264.html  easy

136. Single Number  

  https://www.cnblogs.com/zhacai/p/10429270.html  easy

141. Linked List Cycle

  https://www.cnblogs.com/zhacai/p/10560803.html easy 链表 快慢指针

142. Linked List Cycle II 

  https://www.cnblogs.com/zhacai/p/10561152.html medium 链表 快慢指针

146. LRU Cache

  https://www.cnblogs.com/zhacai/p/10665121.html hard LRUCache linkedHashMap hashmap+双向链表

152. Maximum Product Subarray

  https://www.cnblogs.com/zhacai/p/10644095.html medium dp

155. Min Stack

  https://www.cnblogs.com/zhacai/p/10429280.html  easy

169.Majority Element

  https://www.cnblogs.com/zhacai/p/10429286.html  easy

200.Number of Islands

  https://www.cnblogs.com/zhacai/p/10662241.html medium dfs 并查集

206.ReverseLinked List

  https://www.cnblogs.com/zhacai/p/10429295.html  easy 链表

208.Implement Trie(Prefix Tree)

  https://www.cnblogs.com/zhacai/p/10640769.html medium 字典树

226.Invert Binary Tree  

  https://www.cnblogs.com/zhacai/p/10431018.html  easy 树 递归

236.Lowest Common Ancestor of a Binary Tree

  https://www.cnblogs.com/zhacai/p/10592779.html medium 树 递归

239.Sliding Window Maximum

  https://www.cnblogs.com/zhacai/p/10567783.html  hard 双向队列 大顶堆

283.Move Zeroes

  https://www.cnblogs.com/zhacai/p/10431000.html  easy

300.Longest Increasing Subsequence

  https://www.cnblogs.com/zhacai/p/10661330.html medium dp

309.Best Time to Buy and Sell Stock with Cooldown

  https://www.cnblogs.com/zhacai/p/10655970.html medium dp

322.Coin Change

  https://www.cnblogs.com/zhacai/p/10661489.html medium dp

338. Counting Bits

  https://www.cnblogs.com/zhacai/p/10430986.html  easy  位运算 找规律

347. Top K Frequent Elements(待完善)

  https://www.cnblogs.com/zhacai/p/10436261.html  medium 排序 map

438.Find All Anagrams in a String

  https://www.cnblogs.com/zhacai/p/10596274.html  easy 滑动窗口 map

448. Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. Find all the elements of [1, n] inclusive that do not appear in this array. Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

Example:

Input:
[4,3,2,7,8,2,3,1] Output:
[5,6]
public List<Integer> findDisappearedNumbers(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int x = nums[i]<0?-nums[i]:nums[i];
if(nums[x-1]>0){
nums[x-1]= -nums[x-1];
}
}
List<Integer> re = new ArrayList<Integer>();
for (int i = 0; i < nums.length; i++) {
if(nums[i]>0){
re.add(i+1);
}
}
return re;
}

    

461.Hamming Distance  

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.

Note:0 ≤ x, y < 231.

Example:

Input: x = 1, y = 4

Output: 2

Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑ The above arrows point to positions where the corresponding bits are different.
public int hammingDistance(int x, int y) {//位运算
int n = x^y;
int re = 0;
//计算整数二进制中1的个数
while(0!=n){
n=n&(n-1);
re++;
}
return re;
}

538.Convert BST to Greater Tree

Given a Binary Search Tree (BST), convert it to a Greater Tree such that every key of the original BST is changed to the original key plus sum of all keys greater than the original key in BST.

Example:

Input: The root of a Binary Search Tree like this:
5
/ \
2 13 Output: The root of a Greater Tree like this:
18
/ \
20 13
class Solution {
int sum=0;
public TreeNode convertBST(TreeNode root) {//树
if(null!=root){
TreeNode right = convertBST(root.right);
root.val+=sum;
sum = root.val;
TreeNode left = convertBST(root.left);
}
return root;
}
}

  

543.Diameter of Binary Tree

Given a binary tree, you need to compute the length of the diameter of the tree. The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root.

Example:
Given a binary tree

          1
/ \
2 3
/ \
4 5

Return 3, which is the length of the path [4,2,1,3] or [5,2,1,3].

Note: The length of path between two nodes is represented by the number of edges between them.

class Solution {
int re;
public int diameterOfBinaryTree(TreeNode root) {//树的深度 递归
re =0;
maxDepth(root);
return re;
}
public int maxDepth(TreeNode root) {
if(null==root){
return 0;
}
int ld=maxDepth(root.left);
int lr = maxDepth(root.right);
int d = ld>lr?(ld+1):(lr+1);
re =re>(ld+lr)?re:(ld+lr);
return d;
}
}

  

617.Merge Two Binary Trees

Given two binary trees and imagine that when you put one of them to cover the other, some nodes of the two trees are overlapped while the others are not. You need to merge them into a new binary tree. The merge rule is that if two nodes overlap, then sum node values up as the new value of the merged node. Otherwise, the NOT null node will be used as the node of new tree.

Example 1:

Input:
Tree 1 Tree 2
1 2
/ \ / \
3 2 1 3
/ \ \
5 4 7
Output:
Merged tree:
3
/ \
4 5
/ \ \
5 4 7

Note: The merging process must start from the root nodes of both trees.

public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {//树 递归
if(null==t1&&null==t2){
return null;
}
if(null==t1){
return t2;
}
if(null==t2){
return t1;
}
TreeNode left=mergeTrees(t1.left,t2.left);
TreeNode right = mergeTrees(t1.right,t2.right);
TreeNode node = new TreeNode(t1.val+t2.val);
node.left=left;
node.right=right;
return node;
}  

简洁写法

public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null)
return t2;
if (t2 == null)
return t1;
t1.val += t2.val;
t1.left = mergeTrees(t1.left, t2.left);
t1.right = mergeTrees(t1.right, t2.right);
return t1;
}  

非递归方法

public class Solution {
public TreeNode mergeTrees(TreeNode t1, TreeNode t2) {
if (t1 == null)
return t2;
Stack < TreeNode[] > stack = new Stack < > ();
stack.push(new TreeNode[] {t1, t2});
while (!stack.isEmpty()) {
TreeNode[] t = stack.pop();
if (t[0] == null || t[1] == null) {
continue;
}
t[0].val += t[1].val;
if (t[0].left == null) {
t[0].left = t[1].left;
} else {
stack.push(new TreeNode[] {t[0].left, t[1].left});
}
if (t[0].right == null) {
t[0].right = t[1].right;
} else {
stack.push(new TreeNode[] {t[0].right, t[1].right});
}
}
return t1;
}
}

  

771. Jewels and Stones

  https://www.cnblogs.com/zhacai/p/10429670.html  easy  

  

  

7.Reverse Integer  

Given a 32-bit signed integer, reverse digits of an integer.

Example 1:

Input: 123
Output: 321

Example 2:

Input: -123
Output: -321

Example 3:

Input: 120
Output: 21

Note:Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−231,  231 − 1]. For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

public int reverse(int x) {
String str= String.valueOf(x);
StringBuffer strb;
int re=0;
if('-'==str.charAt(0)){
strb = new StringBuffer(str.substring(1,str.length())).reverse();
strb.insert(0,"-");
}
else {
strb= new StringBuffer(str).reverse();
}
try{
re =Integer.valueOf(strb.toString());
}catch (Exception e){ }
return re;
}  

使用数学方法的优解

public int reverse(int x) {
int re = 0;
while(0!=x){
int temp = re * 10 + x % 10;
if(temp / 10 != re)//如果整数不溢出显然相等
return 0;
re = temp;
x /= 10;
}
return re;
}

  

 

LeetCode Top100 Liked Questions的更多相关文章

  1. LeetCode Top Interview Questions

    LeetCode Top Interview Questions https://leetcode.com/problemset/top-interview-questions/ # No. Titl ...

  2. leetcode TOP100 两数相加

    两数相加 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表 ...

  3. leetcode TOP100 字母异位词分组

    字母异位词分组 给定一个字符串数组,将字母异位词组合在一起.字母异位词指字母相同,但排列不同的字符串. 思路: 一个map,将每个字符串字符进行记数,字符作为map的key,次数初始为零,以此来标识字 ...

  4. leetcode TOP100 比特位计数

    338. 比特位计数 题目描述: `给定一个非负整数 num.对于 0 ≤ i ≤ num 范围中的每个数字 i ,计算其二进制数中的 1 的数目并将它们作为数组返回. 示例 1: 输入: 2 输出: ...

  5. 214. Shortest Palindrome

    题目: Given a string S, you are allowed to convert it to a palindrome by adding characters in front of ...

  6. [LeetCode] questions conclusion_ Binary Search

    Binary Search T(n) = T(n/2) + O(1)   =>    T(n) = O(lg n) proof: 如果能用iterable , 就用while loop, 可以防 ...

  7. [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal

    Pre: node 先,                      Inorder:   node in,           Postorder:   node 最后 PreOrder Inorde ...

  8. [LeetCode] All questions numbers conclusion 所有题目题号

    Note: 后面数字n表明刷的第n + 1遍, 如果题目有**, 表明有待总结 Conclusion questions: [LeetCode] questions conclustion_BFS, ...

  9. [LeetCode] questions conclusion_ Dynamic Programming

    Questions: [LeetCode] 198. House Robber _Easy tag: Dynamic Programming [LeetCode] 221. Maximal Squar ...

随机推荐

  1. 阿里云CentOS7挂载SSD云盘的方法

    https://bbs.aliyun.com/read/151152.html 阿里云购买的第2块云盘默认是不自动挂载的,需要手动配置挂载上. 1.查看SSD云盘 sudo fdisk -l Disk ...

  2. Android学习之BitMap用法实例

    下面简单说明了BitMap的用法: 从服务器下载一张图片,显示在ImageView控件上,并将该图片保存在移动设备的SD上. // 根据网络URL获取输入流 public InputStream ge ...

  3. intellij中使用git插件将项目上传到码云

    参考帖子: git上传本地Intellij idea 项目到码云的git仓库中(评论中有彩蛋,一定要看) IntelliJ-IDEA和Git.GitHub.Gitlab的使用 使用idea上传项目到码 ...

  4. drizzleDumper的原理分析和使用说明

    https://blog.csdn.net/qq1084283172/article/details/53561622 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog. ...

  5. VC下遍历文件夹中的所有文件的几种方法

    一.使用::FindFirstFile和::FindNextFile方法 #include "StdAfx.h" #include <windows.h> #inclu ...

  6. 关于PHP中的 serialize () 和 unserialize () 的使用(即关于PHP中的值与已存储的表示的相互转换)

    有时,我们会碰到这样的数据(字符串) 1 a:3:{i:0;s:44:"/Uploads/images/2017-07-21/5971a9a08ad57.png";i:1;s:44 ...

  7. JAVA知多少

    读<java解惑>感觉有意思的就记录一下. 1.判断奇数还是偶数 public boolean isOdd(int i){ return i%2==1; }; 这样子看起来很对,但是考虑到 ...

  8. Android 验证APK是否已经签名或是否是Debug签名

    https://source.android.google.cn/ http://www.android-doc.com/tools/publishing/app-signing.html Signi ...

  9. Elasticsearch学习之深入聚合分析一---基本概念

    首先明白两个核心概念:bucket和metric 1. bucket:一个数据分组 city name 北京 小李 北京 小王 上海 小张 上海 小丽 上海 小陈 基于city划分buckets,划分 ...

  10. Sencha Touch 实战开发培训 视频教程 第二期 基础提高篇 预告

    “抛砖网”国内首家首创纯实战型培训机构,提供在线培训.技术指导及答疑! 团队通过360全方位技术培训+1度手把手技术指导,保证每一个学员能最快掌握实际工作技能: 让每一个学员都能站在我们的肩膀上,展翅 ...