
Leetcode
God_Mood
0x00: 勤学如春起之苗,不见其增,日有所长。
辍学如磨刀之石,不见其损,日有所亏。
0x01: 学习一种新技术最好且最快的方式就是
去官网看文档
展开
-
197. 上升的温度
题目链接:https://leetcode-cn.com/problems/rising-temperature/给定一个 Weather 表,编写一个 SQL 查询,来查找与之前(昨天的)日期相比温度更高的所有日期的 Id。+---------+------------------+------------------+| Id(INT) | RecordDate(DATE) | Te...原创 2019-10-30 10:37:36 · 217 阅读 · 0 评论 -
196. 删除重复的电子邮箱
题目链接:https://leetcode-cn.com/problems/delete-duplicate-emails/编写一个 SQL 查询,来删除Person表中所有重复的电子邮箱,重复的邮箱里只保留Id最小的那个。+----+------------------+| Id | Email |+----+------------------+|...原创 2019-10-30 10:02:56 · 460 阅读 · 0 评论 -
184. 部门工资最高的员工
题目链接:https://leetcode-cn.com/problems/department-highest-salary/Employee 表包含所有员工信息,每个员工有其对应的Id, salary 和 department Id。+----+-------+--------+--------------+| Id | Name | Salary | DepartmentId ...原创 2019-10-29 17:56:05 · 195 阅读 · 0 评论 -
183. 从不订购的客户
题目链接:https://leetcode.com/problems/customers-who-never-order/某网站包含两个表,Customers 表和 Orders 表。编写一个 SQL 查询,找出所有从不订购任何东西的客户。Customers 表:+----+-------+| Id | Name |+----+-------+| 1 | Joe || ...原创 2019-10-29 17:03:06 · 184 阅读 · 0 评论 -
182. 查找重复的电子邮箱
题目链接:https://leetcode-cn.com/problems/duplicate-emails/编写一个 SQL 查询,查找Person 表中所有重复的电子邮箱。示例:+----+---------+| Id | Email |+----+---------+| 1 | a@b.com || 2 | c@d.com || 3 | a@b.com |...原创 2019-10-29 16:49:27 · 290 阅读 · 0 评论 -
181. 超过经理收入的员工
题目链接:https://leetcode-cn.com/problems/employees-earning-more-than-their-managers/Employee表包含所有员工,他们的经理也属于员工。每个员工都有一个 Id,此外还有一列对应员工的经理的 Id。+----+-------+--------+-----------+| Id | Name | Sala...原创 2019-10-29 16:19:31 · 163 阅读 · 0 评论 -
180. 连续出现的数字
题目链接:https://leetcode-cn.com/problems/consecutive-numbers/编写一个 SQL 查询,查找所有至少连续出现三次的数字。+----+-----+| Id | Num |+----+-----+| 1 | 1 || 2 | 1 || 3 | 1 || 4 | 2 || 5 | 1 || 6 |...原创 2019-10-29 15:57:46 · 177 阅读 · 0 评论 -
178. 分数排名
题目链接:https://leetcode.com/problems/rank-scores/编写一个 SQL 查询来实现分数排名。如果两个分数相同,则两个分数排名(Rank)相同。请注意,平分后的下一个名次应该是下一个连续的整数值。换句话说,名次之间不应该有“间隔”。+----+-------+| Id | Score |+----+-------+| 1 | 3.50 |...原创 2019-10-29 15:20:54 · 230 阅读 · 0 评论 -
177. 第N高的薪水
https://leetcode.com/problems/nth-highest-salary/submissions/编写一个 SQL 查询,获取 Employee 表中第n高的薪水(Salary)。+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 ...原创 2019-10-29 14:31:48 · 200 阅读 · 0 评论 -
176. 第二高的薪水
题目链接:https://leetcode.com/problems/second-highest-salary/编写一个 SQL 查询,获取 Employee表中第二高的薪水(Salary)。+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 |+...原创 2019-10-29 11:37:33 · 204 阅读 · 0 评论 -
224. Basic Calculator(基本计算器)
https://leetcode.com/problems/basic-calculator/submissions/实现一个基本的计算器来计算一个简单的字符串表达式的值。字符串表达式可以包含左括号(,右括号),加号+,减号-,非负整数和空格。示例 1:输入: "1 + 1"输出: 2示例 2:输入: " 2-1 + 2 "输出: 3示例 3:输入:...原创 2019-10-20 17:15:24 · 230 阅读 · 0 评论 -
223. Rectangle Area(矩形面积)
https://leetcode.com/problems/rectangle-area/在二维平面上计算出两个由直线构成的矩形重叠后形成的总面积。每个矩形由其左下顶点和右上顶点坐标表示,如图所示。示例:输入: -3, 0, 3, 4, 0, -1, 9, 2输出: 45class Solution { public int computeArea(in...原创 2019-10-20 16:15:52 · 406 阅读 · 0 评论 -
221. Maximal Square(最大正方形)
题目链接:https://leetcode.com/problems/maximal-square/在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。示例:输入:1 0 1 0 01 0 1 1 11 1 1 1 11 0 0 1 0输出: 4class Solution { public int maximalSq...原创 2019-10-20 15:42:06 · 143 阅读 · 0 评论 -
222. Count Complete Tree Nodes(完全二叉树的节点个数)
题目链接:https://leetcode.com/problems/count-complete-tree-nodes/给出一个完全二叉树,求出该树的节点个数。说明:完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~2h个节点。示例:输入:...原创 2019-10-20 15:40:51 · 233 阅读 · 0 评论 -
存在重复元素(I II III)小结
217. 存在重复元素给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。示例 1:输入: [1,2,3,1]输出: true示例 2:输入: [1,2,3,4]输出: false示例3:输入: [1,1,1,3,3,4,3,2,4,2]输出: true解法1:用Ha...原创 2019-10-13 16:55:50 · 409 阅读 · 0 评论 -
Leetcode:3. 无重复字符的最长子串
//这道题我花了一晚上才把它调试通,心疼自己的智商一秒,关键部分在于Start的更新时我忽略了一种情况,导致很多次不能提交WTF。class Solution { public int lengthOfLongestSubstring(String s) { int n=s.length(),ans=0; int i,start; Map&l...原创 2018-06-11 22:28:47 · 123 阅读 · 0 评论 -
4. 两个排序数组的中位数
class Solution {//和归并排序中的”并“思想一样 public double findMedianSortedArrays(int[] nums1, int[] nums2) { double ans; int i=0,j=0,k=0; int n1=nums1.length,n2=nums2.length; ...原创 2018-06-11 23:21:13 · 166 阅读 · 0 评论 -
5. 最长回文子串
class Solution { public String longestPalindrome(String s) { int n=s.length(); int [][] dp=new int[n][n]; int i,j,maxlen=0,start=0,end=0; for(i=0;i<n;i++) for(j=i;j<n;j++) ...原创 2018-06-13 14:21:13 · 140 阅读 · 0 评论 -
10. 正则表达式匹配(捷径Java版)
import java.util.regex.Matcher;import java.util.regex.Pattern;class Solution { public boolean isMatch(String s, String p) { Pattern pattern = Pattern.compile(p); Matcher m = pattern....原创 2018-06-14 12:59:58 · 750 阅读 · 0 评论 -
11. 盛最多水的容器
方法一:暴力求解On2,超时不通过。public class test1 { public int maxArea(int[] height) { int len=height.length; int i,j,maxS=0; for(i=0;i<len;i++) for(j=i+1;j<len;j++) { maxS=Math.max(maxS, (heig...原创 2018-06-14 23:19:12 · 148 阅读 · 0 评论 -
17. 电话号码的字母组合
class Solution {//回溯思想,一直Try,到头了再返回 public List<String> letterCombinations(String digits) { StringBuilder sb = new StringBuilder(); List<String> res = new ArrayList<String&...原创 2018-06-15 20:31:29 · 164 阅读 · 0 评论 -
22. 括号生成
class Solution {//很好理解,我就不解释了 public List<String> generateParenthesis(int n) { List<String> res = new ArrayList<String>(); if(n==0) return res; backtrack("",re...原创 2018-06-15 21:23:20 · 337 阅读 · 0 评论 -
合并K个排序链表
class Solution { public ListNode mergeKLists(ListNode[] lists) { if(lists.length==0||lists==null) return null; if(lists.length==1) return lists[0]; int j = lists.length - 1; in...原创 2018-06-15 22:25:55 · 147 阅读 · 0 评论 -
:LeetCode-2.两个列表相加Add Two Numbers --Java(错误与正确两种)
第一种:只能通过部分(Long范围内的案例,超出Long的结果会报错,估计出题人并不想让你走捷径23333)/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } *...原创 2018-06-10 21:02:52 · 309 阅读 · 0 评论 -
二叉树层次遍历(Java版)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */class Solution { pub...原创 2018-09-23 16:32:06 · 3413 阅读 · 0 评论 -
变态青蛙跳台阶
第一种,青蛙跳一阶或两阶,输入Npublic class Solution { public int JumpFloor(int target) { return F(target); } public static int F(int n) { if(n==0) return 0; if(n==1) return 1; if(...原创 2018-09-23 23:35:29 · 145 阅读 · 0 评论 -
二进制中一的个数(移位实现,易错点)
//最初想错了,while(n!=0)代替for(int i=0;i<32;i++)这样是不行的,因为负数用补码的形式转换,比如-3的补码右移一位相当于-3/2向下取整,依次为-2,-1,0,0.......所以还得老老实实用32位int型来操作。public class Solution { public int NumberOf1(int n) { int ...原创 2018-09-24 01:06:24 · 360 阅读 · 0 评论 -
给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值。(java)
leetcode 530.给定一个所有节点为非负值的二叉搜索树,求树中任意两节点的差的绝对值的最小值。注意: 树中至少有2个节点。一个二叉搜索树的中序遍历结果是从小到大有序排列的,用一个栈来保存。/** * Definition for a binary tree node. * public class TreeNode { * int val; * Tre...原创 2018-09-25 17:26:39 · 1255 阅读 · 0 评论 -
复杂链表的复制(辅助存储避免原址引用)
刚看到这个题目的时候想到的是利用三个队列进行存储三个域(一个队列也行),我当时第一次写出的代码如下:import java.util.ArrayList;import java.util.LinkedList;import java.util.List;import java.util.Stack; class RandomListNode { int label; ...原创 2018-09-30 23:15:50 · 227 阅读 · 0 评论 -
输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)
最开始的思路是选择对二叉树进行三种遍历+层次遍历中的一种,然后保存节点到一个ArrayList中,在用containsAll方法。后来提交后才发现,题目问的是子结构,不是子树。于是选择了网上常见的递归式依次判断。当一个root2的值为Null时,说明这个结构的某一条路已经通了(及root1中也存在这一条路),故返回True。代码如下:public class Solution { ...原创 2018-09-26 01:11:07 · 480 阅读 · 0 评论 -
字符串全排列(性能分析Java版)
具体的思路我就不写了,借用网上的一张图来表示:我的代码如下:import java.util.*;public class Solution { public ArrayList<String> Permutation(String str) { ArrayList<String> ls=new ArrayList<String...原创 2018-10-02 17:10:38 · 454 阅读 · 0 评论 -
丑数(两种方法)
方法一:最初写的时候只想到了这一种,最多的优化也就是 i*2,后来想想还是练的少,没有思路。先把测试程序贴出来public class Solution { public static void main(String[] args) { //Scanner sc = new Scanner(System.in); for(int i=1;i<1010;i++) { ...原创 2018-10-05 00:38:09 · 293 阅读 · 0 评论 -
重建二叉树
//Debug了好久,结果发现原因是当前节点没初始化。。。郁闷啊public static TreeNode BuiltBinaryTree(int[] pre,int[] in) { TreeNode root=null; if(pre.length==0||in.length==0) { return root; } int i,j,flag=0; while(...原创 2018-09-22 15:58:57 · 141 阅读 · 0 评论 -
两个栈实现队列&&两个队列实现栈
//首先两个栈实现队列import java.util.Stack;public class Solution { Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); publ...原创 2018-09-22 17:17:55 · 155 阅读 · 0 评论 -
二叉搜索树的后序遍历序列
输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。本来的思路是找到大于小于root的两个分界点,然后让midleft+1==midright;后来发现这种思路有BUG,如果不存在分界点的话那个等式不成立。于是改为从后往前遍历,找到临界点,然后临界点之前的数字必须满足<array[root]。然后递...原创 2018-09-30 12:30:26 · 108 阅读 · 0 评论 -
序列化二叉树
请实现两个函数,分别用来序列化和反序列化二叉树思路是看大佬们的文章学到的,然后整理思路自己敲一遍。无论采用何种遍历方式序列化二叉树,反序列化时采用相同的方式即可,不过先序遍历的方式来反序列化比较简单。/*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = nu...原创 2019-01-31 18:15:22 · 133 阅读 · 0 评论 -
二叉搜索树第K小节点
/*public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; }}*/public class Solution { priva...原创 2019-01-31 19:48:21 · 221 阅读 · 0 评论 -
数据流的中位数(Java版)
用Java自带的小顶堆PriorityQueue,改写Comparator的compare方法后可实现大顶堆。注意大顶堆与小顶堆的元素差距不能超过1.import java.util.*;public class Solution { PriorityQueue<Integer> minHeap=new PriorityQueue<Integer>();...原创 2019-02-01 11:23:06 · 546 阅读 · 0 评论 -
滑动窗口的最大值(Java版)
此处用到的数据结构是双端队列(详见百度)双端队列里面存放的是数组中的下标,er而不是数组中元素的值。import java.util.*;public class Solution { public ArrayList<Integer> maxInWindows(int [] num, int size) { ArrayList<Int...原创 2019-02-01 19:50:37 · 445 阅读 · 0 评论 -
链表中环的入口结点(两种方法)
1 . 断链法public class Solution { public ListNode EntryNodeOfLoop(ListNode pHead) { if(pHead==null||pHead.next==null) return null; ListNode pre=pHead; List...原创 2019-01-28 22:23:35 · 813 阅读 · 1 评论