
LeetCode
文章平均质量分 57
. . . . .
越努力越幸运!
展开
-
LeetCode26. 删除排序数组中的重复项
题目链接给定一个排序数组,你需要在 原地 删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。不要使用额外的数组空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。示例 1:给定数组 nums = [1,1,2],函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。你不需要考虑数组中超出新长度后面的元素。示例...原创 2020-04-27 15:39:54 · 181 阅读 · 0 评论 -
打家劫舍(动态规划)
题目链接:https://leetcode-cn.com/explore/interview/card/top-interview-quesitons-in-2018/272/dynamic-programming/1177/description:你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋...原创 2019-08-06 19:51:42 · 1659 阅读 · 0 评论 -
LeetCode2. 两数相加 (中等题目)
题目链接:https://leetcode-cn.com/problems/add-two-numbers/description:给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。...原创 2019-08-08 22:17:45 · 238 阅读 · 0 评论 -
Two Sum
题目链接:https://leetcode-cn.com/problems/two-sumdescription:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。示例:给定 nums = [2, 7, 11, 15], target ...原创 2019-08-06 17:30:11 · 158 阅读 · 0 评论 -
打家劫舍 II(动态规划)
题目链接:https://leetcode-cn.com/problems/house-robber-iidescription:你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金。这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的。同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。给定一个代表每个房屋存放金额...原创 2019-08-07 22:20:11 · 1161 阅读 · 1 评论 -
Roman to Integer
题目链接Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.Symbol ValueI 1V 5X 10L 50C 100D ...原创 2019-01-30 18:41:25 · 185 阅读 · 0 评论 -
Fizz Buzz
题目链接Write a program that outputs the string representation of numbers from 1 to n.But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”...原创 2019-01-22 23:10:40 · 154 阅读 · 0 评论 -
Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.Example 1:Input: [3,0,1]Output: 2Example 2:Input: [9,6,4,2,3,5,7,0,1]Output:...原创 2019-02-01 17:00:53 · 437 阅读 · 0 评论 -
Valid Parentheses
题目链接Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid.An input string is valid if:Open brackets must be closed by the same type ...原创 2019-02-01 16:20:29 · 1031 阅读 · 0 评论 -
Pascal's Triangle
题目链接Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.In Pascal’s triangle, each number is the sum of the two numbers directly above it.Example:Input: 5Output...原创 2019-02-01 11:58:38 · 265 阅读 · 0 评论 -
Reverse Bits
Reverse bits of a given 32 bits unsigned integer.Example 1:Input: 00000010100101000001111010011100Output: 00111001011110000010100101000000Explanation: The input binary string 00000010100101000001...原创 2019-01-31 20:42:08 · 364 阅读 · 1 评论 -
Hamming Distance
The Hamming distance between two integers is the number of positions at which the corresponding bits are different.Given two integers x and y, calculate the Hamming distance.Note:0 ≤ x, y < 231....原创 2019-01-31 19:03:39 · 552 阅读 · 0 评论 -
Number of 1 Bits
题目链接Write a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight).Example 1:Input: 00000000000000000000000000001011Output: 3Explanat...原创 2019-01-31 18:13:48 · 285 阅读 · 0 评论 -
两个有序数组的合并,时间复杂度为O(m+n)
2019.8.18 字节跳动一面题目实现思路:三个指针实现:JavaScript代码:let arr1 = [1, 5]let arr2 = [7, 8, 30, 100]let arr = []let i = 0, j = 0let k = 0;while(i<arr1.length && j< arr2.length) { if (arr...原创 2019-08-19 10:19:48 · 5509 阅读 · 0 评论 -
无重复字符的最长子串(LeetCode 3 middle)
题目链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/submissions/description给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。示例 1:输入: “abcabcbb”输出: 3解释: 因为无重复字符的最长子串是 “abc”,所以其长度...原创 2019-08-10 09:36:02 · 184 阅读 · 0 评论 -
迷宫问题(回溯思想)
题目链接:https://www.luogu.org/problem/P1605题目背景:给定一个N*M方格的迷宫,迷宫里有T处障碍,障碍处不可通过。给定起点坐标和终点坐标,问: 每个方格最多经过1次,有多少种从起点坐标到终点坐标的方案。在迷宫中移动有上下左右四种方式,每次只能移动一个方格。数据保证起点上没有障碍。输入格式:第一行N、M和T,N为行,M为列,T为障碍总数。第二行起点坐标SX...原创 2019-08-14 17:33:01 · 1598 阅读 · 2 评论 -
LeetCode 611. 有效三角形的个数
题目链接:https://leetcode-cn.com/problems/valid-triangle-number/comments/给定一个包含非负整数的数组,你的任务是统计其中可以组成三角形三条边的三元组个数。示例 1:输入: [2,2,3,4]输出: 3解释:有效的组合是:2,3,4 (使用第一个 2)2,3,4 (使用第二个 2)2,2,3注意:数组长度不超...原创 2019-09-29 19:58:22 · 313 阅读 · 0 评论 -
LeetCode 415. 字符串相加
题目链接:https://leetcode-cn.com/problems/add-strings/description:给定两个字符串形式的非负整数 num1 和num2 ,计算它们的和。注意:num1 和num2 的长度都小于 5100.num1 和num2 都只包含数字 0-9.num1 和num2 都不包含任何前导零。你不能使用任何內建 BigInteger 库, 也不能直...原创 2019-09-13 11:19:14 · 135 阅读 · 0 评论 -
没有括号的算式(python中for循环如何跳跃处理)
有一个写在黑板上的加减法算式,运算符只包含加号、减号和括号,但其中的括号被清洁工擦去了。现在需要你写一个算法计算这个算式括号被擦去之前的可能的最小结果值。输入:仅有一行,被擦去括号后的算式输入约束:算式最多有50个字符,且其中仅包含0-9和+、-算式的第一个字符一定是数字算式中不会连续出现两个运算符算式中每个整数最多有5位输出:一个整数:即括号被擦去之前,该算式可能的最小结果值...原创 2019-09-11 17:08:36 · 1908 阅读 · 0 评论 -
LeetCode 605. 种花问题
题目链接:https://leetcode-cn.com/problems/can-place-flowers/submissions/description:假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植...原创 2019-09-02 20:54:20 · 314 阅读 · 0 评论 -
153. 寻找旋转排序数组中的最小值
题目链接:https://leetcode-cn.com/problems/find-minimum-in-rotated-sorted-array/description:假设按照升序排序的数组在预先未知的某个点上进行了旋转。( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。请找出其中最小的元素。你可以假设数组中不存在重复元素。示例 1:...原创 2019-08-26 09:56:56 · 106 阅读 · 0 评论 -
LeetCode 46. 全排列
题目链接:https://leetcode-cn.com/problems/permutations/原创 2019-08-17 19:45:15 · 131 阅读 · 0 评论 -
LeetCode 78求子集
题目链接:https://leetcode-cn.com/problems/subsets/description:给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。说明:解集不能包含重复的子集。示例:输入: nums = [1,2,3]输出:[[3],[1],[2],[1,2,3],[1,3],[2,3],[1,2],[]]思路:...原创 2019-08-17 19:05:49 · 391 阅读 · 0 评论 -
正整数分解使得乘积最大问题
问题描述:设n是一个正整数。现在要求将n分解为若干个自然数之和,使得自然数的乘积最大。输出这个最大的乘积。这类问题一般有两种问法:(1)要求这些自然数互不相同(2)要求这些自然数可以是相同的对于第一种:要求自然数互不相同。这类题一开始需要我们手写几个数来看看规律,从5开始写起,5=2+3,6=2+4,7=3+4,8=3+5,9=2+3+4,10=2+3+5,11=2+4+5发现规律如...原创 2019-08-17 10:34:46 · 2548 阅读 · 0 评论 -
LeetCode 704. 二分查找
题目链接:https://leetcode-cn.com/problems/binary-search/submissions/给定一个 n 个元素有序的(升序)整型数组 nums 和一个目标值 target ,写一个函数搜索 nums 中的 target,如果目标值存在返回下标,否则返回 -1。示例 1:输入: nums = [-1,0,3,5,9,12], target = 9输出...原创 2019-08-16 21:58:30 · 190 阅读 · 0 评论 -
202. 快乐数
题目链接:https://leetcode-cn.com/problems/happy-number/description:编写一个算法来判断一个数是不是“快乐数”。一个“快乐数”定义为:对于一个正整数,每一次将该数替换为它每个位置上的数字的平方和,然后重复这个过程直到这个数变为 1,也可能是无限循环但始终变不到 1。如果可以变为 1,那么这个数就是快乐数。示例:输入: 19输出:...原创 2019-08-25 20:44:55 · 185 阅读 · 0 评论 -
最优打字策略(京东笔试)
description:在英文的输入中,我们经常会遇到大小写切换的问题,频繁切换大小写会增加我们的按键次数,也会降低我们的打字效率。 众所周知,切换大小写有两种方式,一种是按下"caps locks",也就是大写锁定键,这样一来,之后的输入模式都会被切换。另一种是同时按下shift和需要打印的字母,可以临时切换大小写(算作按下两个键)。已知初始状态下,打字模式是小写,现在给出需要打印的字符串(...原创 2019-08-25 09:18:23 · 1301 阅读 · 0 评论 -
Min Stack
题目链接Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Ge...原创 2019-01-21 20:40:35 · 177 阅读 · 0 评论 -
Shuffle an Array
题目链接Shuffle a set of numbers without duplicates.Example:// Init an array with set 1, 2, and 3.int[] nums = {1,2,3};Solution solution = new Solution(nums);// Shuffle the array [1,2,3] and return...原创 2019-01-21 19:36:49 · 317 阅读 · 0 评论 -
Reverse Linked List
题目链接Reverse a singly linked list.Example:Input: 1->2->3->4->5->NULLOutput: 5->4->3->2->1->NULLFollow up:A linked list can be reversed either iteratively or recurs...原创 2018-12-20 22:01:14 · 132 阅读 · 0 评论 -
Remove Nth Node From End of List
Given a linked list, remove the n-th node from the end of list and return its head.Example:Given linked list: 1-&gt;2-&gt;3-&gt;4-&gt;5, and n = 2.After removing the second node from the end, the ...原创 2018-12-20 21:52:10 · 194 阅读 · 0 评论 -
Delete Node in a Linked List
题目链接Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Given linked list – head = [4,5,1,9], which looks like following:4 -> 5 -> 1 -&...原创 2018-12-19 16:57:33 · 162 阅读 · 0 评论 -
Valid Sudoku
题目链接Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:Each row must contain the digits 1-9 without repetition.Each column must c...原创 2018-12-03 20:25:12 · 355 阅读 · 0 评论 -
First Unique Character in a String
题目链接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 ma...原创 2018-12-06 11:21:38 · 181 阅读 · 0 评论 -
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 s...原创 2018-11-30 13:25:03 · 152 阅读 · 0 评论 -
Move Zeroes
题目链接Given an array nums, write a function to move all 0’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...原创 2018-11-29 21:26:44 · 123 阅读 · 0 评论 -
8. String to Integer (atoi)
题目链接import reclass Solution: def myAtoi(self, str): # 首先取出字符前面的空格 str2 = str for index, item in enumerate(str): if item == ' ': str2 = str[i...原创 2018-11-19 23:32:14 · 154 阅读 · 0 评论 -
华为上机迷宫问题
题目描述定义一个二维数组N*M(其中2<=N<=10;2<=M<=10),如5 × 5数组下所示:int maze[5][5] = { 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0,};它表示一个迷宫,其中的1表示墙...原创 2018-09-19 20:48:29 · 490 阅读 · 0 评论 -
统计字符
题目描述给定一个英文字符串,请写一段代码找出这个字符串中首先出现三次的那个英文字符(需要区分大小写)。输入描述:输入数据一个字符串,包括字母,数字等。输出描述:输出首先出现三次的那个英文字符示例1输入Have you ever gone shopping and输出e代码实现:string = input()i = 0while i&lt;len(string):...原创 2018-09-19 20:44:31 · 271 阅读 · 0 评论 -
句子翻转
题目描述给定一个句子(只包含字母和空格), 将句子中的单词位置反转,单词用空格分割, 单词之间只有一个空格,前后没有空格。 比如: (1) “hello xiao mi”-&gt; “mi xiao hello”输入描述:输入数据有多组,每组占一行,包含一个句子(句子长度小于1000个字符)输出描述:对于每个测试示例,要求输出句子中单词反转后形成的句子示例1输入hello xiao...原创 2018-09-19 20:41:26 · 617 阅读 · 0 评论