
LeetCode
IUVGood
此人不懒,但什么都没有写
展开
-
LeetCode6. Z 字形变换
将一个给定字符串根据给定的行数,以从上往下、从左到右进行Z 字形排列。比如输入字符串为"LEETCODEISHIRING"行数为 3 时,排列如下:L C I RE T O E S I I GE D H N之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。请你实现这个将字符串进行指定行数变换的...转载 2019-03-14 14:21:19 · 94 阅读 · 0 评论 -
LeetCode5. 最长回文子串
给定一个字符串s,找到s中最长的回文子串。你可以假设s的最大长度为 1000。示例 1:输入: "babad"输出: "bab"注意: "aba" 也是一个有效答案。示例 2:输入: "cbbd"输出: "bb"解题思路:动态规划 dp[i][j]=1,s[i]==s[j]&&dp[i+1][j-1]=1 ;dp[i][j]=0,el...转载 2019-04-09 17:49:39 · 151 阅读 · 0 评论 -
LeetCode143. Reorder List
Given a singly linked listL:L0→L1→…→Ln-1→Ln,reorder it to:L0→Ln→L1→Ln-1→L2→Ln-2→…You maynotmodify the values in the list's nodes, only nodes itself may be changed.Example 1:Given 1->2-...转载 2019-06-13 14:59:10 · 179 阅读 · 0 评论 -
LeetCode160. Intersection of Two Linked Lists [C++]
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:begin to intersect at node c1.Example 1:Input: ...原创 2019-06-13 15:48:41 · 289 阅读 · 0 评论 -
LeetCode599. Minimum Index Sum of Two Lists [C++]
Suppose Andy and Doris want to choose a restaurant for dinner, and they both have a list of favorite restaurants represented by strings.You need to help them find out theircommon interestwith the...原创 2019-06-13 20:21:31 · 186 阅读 · 0 评论 -
LeetCode500. Keyboard Row [C++]
Given a List of words, return the words that can be typed using letters ofalphabeton only one row's of American keyboard like the image below.Example:Input: ["Hello", "Alaska", "Dad", "...原创 2019-06-13 21:21:12 · 275 阅读 · 0 评论 -
LeetCode219. Contains Duplicate II [C++]
Given an array of integers and an integerk, find out whether there are two distinct indicesiandjin the array such thatnums[i] = nums[j]and theabsolutedifference betweeniandjis at mostk....原创 2019-06-14 15:00:14 · 196 阅读 · 0 评论 -
LeetCode217. Contains Duplicate [C++]
Given an array of integers, find if the array contains any duplicates.Your function should return true if any value appears at least twice in the array, and it should return false if every element i...原创 2019-06-14 15:11:14 · 273 阅读 · 0 评论 -
LeetCode220. Contains Duplicate III
Given an array of integers, find out whether there are two distinct indicesiandjin the array such that theabsolutedifference betweennums[i]andnums[j]is at mosttand theabsolutedifference ...转载 2019-06-14 17:19:39 · 157 阅读 · 0 评论 -
LeetCode11. Container With Most Water[C++]
Givennnon-negative integersa1,a2, ...,an, where each represents a point at coordinate (i,ai).nvertical lines are drawn such that the two endpoints of lineiis at (i,ai) and (i, 0). Find two ...原创 2019-06-19 16:07:15 · 195 阅读 · 0 评论 -
LeetCode283. Move Zeroes
Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements.Example:Input: [0,1,0,3,12]Output: [1,3,12,0,0]Note:You m...转载 2019-06-19 17:51:51 · 121 阅读 · 0 评论 -
LeetCode43. Multiply Strings
Given two non-negative integersnum1andnum2represented as strings, return the product ofnum1andnum2, also represented as a string.Example 1:Input: num1 = "2", num2 = "3"Output: "6"Example...转载 2019-06-26 19:35:13 · 148 阅读 · 0 评论 -
LeetCode415. Add Strings
Given two non-negative integersnum1andnum2represented as string, return the sum ofnum1andnum2.Note:The length of bothnum1andnum2is < 5100. Bothnum1andnum2contains only digits0-...转载 2019-06-26 19:59:47 · 205 阅读 · 0 评论 -
LeetCode989. Add to Array-Form of Integer
For a non-negative integerX, thearray-form ofXis an array of its digits in left to right order. For example, ifX = 1231, then the array form is[1,2,3,1].Given the array-formAof a non-negati...转载 2019-06-26 20:36:26 · 154 阅读 · 0 评论 -
2019网易秋招笔试题:瞌睡
#include <iostream>using namespace std;int a[10001] = { 0 };bool t[10001] = { 0 };int main(){ int n, k; cin >> n >> k; for (int i = 1; i <= n; i++) { cin >> a...原创 2019-08-01 21:13:45 · 436 阅读 · 0 评论 -
2019网易秋招笔试题:[编程题]丰收
又到了丰收的季节,恰逢小易去牛牛的果园里游玩。牛牛常说他对整个果园的每个地方都了如指掌,小易不太相信,所以他想考考牛牛。在果园里有N堆苹果,每堆苹果的数量为ai,小易希望知道从左往右数第x个苹果是属于哪一堆的。牛牛觉得这个问题太简单,所以希望你来替他回答。输入描述:第一行一个数n(1 <= n <= 105)。第二行n个数ai(1 <= ai<= ...原创 2019-08-01 21:41:05 · 306 阅读 · 0 评论 -
LeetCode3. 无重复字符的最长子串
给定一个字符串,请你找出其中不含有重复字符的最长子串的长度。示例1:输入: "abcabcbb"输出: 3 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。示例 2:输入: "bbbbb"输出: 1解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。示例 3:输入: "pwwkew"输出: 3解释: 因为无重复字符的最...转载 2019-04-09 16:28:48 · 114 阅读 · 0 评论 -
LeetCode17. 电话号码的组合
给定一个仅包含数字2-9的字符串,返回所有它能表示的字母组合。给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。示例:输入:"23"输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"].解题思路:通过输入的digits、找到对应的字母,然后将目前的字母和已有的字母进行组合。cla...转载 2019-03-28 16:52:04 · 200 阅读 · 0 评论 -
LeetCode17.删除排序数组中的重复项 C++
给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。示例1:给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后面的元素。示例2:...转载 2019-03-28 16:49:05 · 530 阅读 · 0 评论 -
LeetCode5.最长回文子串
给定一个字符串s,找到s中最长的回文子串。你可以假设s的最大长度为 1000。示例 1:输入: "babad"输出: "bab"注意: "aba" 也是一个有效答案。示例 2:输入: "cbbd"输出: "bb"动态规划。1、申请数组dp[i][j],用来表示i,j的子串是否为回文子串。2、判断长度L为2的字串是否为回文子串。3、判断长度为L的...转载 2019-03-10 16:17:31 · 77 阅读 · 0 评论 -
LeetCode214.最短回文串
给定一个字符串s,你可以通过在字符串前面添加字符将其转换为回文串。找到并返回可以用这种方式转换的最短回文串。示例1:输入: "aacecaaa"输出: "aaacecaaa"示例 2:输入: "abcd"输出: "dcbabcd"1、s=s1+s2,其中s1是回文串,s2是s-s1。2、ans=反转后的s2 +sclass Solution {publ...转载 2019-03-10 16:54:53 · 668 阅读 · 0 评论 -
LeetCode72. 编辑距离
给定两个单词word1和word2,计算出将word1转换成word2所使用的最少操作数。你可以对一个单词进行如下三种操作:插入一个字符 删除一个字符 替换一个字符示例1:输入: word1 = "horse", word2 = "ros"输出: 3解释: horse -> rorse (将 'h' 替换为 'r')rorse -> ros...转载 2019-03-14 20:55:14 · 116 阅读 · 0 评论 -
LeetCode4.寻找两个有序数组的中位数
给定两个大小为 m 和 n 的有序数组nums1和nums2。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为O(log(m + n))。你可以假设nums1和nums2不会同时为空。示例 1:nums1 = [1, 3]nums2 = [2]则中位数是 2.0示例 2:nums1 = [1, 2]nums2 = [3, 4]则中...转载 2019-03-10 20:18:41 · 171 阅读 · 0 评论 -
LeetCode234. Palindrome Linked List
Given a singly linked list, determine if it is a palindrome.Example 1:Input: 1->2Output: falseExample 2:Input: 1->2->2->1Output: trueFollow up:Could you do it in O(n) time a...转载 2019-03-11 16:07:08 · 194 阅读 · 0 评论 -
LeetCode15.三数之和
给定一个包含n个整数的数组nums,判断nums中是否存在三个元素a,b,c ,使得a + b + c =0 ?找出所有满足条件且不重复的三元组。注意:答案中不可以包含重复的三元组。例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],满足要求的三元组集合为:[ [-1, 0, 1], [-1, -1, 2]]先排序,从小到大选取...转载 2019-03-11 17:01:42 · 91 阅读 · 0 评论 -
LeetCode16.最接近的三数之和
给定一个包括n个整数的数组nums和 一个目标值target。找出nums中的三个整数,使得它们的和与target最接近。返回这三个数的和。假定每组输入只存在唯一答案。例如,给定数组 nums = [-1,2,1,-4], 和 target = 1.与 target 最接近的三个数的和为 2. (-1 + 2 + 1 = 2).道理与15一样,进行逼近。cla...转载 2019-03-11 19:46:09 · 88 阅读 · 0 评论 -
LeetCode18.四数之和
给定一个包含n个整数的数组nums和一个目标值target,判断nums中是否存在四个元素a,b,c和d,使得a+b+c+d的值与target相等?找出所有满足条件且不重复的四元组。注意:答案中不可以包含重复的四元组。示例:给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。满足要求的四元组集合...转载 2019-03-12 16:57:09 · 117 阅读 · 0 评论 -
LeetCode454.四数相加II
给定四个包含整数的数组列表A , B , C , D ,计算有多少个元组(i, j, k, l),使得A[i] + B[j] + C[k] + D[l] = 0。为了使问题简单化,所有的 A, B, C, D 具有相同的长度N,且 0 ≤ N ≤ 500 。所有整数的范围在 -228到 228- 1 之间,最终结果不会超过231- 1 。例如:输入:A = [ 1...转载 2019-03-12 21:01:01 · 113 阅读 · 0 评论 -
LeetCode23.合并k个排序链表
http://www.cnblogs.com/grandyang/p/4606710.html转载 2019-03-18 20:53:41 · 140 阅读 · 0 评论 -
LeetCode39. 组合总和
给定一个无重复元素的数组candidates和一个目标数target,找出candidates中所有可以使数字和为target的组合。candidates中的数字可以无限制重复被选取。说明:所有数字(包括target)都是正整数。 解集不能包含重复的组合。示例1:输入: candidates = [2,3,6,7], target = 7,所求解集...转载 2019-03-29 16:27:36 · 141 阅读 · 0 评论 -
LeetCode12. 整数转罗马数字
罗马数字包含以下七种字符:I,V,X,L,C,D和M。字符 数值I 1V 5X 10L 50C 100D 500M 1000例如, 罗马数字 2 写做II,即为两个并列的 1...转载 2019-03-26 16:08:27 · 133 阅读 · 0 评论 -
LeetCode42. 接雨水
https://blog.youkuaiyun.com/lv1224/article/details/81023833接雨水2http://www.cnblogs.com/grandyang/p/5928987.html转载 2019-03-26 21:06:23 · 98 阅读 · 0 评论 -
LeetCode31. 下一个排列
实现获取下一个排列的函数,算法需要将给定数字序列重新排列成字典序中下一个更大的排列。如果不存在下一个更大的排列,则将数字重新排列成最小的排列(即升序排列)。必须原地修改,只允许使用额外常数空间。以下是一些例子,输入位于左侧列,其相应输出位于右侧列。1,2,3→1,3,23,2,1→1,2,31,1,5→1,5,1解题思路:判断按照字典序有没有下一个,如果完全降序就没...转载 2019-03-31 15:47:40 · 115 阅读 · 0 评论 -
LeetCode46. 全排列
给定一个没有重复数字的序列,返回其所有可能的全排列。示例:输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]解题思路:1、插空法:向1的左右插入2、得到:1、2;2、1;再插入3得到3、1、2;1、3、2;1、2、3; 3、2、1;2、3、1;2、1、3;...转载 2019-03-31 16:49:33 · 109 阅读 · 0 评论 -
LeetCode10.正则表达式匹配
http://www.cnblogs.com/grandyang/p/4461713.html给定一个字符串(s) 和一个字符模式(p)。实现支持'.'和'*'的正则表达式匹配。'.' 匹配任意单个字符。'*' 匹配零个或多个前面的元素。匹配应该覆盖整个字符串(s) ,而不是部分字符串。说明:s可能为空,且只包含从a-z的小写字母。 p可能为空,且...转载 2019-03-27 15:39:39 · 136 阅读 · 0 评论