leetcode

【LeetCode】377.Combination Sum IV(Medium)解题报告

我只是一个虾纸丫 提交于 2019-11-29 08:02:51
【LeetCode】377.Combination Sum IV(Medium)解题报告 题目地址: https://leetcode.com/problems/combination-sum-iv/description/ 题目描述:   Given an integer array with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example: nums = [ 1 , 2 , 3 ] target = 4 The possible combination ways are: ( 1 , 1 , 1 , 1 ) ( 1 , 1 , 2 ) ( 1 , 2 , 1 ) ( 1 , 3 ) ( 2 , 1 , 1 ) ( 2 , 2 ) ( 3 , 1 ) Note that different sequences are counted as different combinations. Therefore the output is 7. Follow up: What if negative numbers are allowed in the given array

【LeetCode】40.Combination Sum II(Medium)解题报告

廉价感情. 提交于 2019-11-29 08:01:38
【LeetCode】40.Combination Sum II(Medium)解题报告 题目地址: https://leetcode.com/problems/combination-sum-ii/description/ 题目描述:   Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.   Each number in C may only be used once in the combination.   Note:   All numbers (including target) will be positive integers.   The solution set must not contain duplicate combinations.   For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,   A solution set is: [[1, 7],[1, 2, 5],[2, 6],[1, 1, 6]]   这道题要求数字不可以多次利用

LeetCode 137. 只出现一次的数字 II

不问归期 提交于 2019-11-29 06:07:58
#LeetCode 137. 只出现一次的数字 II 题目链接 我的掘金地址: https://juejin.im/post/5cb962dbf265da03761e8601 这题起初根本想不到.直到翻了leetcode.com的discuss…然后我整个人都"awesome!!!"地瞎叫了. 下面讲讲我学来的思路. 这题本质上来讲,就是让我们做一个有限状态机(finite state machine)(瞎翻的). 首先 : 一般看到这种题,我们第一反应是做状态记录,比如数字x出现了y次.然后从记录里查询出现了一次的数字.但是现在我们的空间复杂度被限制成了O(1),不可能对所有的64位二进制表示的整数进行统计次数. 于是 我们只能着眼于二进制上了,我们先将状态记录缩小到记录目标数字的每一个 二进制 上.那么这题就会变成遍历所有数,一个二进制位出现 1 的次数对 3 取余是否为 1 ,若为 1 ,那么我们目标数字的这一位也就是 1 . 比如 [1,1,1,2] 他们转换成二进制就是[01,01,01,10],我们可以知道第一位(从右往左)的二进制总共出现了三次1,那么我们的目标结果在第一位就绝对不会是1,相对的第二位出现了一次1,那么我们的目标值在这一位就是1 实际上 这题我们也可以写64个变量(不知道数据量范围多大或者32位也可以?)记录每位的二进制1出现的次数

(LeetCode) First Unique Character in a String - LeetCode

不问归期 提交于 2019-11-29 05:55:15
Description: Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You may assume the string contain only lowercase letters. Accepted 310,115 Submissions 613,252 Solution: Attempt 1: Failed : Timeout Exception: class Solution { public int firstUniqChar(String s) { if(s==null||s.length()==0){ return -1; } for(int i = 0; i<s.length(); i++){ if(Count(s, s.charAt(i))==1){ return i ; } } return -1; } static int Count(String s, char a){ int count = 0; for(int i = 0; i

工具推荐--刷LeetCode的神器

吃可爱长大的小学妹 提交于 2019-11-29 04:59:30
本文首发于微信公众号:【坂本先生】,文章地址为: https://mp.weixin.qq.com/s/vHv5hO8nils_g2VSKwu1Cg 如有转载请标明出处 今天给大家安利一款快速刷LeetCode的工具,能够让你专注于题目本身,而不是如何去建立题目。这个工具是基于IDEA的,名叫LeetCode Editor,它的官方GitHub地址为:https://github.com/shuzijun/leetcode-editor 至于为什么要刷LeetCode,嘿嘿嘿,当然是为了应付笔试啦,当然这也是一个程序员必练的内功心法哦,虽然我现在也没有刷多少道题,不过我也有在努力哦~后期可能会出一些关于Leetcode的题目。 1、安装 打开你的IDEA,左上角,file-settings-plugins(如下图),点击下方红框中的按钮然后搜索leetcode,安装该插件即可,插件安装完毕之后重启IDEA。安装简单,想要配置好就要用点心了~ 2、配置 2.1 用户名密码配置 如果不出什么意外的话,你可以在你的设置中找到如下配置项, 我们可以看到主页面需要填写用户名密码,这里的用户名密码是你的leetcode的用户名密码 2.2 TempFilePath配置 TempFilePath选项,这个是指新建的文件要放在那里,这个你自己定就好了,如果想先快速了解一下这个插件

LeetCode:Valid Palindrome

牧云@^-^@ 提交于 2019-11-29 04:43:20
1、题目名称 Valid Palindrome(回文字符串) 2、题目地址 https://leetcode.com/problems/valid-palindrome/ 3、题目内容 英文:Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. 中文:给出一个字符串,只考虑字母和数字,判断该字符串是否为回文字符串 例如: "A man, a plan, a canal: Panama" 是一个回文字符串 "race a car" 不是一个回文字符串 4、题目分析 如果要判断一个字符串是否是回文字符串,可以两个变量,从两侧开始比较有效的字符是否相等。这里有效的字符是指 'a'-'z'、'A'-'Z'、'0'-'9' 这三个区间内的字符。 5、解题方法1 可以将字符串中有效的字符,写入一个链表,再将链表转化为数组,直接判断数组两侧对称位置的字符是否匹配。 实现的Java代码如下: import java.util.LinkedList; /** * 功能说明:LeetCode 125 - Valid Palindrome * 开发人员:Tsybius2014 * 开发时间:2015年8月4日 */ public class

LeetCode 620. Not Boring Movies (有趣的电影)

时光毁灭记忆、已成空白 提交于 2019-11-29 04:03:27
题目标签:   题目给了我们一个 cinema 表格, 让我们找出 不无聊的电影,并且id 是奇数的,降序排列。   比较直接简单的,具体看code。 Java Solution: Runtime: 149 ms, faster than 29.21% Memory Usage: N/A 完成日期:07/02/2019 关键点:NOT IN # Write your MySQL query statement below SELECT id, movie, description, rating FROM cinema WHERE id % 2 != 0 AND description NOT IN ("boring") ORDER BY rating DESC; 参考资料:n/a LeetCode 题目列表 - LeetCode Questions List 题目来源:https://leetcode.com/ 来源: https://www.cnblogs.com/jimmycheng/p/11450733.html

LeetCode 183. Customers Who Never Order (从不订购的客户)

北慕城南 提交于 2019-11-29 03:48:16
题目标签:   题目给了我们 Customers 和 Orders 两个表格,让我们找到 从没订购过的客户。   首先从Orders 得到 订购过的CustomerId,然后再去Customers 里找 没有出现过的 Id,返回名字。 Java Solution: Runtime: 264 ms, faster than 53 % Memory Usage: N/A 完成日期:06/01/2019 关键点:NOT IN # Write your MySQL query statement below SELECT Customers.Name AS Customers FROM Customers WHERE Customers.Id NOT IN (SELECT CustomerId FROM Orders); 参考资料:n/a LeetCode 题目列表 - LeetCode Questions List 题目来源:https://leetcode.com/ 来源: https://www.cnblogs.com/jimmycheng/p/11444496.html

LeetCode:Delete Duplicate Emails

[亡魂溺海] 提交于 2019-11-29 03:34:50
1、题目名称 Delete Duplicate Emails(删除重复的邮箱数据) 2、题目地址 https://leetcode.com/problems/delete-duplicate-emails/ 3、题目内容 写一个SQL删除表Person中所有的重复数据,对于重复的数据只保留Id最小的数据 +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | | 3 | john@example.com | +----+------------------+ Id is the primary key column for this table. 例如,在执行完该SQL后,表中剩余数据如下: +----+------------------+ | Id | Email | +----+------------------+ | 1 | john@example.com | | 2 | bob@example.com | +----+------------------+ 4、初始化数据库脚本 在MySQL数据库中建立一个名为LEETCODE的数据库,用MySQL命令行中的source命令执行下面脚本: --

Leetcode题解记录

China☆狼群 提交于 2019-11-29 00:55:01
1. 两数相加 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/add-two-numbers /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ 主要需要注意两个链表长度不一致的情况,控制好边界条件 2. 寻找两个有序数组的中位数 给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。 你可以假设 nums1 和 nums2 不会同时为空。 示例 1: nums1 = [1, 3] nums2 =