本文记录个人刷题记录

推荐两个刷题网站:

地址:https://leetcode.com/

另外一个地址:http://www.lintcode.com/

1.Write a SQL query to get the second highest salary from the Employee table.

题目地址:https://leetcode.com/problems/second-highest-salary/

+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+

For example, given the above Employee table, the second highest salary is 200.
If there is no second highest salary, then the query should return null.

解:

先算出所有员工中最高的工资,然后在从非最高工资中的记录,取最大的一个,就是第二高工资了

select max(Salary) as 'SecondHighestSalary'
from Employee where Salary<(select max(Salary) from Employee)

2.Given an array of integers, return indices of
the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution.

地址:https://leetcode.com/problems/two-sum/

Example:

Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

解:

两层遍历,外层遍历是负责遍历每个元素,内层遍历是用于把当前 外层循环去取出的数与除它之外的元素相加,看是否等于目标数值,如果等于,就返回当前两个元素的索引,如果都没有命中,就返回空数组

public class Solution {
public int[] twoSum(int[] nums, int target) {
for(int i=0 ;i<nums.length;i++){
for(int j=i+1;j<nums.length;j++)
if (nums[i] + nums[j] == target) {
return new int[]{
i,j
};
}
}
return new int[]{};
}
}

3.Combine Two Tables

Table: Person

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.

Table: Address

+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an
address for each of those people:

FirstName, LastName, City, State

解:

两个表通过PersonId关联查询即可,但是因为需要有所有Person表记录,所以这里要用leftjoin

SELECT pes.firstName as 'FirstName',
pes.LastName as 'LastName',
adr.City as 'City',
adr.State as 'State'
FROM Person pes
LEFT JOIN Address adr
on pes.personid = adr.personid;

Leetcode | 刷题日记(1)的更多相关文章

  1. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  2. leetcode 刷题进展

    最近没发什么博客了 凑个数 我的leetcode刷题进展 https://gitee.com/def/leetcode_practice 个人以为 刷题在透不在多  前200的吃透了 足以应付非算法岗 ...

  3. LeetCode刷题指南(字符串)

    作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7% ...

  4. leetcode刷题记录--js

    leetcode刷题记录 两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但 ...

  5. LeetCode刷题总结之双指针法

    Leetcode刷题总结 目前已经刷了50道题,从零开始刷题学到了很多精妙的解法和深刻的思想,因此想按方法对写过的题做一个总结 双指针法 双指针法有时也叫快慢指针,在数组里是用两个整型值代表下标,在链 ...

  6. Leetcode刷题记录(python3)

    Leetcode刷题记录(python3) 顺序刷题 1~5 ---1.两数之和 ---2.两数相加 ---3. 无重复字符的最长子串 ---4.寻找两个有序数组的中位数 ---5.最长回文子串 6- ...

  7. LeetCode刷题总结-数组篇(上)

    数组是算法中最常用的一种数据结构,也是面试中最常考的考点.在LeetCode题库中,标记为数组类型的习题到目前为止,已累计到了202题.然而,这202道习题并不是每道题只标记为数组一个考点,大部分习题 ...

  8. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  9. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

随机推荐

  1. 如何修改eclipse中Dynamic web module的 version

    我们直接在eclipse中修改Dynamic Web Module的话会报错,改不了的 所以我们可以找到项目文件中的.setting文件下的org.eclipse.wst.common.project ...

  2. Ubuntu 18.04安装搜狗拼音

    首先安装fcitx 一.检测是否安装fcitx 首先检测是否有fcitx,因为搜狗拼音依赖fcitx > fcitx 提示: 程序“fcitx”尚未安装. 您可以使用以下命令安装: > s ...

  3. 洛谷$P$1486 郁闷的出纳员 $[NOI2004]$ $splay$

    正解:$splay$ 解题报告: 传送门! 依然先考虑要呲呲些什么操作鸭$QwQ$ 其实就只要一个删除区间,一个查询第$k$大,还一个插入就欧克? 删除区间的话直接旋转下根什么的然后直接把子树删了就好 ...

  4. 淘淘购物网Ⅱ——SSM架构搭建

    课程计划 1.SSM框架整合 2.mybatis逆向工程 3.整合测试 4.Debug调试 SSM框架整合 前后台所用的技术 框架:Spring + SpringMVC + Mybatis 前端:Ea ...

  5. flask 中的 werkzeug Local,LocalStack 和 LocalProxy 技术应用

    什么是 Local wsgi 每次请求,会把过程进行抽离无状态话,过程数据存储在本次请求的全局变量中,使用到了Local. Local 作为每次请求的全局命令空间,属于每次请求的私有 LocalSta ...

  6. play framework 相关

    1.下载 官网下载解压,安装有jkd即可使用 2.helloworld $ activator new my-first-app play-java https://www.playframework ...

  7. Selenium的简单使用

    selenium的使用对于新手来说十分友好,因为他避开了如今网络中的异步加载抓取的困扰,使得我们大部分的时间可以用于提取信息和存储中,下面就简单的列一些使用的代码,希望给同样初学的你有一定的参考价值. ...

  8. 7.JavaSE之类型转换

    类型转换: 由于Java是强类型语言,所以要进行运算的时候,需要用到类型转换. 图中优先级从低到高,小数优先级大于整数. 运算中,不同类型的数据需要转换为同一类型,然后进行运算. 强制类型转换:(类型 ...

  9. 玩转Django2.0---Django笔记建站基础七(表单与模型)

    第七章 表单与模型 表单是搜集用户数据信息的各种表单元素的集合,作用是实现网页上的数据交互,用户在网站输入信息,然后提交到网站服务器端进行处理(如数据录入和用户登录.注册等). 用户表单是web开发的 ...

  10. dotnet restore 初次运行 这个 指令 会安装 特别多的 4.0.0 或者 4.1 的 rc2-24027的 东东 这些东西。