
LeetCode
nomasp
Android 工程师
展开
-
LeetCode 8 String to Integer (atoi)(转换到整型)
翻译实现“atoi”将字符串转换成整型数。提示:仔细考虑所有可能的输入。如你想要挑战,请不要参阅下面并问问自己都有哪些可能的输入请看。说明:模糊的指定(没有给定的输入规格)就是为了这个问题。你负责收集所有可能的输入。atoi的要求:函数首先放弃尽可能多的空字符直到找到一个非空白字符。然后从这个字符开始,带上可选的初始加/减字符,其后还可能跟着越多越好的数字,并将它们解释成一个数值。这个字符串可能在这原创 2015-09-25 09:35:06 · 2392 阅读 · 0 评论 -
LeetCode 2 Add Two Numbers
翻译:给你两个表示两个非负数字的链表。数字以相反的顺序存储,其节点包含单个数字。将这两个数字相加并将其作为一个链表返回。输入: (2 -> 4 -> 3) + (5 -> 6 -> 4)输出: 7 -> 0 -> 8原题:You are given two linked lists representing two non-negative numbers. The digits are stor原创 2015-09-14 22:20:37 · 2483 阅读 · 0 评论 -
LeetCode 17 Letter Combinations of a Phone Number(电话号码的字母组合)
翻译给定一个数字字符串,返回所有这些数字可以表示的字母组合。一个数字到字母的映射(就像电话按钮)如下图所示。输入:数字字符串“23”输出:["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]备注:尽管以上答案是无序的,如果你想的话你的答案可以是有序的。原图原文Given a digit string, return all possible原创 2015-10-18 10:59:08 · 4952 阅读 · 0 评论 -
LeetCode 18 4Sum(4个数的和)
翻译给定一个有n个数字的数组S,在S中是否存在元素a,b,c和d的和恰好满足a + b + c + d = target。找出数组中所有的不想等的这四个元素,其和等于target。备注:在(a,b,c,d)中的元素必须从小到大排列。(a ≤ b ≤ c ≤ d)其结果必须不能够重复。例如,给定S = {1 0 -1 0 -2 2},target = 0。一个结果集为:(-1, 0, 0, 1原创 2015-10-18 12:11:53 · 3724 阅读 · 1 评论 -
LeetCode 15 3Sum(3个数的和)
翻译给定一个有n个整数的数组S,是否存在三个元素a,b,c使得a+b+c=0?找出该数组中所有不重复的3个数,它们的和为0。备注:这三个元素必须是从小到大进行排序。结果中不能有重复的3个数。例如,给定数组S={-1 0 1 2 -1 4},一个结果集为:(-1, 0, 1)(-1, -1, 2)原文Given an array S of n integers, are there ele原创 2015-10-17 23:19:02 · 4901 阅读 · 1 评论 -
LeetCode 16 3Sum Closest(最接近的3个数的和)
翻译给定一个有n个整数的数组S,找出S中3个数,使其和等于一个给定的数,target。返回这3个数的和,你可以假定每个输入都有且只有一个结果。例如,给定S = {-1 2 1 -4},和target = 1。那么最接近target的和是2。(-1 + 2 + 1 = 2)。原文Given an array S of n integers, find three integers in S such原创 2015-10-18 09:59:08 · 4604 阅读 · 0 评论 -
LeetCode 1 Two Sum
翻译:给定一个整型数组,找出能相加起来等于一个特定目标数字的两个数。函数twoSum返回这两个相加起来等于目标值的数字的索引,且index1必须小于index2。请记住你返回的答案(包括index1和index2)都不是从0开始的。你可以假定每个输入都有且仅有一个解决方案。输入: numbers={2, 7, 11, 15}, target=9输出: index1=1, index2=2原文:G原创 2015-09-11 10:39:06 · 2845 阅读 · 0 评论 -
LeetCode 3 Longest Substring Without Repeating Characters
翻译 给定一个字符串,找出其没有重复字符的最大子序列的长度。 例如,“abcabcbb”的无重复字符的最大子序列是“abc”,它的长度是3。 “bbbbb”的最大子序列是“b”,它的长度是1。原文Given a string, find the length of the longest substring without repeating characters. For原创 2015-09-16 12:09:35 · 2163 阅读 · 0 评论 -
LeetCode 4 Median of Two Sorted Arrays
翻译有两个给定的排好序的数组nums1和nums2,其大小分别为m和n。找出这两个已排序数组的中位数。总运行时间的复杂度应该是O(log(m+n))。原文There are two sorted arrays nums1 and nums2 of size m and n respectively.Find the median of the two sorted arrays. The o原创 2015-09-17 16:13:18 · 2684 阅读 · 0 评论 -
LeetCode 5 Longest Palindromic Substring(最大回文子字符串)
翻译给定一个字符串S,找出它的最大回文子字符串。你可以假定S的最大长度为1000,并且这里存在唯一一个最大回文子字符串。原文Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists on原创 2015-09-20 13:46:10 · 2921 阅读 · 12 评论 -
LeetCode 7 Reverse Integer(翻转整数)
翻译翻转一个整型数例1:x = 123, 返回 321例2:x = -123, 返回 -321原文Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this? (来自LeetCode官网)Here are some good原创 2015-09-23 09:13:52 · 4805 阅读 · 0 评论 -
LeetCode 11 Container With Most Water(最大水容器)
翻译给定n个非负整数a1,a2,...,an,其中每个代表一个点坐标(i,ai)。n个垂直线段例如线段的两个端点在(i,ai)和(i,0)。找到两个线段,与x轴形成一个容器,使其包含最多的水。备注:你不必倾倒容器。翻译Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate原创 2015-10-12 22:11:52 · 6519 阅读 · 0 评论 -
LeetCode 10 Regular Expression Matching (正则表达式匹配)
翻译实现支持“.”和“*”的正则表达式匹配。“.” 匹配支持单个字符“*” 匹配零个或多个前面的元素匹配应该覆盖到整个输入的字符串(而不是局部的)。该函数的原型应该是:bool isMatch(const char * s, const char * p)示例:isMatch("aa","a") → falseisMatch("aa","aa") → trueisMatch("aaa","aa原创 2015-10-12 12:07:17 · 3726 阅读 · 0 评论 -
LeetCode 20 Valid Parentheses(有效的括号)
翻译给定一个只包含'(', ')', '{', '}', '[' 和']'的字符串,判断这个输入的字符串是否是有效的。括号必须在正确的形式下闭合,"()" 和"()[]{}" 是有效的,但是 "(]" 和"([)]" 则不是。原文Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determ原创 2015-11-10 20:22:23 · 4005 阅读 · 0 评论 -
LeetCode 26 Remove Duplicates from Sorted Array(从已排序数组中移除重复元素)
翻译给定一个已排序的数组,删除重复的元素,这样每个元素只出现一次,并且返回新的数组长度。不允许为另一个数组使用额外的空间,你必须就地以常量空间执行这个操作。例如,给定输入数组为 [1,1,2]你的函数应该返回length = 2, 其前两个元素分别是1和2。它不关心你离开后的新长度。原文Given a sorted array, remove the duplicates in place suc原创 2015-11-13 22:41:10 · 3012 阅读 · 0 评论 -
LeetCode 27 Remove Element(移除元素)
翻译这里写代码片原文Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.原创 2015-11-13 23:25:35 · 4454 阅读 · 4 评论 -
LeetCode 22 Generate Parentheses(生成括号)
翻译给定一个括号序列,写一个函数用于生成正确形式的括号组合。例如,给定n = 3,一个解决方案集是:"((()))", "(()())", "(())()", "()(())", "()()()"原文Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthe原创 2015-11-10 22:41:54 · 5916 阅读 · 1 评论 -
LeetCode 13 Roman to Integer(罗马数到整型数)
翻译给定一个罗马数字,将其转换到整型数值。输入被保证在1到3999之间。原文Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.一开始就没有构思好,虽然按上一题的套路可以走下去,但结果就是像我下面这样……代码凌乱……public class原创 2015-10-15 16:24:24 · 3198 阅读 · 0 评论 -
LeetCode 12 Integer to Roman(整型数到罗马数)
翻译给定一个整型数值,将其转换到罗马数字。输入被保证在1到3999之间。原文Given an integer, convert it to a roman numeral.Input is guaranteed to be within the range from 1 to 3999.我不会告诉你一开始我是用的无数个变量和ifif……后来实在受不了这么多变量就将其写成了枚举,那么接下来就迎刃而解了原创 2015-10-15 14:46:35 · 2913 阅读 · 4 评论 -
LeetCode 28 Implement strStr()(实现strStr()函数)
翻译实现strStr()函数。返回针(needle)在草垛/针垛(haystack)上第一次出现的索引, 如果不存在其中则返回-1。其实也就是说字符串str2在字符串str1中第一次出现的索引而已。原文Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle原创 2015-11-13 23:46:13 · 4548 阅读 · 0 评论 -
LeetCode 29 Divide Two Integers(两个整数相除)(*)
翻译不用乘法、除法、取余操作,将两个数相除。如果它溢出了,返回MAX_INT原文Divide two integers without using multiplication, division and mod operator.If it is overflow, return MAX_INT.代码一心扑到了递归上,可惜没能写出来…………烦躁至极还是找了别人的答案……class Solution原创 2015-11-17 20:04:53 · 4959 阅读 · 0 评论 -
LeetCode 19 Remove Nth Node From End of List(从列表尾部删除第N个结点)(*)
翻译给定一个链表,移除从尾部起的第n个结点,并且返回它的头结点。例如,给定链表:1->2->3->4->5,n = 2。在移除尾部起第二个结点后,链表将变成:1->2->3->5。备注:给定的n是有效的,代码尽量一次通过。原文Given a linked list, remove the nth node from the end of list and return its head.For ex原创 2015-10-21 15:32:44 · 2579 阅读 · 0 评论 -
LeetCode 30 Substring with Concatenation of All Words(与所有文字串联子串)(*)
翻译给定一个字符串S,一个单词的列表words,全是相同的长度。找到的子串(多个)以s即每个词的字串联恰好一次并没有任何插入的字符所有的起始索引。原文You are given a string, s, and a list of words, words, that are all of the same length. Find all starting indices of substring原创 2015-11-18 21:30:43 · 2790 阅读 · 0 评论 -
LeetCode 32 Longest Valid Parentheses(最长有效括号)(*)
翻译给定一个仅仅包含“(”或“)”的字符串,找到其中最长有效括号子集的长度。对于“(()”,它的最长有效括号子集是“()”,长度为2。另一个例子“)()())”,它的最长有效括号子集是“()()”,其长度是4。原文Given a string containing just the characters '(' and ')', find the length of the longest val原创 2015-11-26 18:19:28 · 3615 阅读 · 1 评论 -
LeetCode 33 Search in Rotated Sorted Array(在旋转排序数组中搜索)(*)
翻译假定一个数组在一个我们预先不知道的轴点旋转。例如,0 1 2 4 5 6 7可能会变为4 5 6 7 0 1 2。给你一个目标值去搜索,如果找到了则返回它的索引,否则返回-1。你可以假定没有重复的元素存在于数组中。原文Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4原创 2015-11-26 18:50:31 · 3628 阅读 · 0 评论 -
LeetCode 326 Power of Three(3的幂)(递归、Log函数)
翻译给定一个整型数,写一个函数决定它是否是3的幂(翻译可能不太合适……跟进:你是否可以不用任何循环或递归来完成。原文Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?分析题意我其原创 2016-01-19 09:50:17 · 6553 阅读 · 0 评论 -
LeetCode 202 Happy Number(开心数)(vector、unordered_set)
翻译写一个算法来决定一个数是否是“开心”的。开心数是被如下步骤所定义的数:从所有正整数开始,用其每个数字的平方和来替代这个数,不断重复这个过程直到最后的结果为1(此时它就停止了),或者它在一个不包含1的周期内无限循环。这些在这个过程中以1结尾的数就是开心数。例如:19是开心数。12+92=821^2 + 9^2 = 82 82+22=688^2 + 2^2 = 68 62+82=1006^2 +原创 2016-01-19 10:51:00 · 2773 阅读 · 0 评论 -
LeetCode 231 Power of Two(2的幂)
翻译给定一个整型数,写一个函数来决定它是否是2的幂。原文Given an integer, write a function to determine if it is a power of two.分析详情请看这篇文章:LeetCode 326 Power of Three(3的幂)(递归、Log函数) 看题号,326是本题的加强版,326是要求不能用循环或递归的……大家可以去看看上面那篇文章。本原创 2016-01-19 11:03:30 · 2441 阅读 · 0 评论 -
LeetCode 34 Search for a Range(搜索范围)
翻译给定一个整型已排序数组,找到一个给定值在其中的起点与终点。你的算法复杂度必须低于O(logn)。如果目标在数组中不会被发现,返回[-1, -1]。例如,给定[5, 7, 7, 8, 8, 10],目标值为8,返回[3, 4]。原文Given a sorted array of integers, find the starting and ending position of a given原创 2015-11-28 21:08:06 · 2592 阅读 · 1 评论 -
LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译用队列来实现栈的如下操作。push(x) —— 将元素x添加进栈pop() —— 从栈顶移除元素top() —— 返回栈顶元素empty() —— 返回栈是否为空注意:你必须使用一个只有标准操作的队列。也就是说,只有push/pop/size/empty等操作是有效的。队列可能不被原生支持,这取决于你所用的语言。只要你只是用queue的标准操作,你可以用list或者deque(double原创 2016-01-20 21:28:09 · 4068 阅读 · 0 评论 -
LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)
翻译用栈来实现队列的下列操作。push(x) —— 将元素x写入到队列的尾部pop() —— 从队列首部移除元素peek() —— 返回队列首部元素empty() —— 返回队列是否为空注意:你必须使用一个只有标准操作的栈。也就是说,只有push/pop/size/empty等操作是有效的。栈可能不被原生支持,这取决于你所用的语言。只要你只是用stack的标准操作,你可以用list或者dequ原创 2016-01-20 21:18:14 · 3540 阅读 · 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 w原创 2015-11-28 21:16:01 · 2865 阅读 · 0 评论 -
LeetCode 37 Sudoku Solver(求解数独)(*)
翻译写一个程序来通过填充空格求解数独。空格用'.'表示。你可以假定这里只有唯一解。(示例图片看下文)原文代码这道题我没写……不过为了博客的连续性,先凑一篇占个位置,以后再修改。class Solution {public: bool col[10][10],row[10][10],f[10][10]; bool flag = false; void solveSudoku(原创 2015-12-02 12:47:14 · 2978 阅读 · 0 评论 -
LeetCode 110 Balanced Binary Tree(平衡二叉树)(*)
翻译给定一个二叉树,决定它是否是高度平衡的。(高度是名词不是形容词……对于这个问题,一个高度平衡二叉树被定义为:这棵树的每个节点的两个子树的深度差不能超过1。原文Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a原创 2016-01-21 11:46:53 · 4123 阅读 · 2 评论 -
LeetCode 101 Symmetric Tree(对称树)(*)
翻译给定一个二叉树,检查它是否是它本身的镜像(也就是说,中心对称)。例如,这个二叉树是对称的: 1 / \ 2 2 / \ / \3 4 4 3但是下面这个却不是: 1 / \ 2 2 \ \ 3 3批注:如果你能同时用递归和迭代来解决这个问题,这会是加分项。原文Given a binary tree, check whet原创 2016-01-21 14:04:11 · 2033 阅读 · 0 评论 -
LeetCode 102 Binary Tree Level Order Traversal(二叉树的层级顺序遍历)(*)
翻译给定一个二叉树,返回按层级顺序遍历的每个节点的值。从左到右,逐层遍历。例如:给定一个二叉树 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7返回它的层级顺序遍历结果为:[ [3], [9,20], [15,7]]翻译Given a binary tree, return the level order tra原创 2016-01-22 10:30:27 · 2615 阅读 · 0 评论 -
LeetCode 107 Binary Tree Level Order Traversal II(二叉树的层级顺序遍历2)(*)
翻译给定一个二叉树,返回从下往上遍历经过的每个节点的值。从左往右,从叶子到节点。例如:给定的二叉树是 {3,9,20,#,#,15,7}, 3 / \ 9 20 / \ 15 7返回它从下往上的遍历结果:[ [15,7], [9,20], [3]]原文Given a binary tree, return the bottom-up lev原创 2016-01-22 10:03:04 · 3646 阅读 · 0 评论 -
LeetCode 198 House Robber(强盗盗窃最大值)(动态规划)(*)
翻译你是一个专业强盗,并计划沿街去盗窃每一个住户。每个房子都有一定量的现金,阻止你盗窃的唯一阻碍是相邻的两个房子之间有安全系统。一旦这两个房子同时被盗窃,系统就会自动联系警察。给定一系列非负整数代表每个房子的金钱,求出再不惊动警察的情况下能盗窃到的最大值。原文You are a professional robber planning to rob houses along a street. Ea原创 2016-01-22 11:09:45 · 3566 阅读 · 0 评论 -
LeetCode 66 Plus One(加一)(vector)
翻译给定一个以一系列数字表示的非负数,将其加一并转换成数字。数字存储的最高位在列的最前面。原文Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the原创 2016-01-22 21:47:33 · 2724 阅读 · 1 评论 -
LeetCode 263 Ugly Number(丑数)
翻译写一个程序来检查给定的数字是否是丑数(Ugly number)。丑数是一个正数,它的质因子只包括2、3、5。例如,6、8是丑数,而因为包含了7这个因子,所以14不是丑数。请注意,1通常被视为一个丑数。原文Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbe原创 2016-01-18 13:24:00 · 2930 阅读 · 0 评论