Leetcode
文章平均质量分 61
koishi70
cs
展开
-
Leetcode 88 Merge Sorted Array 合并两个有序数组
题目描述:Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additi...原创 2018-03-23 09:17:56 · 173 阅读 · 0 评论 -
Leetcode 21 Merge Two Sorted Lists 将两个已排序的链表合并起来
题目描述:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.把两个已经排序的链表合并起来,合并后仍然要有序Input: 1->2->4, 1->3-&...原创 2018-03-22 09:00:30 · 150 阅读 · 0 评论 -
Leetcode 26 Remove Duplicates from Sorted Array 有序向量中的去重
题目描述:Given a sorted array, remove the duplicates in-place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this by modifying...原创 2018-03-22 09:08:14 · 91 阅读 · 0 评论 -
Leetcode 38 Count and Say
题目描述:The count-and-say sequence is the sequence of integers with the first five terms as following:1. 12. 113. 214. 12115. 1112211 is read off as "one 1" or 11.11 is read off ...原创 2018-03-22 09:16:41 · 89 阅读 · 0 评论 -
Leetcode 53 Maximum Subarray 最大子串和
题目描述:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [-2,1,-3,4,-1,2,1,-5,4],the contiguous subarray [4,-1,2,1] has...原创 2018-03-22 09:30:08 · 182 阅读 · 0 评论 -
Leetcode 58 Length of Last Word 句子中最后一个词的长度
题目描述:Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is defi...原创 2018-03-22 09:34:06 · 147 阅读 · 0 评论 -
Leetcode 66 Plus One
题目描述:Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.You may assume the integer do not contain any leading zero, except the number 0 itself.The digits ...原创 2018-03-22 09:40:12 · 101 阅读 · 0 评论 -
Leetcode 67 Add Binary 二进制加
题目描述:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".用两个string表示大数,返回他们的和(也用string)思路: 1.不用想,这道题用string肯定是有坑的,很有可能输入的string很长,远远超过了long lon...原创 2018-03-22 09:47:39 · 109 阅读 · 0 评论 -
Leetcode 69 Sqrt(x) 求x开根号的整数部分
题目描述:Implement int sqrt(int x).Compute and return the square root of x.x is guaranteed to be a non-negative integer.Example 1:Input: 4Output: 2Example 2:Input: 8Output: 2Explanation: The square roo...原创 2018-03-22 09:54:16 · 657 阅读 · 0 评论 -
Leetcode 70 Climbing Stairs 爬楼梯的方法(动态规划)
题目描述:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?Note: Given n will be a positiv...原创 2018-03-22 09:58:20 · 320 阅读 · 0 评论 -
Leetcode 83 Remove Duplicates from Sorted List 有序链表去重
题目描述:Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.思路:...原创 2018-03-22 10:00:41 · 155 阅读 · 0 评论 -
Leetcode 848 字母移位
题目描述:有一个由小写字母组成的字符串 S,和一个整数数组 shifts。我们将字母表中的下一个字母称为原字母的 移位(由于字母表是环绕的, 'z' 将会变成 'a')。例如·,shift('a') = 'b', shift('t') = 'u',, 以及 shift('z') = 'a'。对于每个 shifts[i] = x , 我们会将 S 中的前 i+1 个字母移位 x 次。返回将所有这些移...原创 2018-06-10 11:02:53 · 425 阅读 · 0 评论 -
Leetcode 850 矩形面积 II
题目描述:我们给出了一个(轴对齐的)矩形列表 rectangles 。 对于 rectangle[i] = [x1, y1, x2, y2],其中(x1,y1)是矩形 i 左下角的坐标,(x2,y2)是该矩形右上角的坐标。找出平面中所有矩形叠加覆盖后的总面积。 由于答案可能太大,请返回它对 10 ^ 9 + 7 取模的结果。示例 1:输入:[[0,0,2,2],[1,0,2,3],[1,0,3,1...原创 2018-06-10 11:02:43 · 2919 阅读 · 0 评论 -
Leetcode 849 到最近的人的最大距离
问题描述:在一排座位( seats)中,1 代表有人坐在座位上,0 代表座位上是空的。至少有一个空座位,且至少有一人坐在座位上。亚历克斯希望坐在一个能够使他与离他最近的人之间的距离达到最大化的座位上。返回他到离他最近的人的最大距离。示例 1:输入:[1,0,0,0,1,0,1]输出:2解释:如果亚历克斯坐在第二个空位(seats[2])上,他到离他最近的人的距离为 2 。如果亚历克斯坐在其...原创 2018-06-10 11:09:28 · 1050 阅读 · 0 评论 -
Leetcode 860 柠檬水找零
题目描述:在柠檬水摊上,每一杯柠檬水的售价为 5 美元。顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯。每位顾客只买一杯柠檬水,然后向你付 5 美元、10 美元或 20 美元。你必须给每个顾客正确找零,也就是说净交易是每位顾客向你支付 5 美元。注意,一开始你手头没有任何零钱。如果你能给每位顾客正确找零,返回 true ,否则返回 false 。示例 1:输入:[5,5,5,...原创 2018-07-01 11:14:19 · 1741 阅读 · 0 评论 -
Leetcode 869 重新排序得到 2 的幂
题目描述:从正整数 N 开始,我们按任何顺序(包括原始顺序)将数字重新排序,注意其前导数字不能为零。如果我们可以通过上述方式得到 2 的幂,返回 true;否则,返回 false。 示例 1:输入:1输出:true示例 2:输入:10输出:false示例 3:输入:16输出:true示例 4:输入:24输出:false示例 5:输入:46输出:true 提示:1 <= N...原创 2018-07-15 20:24:56 · 687 阅读 · 0 评论 -
Leetcode 20 Valid Parentheses 判断括号匹配
题目描述:Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid...原创 2018-03-21 18:15:43 · 145 阅读 · 0 评论 -
Leetcode 14 寻找字符串之间的最长前缀
题目描述:Write a function to find the longest common prefix string amongst an array of strings.有许多字符串,寻找他们之间的最长的公共前缀思路:1.将n个字符串的比较转化为2个之间的,n个之间的无非是两两比较得到结果的最小值2.假设有m个字符串,每个字符串n长度,时间复杂度O(mn),空间复杂度O(1)源代码:c...原创 2018-03-21 18:10:37 · 466 阅读 · 0 评论 -
Leetcode 27 Remove Element
题目描述:Given an array and a value, remove all instances of that value in-place and return the new length.Do not allocate extra space for another array, you must do this by modifying the input array in-p...原创 2018-03-23 09:24:41 · 111 阅读 · 1 评论 -
Leetcode 28 Implement strStr() 字符串匹配
题目描述:Implement strStr().Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Example 2:In...原创 2018-03-23 12:32:18 · 138 阅读 · 0 评论 -
Leetcode 35 Search Insert Position 二分查找插入数字位置
题目描述:Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You may assume no duplicates in the array...原创 2018-03-23 12:42:08 · 216 阅读 · 0 评论 -
Leetcode 100 Same Tree 判断两棵树相同
题目描述:Given two binary trees, write a function to check if they are the same or not.Two binary trees are considered the same if they are structurally identical and the nodes have the same value.给出两个二叉树...原创 2018-03-24 20:13:50 · 176 阅读 · 0 评论 -
Leetcode 101 Symmetric Tree 判断一棵树是否对称
题目描述:Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3Bu...原创 2018-03-24 20:20:40 · 195 阅读 · 0 评论 -
Leetcode 104 Maximum Depth of Binary Tree 二叉树的最大深度
题目描述:Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.For example:Given binary tree [3,9,20...原创 2018-03-24 20:25:23 · 129 阅读 · 0 评论 -
Leetcode 107 Binary Tree Level Order Traversal II 二叉树层次遍历与倒转
题目描述:Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree [3,9,20,null,null,15...原创 2018-03-24 20:32:25 · 233 阅读 · 0 评论 -
Leetcode 108 Convert Sorted Array to Binary Search Tree 将一个有序数组变成BST
题目描述:Given an array where elements are sorted in ascending order, convert it to a height balanced BST.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of ...原创 2018-03-24 20:40:14 · 182 阅读 · 0 评论 -
Leetcode 804 Unique Morse Code Words 莫尔斯电码重复问题
题目描述:International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so o原创 2018-03-25 11:05:07 · 2179 阅读 · 0 评论 -
Leetcode 806 Number of Lines To Write String 字符串的行数
题目描述:We are to write the letters of a given string S, from left to right into lines. Each line has maximum width 100 units, and if writing a letter would cause the width of the line to exceed 100 unit...原创 2018-03-25 11:05:11 · 1149 阅读 · 0 评论 -
Leetcode 807 Max Increase to Keep City Skyline 不变天际线
题目描述:In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located there. We are allowed to increase the height of any number of buildings, by any amount (the amount...原创 2018-03-25 11:05:09 · 1194 阅读 · 1 评论 -
Leetcode 1 Two Sum 两数之和
题目描述: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, and you may not use the sa...原创 2018-03-20 23:43:05 · 275 阅读 · 0 评论 -
Leetcode 7 Reverse Integer 将整数逆转
题目描述:Given a 32-bit signed integer, reverse digits of an integer.给一个int整数,将数字部分反转,符号不变,翻转后溢出则返回0思路:1.利用string作为中转,反转数字2.保证函数总是反转x的绝对值,减少判断。3.最开始将所有的末尾0去掉(除以10即可),免得翻转后开头出现04.时间复杂度O(lgn),空间复杂度O(lgn),n为...原创 2018-03-21 17:46:47 · 254 阅读 · 0 评论 -
Leetcode 9 Palindrome Number 判断一个数是否为回文数
题目描述:Determine whether an integer is a palindrome. Do this without extra space.判断一个数是否是回文数(正着读和反着读一样),负数没有回文数思路:1.取a=输入数。令b=0,是a的倒转形式每次b乘以10,以表示b向左移动一次,取a的最后一位数字加到b中,做完后a除以10,表示下一次可以取出a 的左面一位。假设a=121,...原创 2018-03-21 17:54:15 · 304 阅读 · 0 评论 -
Leetcode 13 Roman to Integer 罗马数字转整数
题目描述:Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.将一段string的罗马数字转换到整数,输入数字在1-3999之间思路:1.原来的思路十分麻烦,需要匹配大量的情况,但是看到讨论区的大神的代码后瞬间佩服的五体投地,再也不敢把自己...原创 2018-03-21 18:02:45 · 234 阅读 · 0 评论 -
Leetcoe 868 二进制间距
题目描述:给定一个正整数 N,找到并返回 N 的二进制表示中两个连续的 1 之间的最长距离。 如果没有两个连续的 1,返回 0 。 示例 1:输入:22输出:2解释:22 的二进制是 0b10110 。在 22 的二进制表示中,有三个 1,组成两对连续的 1 。第一对连续的 1 中,两个 1 之间的距离为 2 。第二对连续的 1 中,两个 1 之间的距离为 1 。答案取两个距离之中最...原创 2018-07-15 20:25:47 · 408 阅读 · 0 评论