leetcode

Leet爬楼梯问题

本秂侑毒 提交于 2019-12-06 22:32:46
假设你正在爬楼梯。需要 n 阶你才能到达楼顶。 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢? 注意:给定 n 是一个正整数。 示例 1: 输入: 2 输出: 2 解释: 有两种方法可以爬到楼顶。 1. 1 阶 + 1 阶 2. 2 阶 示例 2: 输入: 3 输出: 3 解释: 有三种方法可以爬到楼顶。 1. 1 阶 + 1 阶 + 1 阶 2. 1 阶 + 2 阶 3. 2 阶 + 1 阶 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/climbing-stairs 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 * * @lc app=leetcode.cn id=70 lang=java * * [70] 爬楼梯 * 解题思路:假设楼梯数为 n,我们有 an 种方法可以爬到楼顶 * 1. 当 n=1时,a1=1; * 2. 当 n=2时,方法有(1+1; 2),a2=2; * 3. 当 n=3时,方法有(1+1+1; 2+1; 1+2),a3=3; * 4. 当 n=4时,方法有(1+1+1+1; 2+1+1; 1+2+1; 1+1+2; 2+2),a4=5; * 5. 我们来总结一下:不妨设a0=1,则a2=a0+a1; a3=a1+a2; a4=a2+a3; ...

字符串数组中两个字符的最短距离

偶尔善良 提交于 2019-12-06 18:07:52
[leetcode] https://leetcode.com/problems/shortest-word-distance/ For example, Assume that words = ["practice", "makes", "perfect", "coding", "makes"] . Given word1 = “coding” , word2 = “practice” , return 3. Given word1 = "makes" , word2 = "coding" , return 1. 需要注意: 该数组中得字符串有可能出现多次,比如例子中的makes; 这个问题比较简单,按照下面的步骤可以简单的做出来: 记录给定两个字符串出现的位置,会得到两个数组;假设为a和b; 然后计算abs(a[i] - b[j])的最小值即可; 第一步扫描一遍字符串数组即可;第二步,如果简单的用两层循环来做,因为a和b的长度都有可能是n(比如各占一半),那么用O(n * n)的时间; 事实上,有意思的地方就在于,第二步可以在线性时间内解出来;这里有一个隐藏的特性,扫描原数组的时候得到的两个字符的位置数组,是有序的;比如数组 [a, b, a, b, b, a], 那么a的位置数组为[0, 2, 5], b的为[1, 3, 4]; 假设我们比较到了位置2和位置3

[Leetcode] Sliding Window Summary

喜夏-厌秋 提交于 2019-12-06 13:09:27
Template from https://leetcode.com/problems/minimum-window-substring/discuss/26808/Here-is-a-10-line-template-that-can-solve-most-'substring'-problems For most substring problem, we are given a string and need to find a substring of it which satisfy some restrictions. A general way is to use a hashmap assisted with two pointers. The template is given below. int findSubstring(string s){ vector<int> map(128,0); int counter; // check whether the substring is valid int begin=0, end=0; //two pointers, one point to tail and one head int d; //the length of substring for() { /* initialize the hash map

LeetCode:Second Highest Salary

你。 提交于 2019-12-06 12:02:14
1、题目名称 Second Highest Salary(第二高的工资) 2、题目地址 https://leetcode.com/problems/second-highest-salary/ 3、题目内容 现在有一张记录了Id(主键)和Salary(工资)的表,求出其中第二高的工资。如果不存在第二高的工资,返回null。 +----+--------+ | Id | Salary | +----+--------+ | 1 | 100 | | 2 | 200 | | 3 | 300 | +----+--------+ 4、初始化数据库脚本 在MySQL数据库中建立一个名为LEETCODE的数据库,用MySQL命令行中的source命令执行下面脚本: -- 执行脚本前必须建立名为LEETCODE的DATABASE USE LEETCODE; DROP TABLE IF EXISTS Employee; CREATE TABLE Employee ( Id INT NOT NULL PRIMARY KEY, Salary INT ); INSERT INTO Employee (Id, Salary) VALUES (1, 100); INSERT INTO Employee (Id, Salary) VALUES (2, 200); INSERT INTO Employee (Id

LeetCode:Department Highest Salary

本秂侑毒 提交于 2019-12-06 12:01:48
1、题目名称 Department Highest Salary(部门内最高工资) 2、题目地址 https://leetcode.com/problems/department-highest-salary/ 3、题目内容 表Employee包括四列:Id、Name、Salary、DepartmentId +----+-------+--------+--------------+ | Id | Name | Salary | DepartmentId | +----+-------+--------+--------------+ | 1 | Joe | 70000 | 1 | | 2 | Henry | 80000 | 2 | | 3 | Sam | 60000 | 2 | | 4 | Max | 90000 | 1 | +----+-------+--------+--------------+ 表Department表中包括了公司的所有部门 +----+----------+ | Id | Name | +----+----------+ | 1 | IT | | 2 | Sales | +----+----------+ 现要求写一个SQL,求出某一部门内领取最高工资的员工。 +------------+----------+--------+ | Department

LeetCode:Ugly Number II

冷暖自知 提交于 2019-12-06 09:18:45
1、题目名称 Ugly Number II(丑数2:找出第n个丑数) 2、题目地址 https://leetcode.com/problems/ugly-number-ii/ 3、题目内容 英文:Write a program to find the n -th ugly number. 中文:写程序找出第n个丑数 说明:丑数具有如下特征:1是丑数,丑数可以表示为有限个2、3、5的乘积 注意:关于“判断指定数字是否为丑数”,请参考 Ugly Number(LeetCode #263) 5、一个TLE的方法 一个最容易想到的方法,就是从1开始依次向后判断各自然数是否为丑数,一直判断到第n个丑数,返回第n个丑数的值。这种方法由于充斥了大量的重复计算,效率很低,因此会导致TLE(Time Limit Exceeded)的结果。 Java代码如下: /** * 功能说明:LeetCode 263 - Ugly Number * 开发人员:Tsybius2014 * 开发时间:2015年8月23日 */ public class Solution { /** * 求第N个丑数 * @param n * @return 第N个丑数 */ public int nthUglyNumber(int n) { if (n <= 1) { return 1; } int counter = 0;

LeetCode:Ugly Number

不想你离开。 提交于 2019-12-06 09:18:31
1、题目名称 Ugly Number(丑数1:判断指定数字是否为丑数) 2、题目地址 https://leetcode.com/problems/ugly-number 3、题目内容 英文:Write a program to check whether a given number is an ugly number. 中文:写程序判断指定数字是否为丑数 说明:丑数具有如下特征:1是丑数,丑数可以表示为有限个2、3、5的乘积 注意:本题的目标不是“判断第N个丑数”,关于“判断第N个丑数”,请参考 Ugly Number II(LeetCode #264) 4、解题方法 根据丑数的定义,可以通过如下方法判断一个数字n是否为丑数:可以试着用2、3、5不断整除n,当n不能再被2、3、5整除时,判断n是否等于1,等于1则指定的数字是丑数(返回真),否则不是(返回假)。 Java代码如下: /** * 功能说明:LeetCode 263 - Ugly Number * 开发人员:Tsybius2014 * 开发时间:2015年8月23日 */ public class Solution { /** * 判断数字是否为丑数 * @param num 被判断数字 * @return true:丑数,false:非丑数 */ public boolean isUgly(int num) { if

LeetCode:Super Ugly Number

ⅰ亾dé卋堺 提交于 2019-12-06 09:17:57
1、题目名称 Super Ugly Number(超级丑数) 2、题目地址 https://leetcode.com/problems/super-ugly-number/ 3、题目内容 英文: Write a program to find the nth super ugly number. Super ugly numbers are positive numbers whose all prime factors are in the given prime list primes of size k . For example, [1, 2, 4, 7, 8, 13, 14, 16, 19, 26, 28, 32] is the sequence of the first 12 super ugly numbers given primes = [2, 7, 13, 19] of size 4. 中文:写程序找出第n个超级丑数 说明:超级丑数具有如下特征:1是超级丑数,超级丑数可以表示为有限个指定质数的乘积 注意: 1)丑数的定义是:1是丑数,丑数可以表示为有限个2、3、5的乘积,超级丑数是对丑数概念的一个扩展 2)关于“判断指定数字是否为丑数”,请参考 Ugly Number(LeetCode #263) 3)关于“找到第n个丑数”,请参考 Ugly Number II

leetcode top100 刷题(位运算)

末鹿安然 提交于 2019-12-06 07:22:05
目录 leetcode top100 刷题(位运算) 78. 子集 136. 只出现一次的数字 169. 多数元素 338. 比特位运算 461.汉明距离 leetcode top100 刷题(位运算) 78. 子集 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []] 来源:力扣(LeetCode) 链接: https://leetcode-cn.com/problems/subsets 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 这个就是代数里面的幂集,首先考虑代数的方法。对于一个有 \(n\) 个数的列表,其幂集大小为 \(2^n\) 。我们可以采取这样的思路,即首先将这 \(2^n\) 个数字在列表中呈现出来,然后再将每个数字按照2进制位数为0或者1映射到子集中去,然后再将子集添加到幂集中。具体考虑映射的时候,我们先将每个数字转化为2进制,随后将二进制数字转化为字符列表,然后反转来补全‘1’,再然后继续再翻转(其实可以去掉这个翻转,不影响结果,纯粹为了数学上好看),然后将原集合映射到子集中去。 class Solution: def

git - IDEA

南笙酒味 提交于 2019-12-06 07:04:16
Annotate 在我本地的项目 java-demo 中是建立了一个 git 仓库,每次提交代码都会在里面建立了一个快照,使用 IDEA 打开这个项目,在代码行处右键打开 Annotate 选项就看到对这份文件每次修改的代码和时间戳,点击里面的某一行还能看到提交这个 commit 的同时整个项目有哪些文件进行修改。 使用 git checkout [SHA-1] 可以返回某个状态的 commit。 接下来我做了一些实验: # 第一次,查看 git log ~/leetcode (master) $ git log commit ce96adc13c17d6cb58c8d9574569a311d1bc7d38 (HEAD -> master, origin/master) # 回到之前提交的某一个 commit ~/leetcode (master) $ git checkout bd25 Note: checking out 'bd25'. # 第二次,查看可知,已经跳到了 'bd25' 的 commit 了,此时 commit 只有 HEAD 指针 ~/leetcode ((bd2512a...)) $ git log commit bd2512a86fe3da8ddfa6a35db9a105e950fbd83d (HEAD) # 回到 master 所在的 commit 编号