53. 最大子序和

2019-12-08
阅读 1 分钟
934
给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。示例: 输入: [-2,1,-3,4,-1,2,1,-5,4],输出: 6解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。进阶: 如果你已经实现复杂度为 O(n) 的解法,尝试使用更为精妙的分治法求解。

52. N皇后 II

2019-12-07
阅读 1 分钟
1.4k
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 上图为 8 皇后问题的一种解法。 给定一个整数 n,返回 n 皇后不同的解决方案的数量。 示例: 输入: 4输出: 2解释: 4 皇后问题存在如下两个不同的解法。[ [".Q..",  // 解法 1  "...Q",  "Q...",  "..Q."],  ["..Q.",  // 解法 2...

51. N皇后

2019-12-07
阅读 3 分钟
970
n 皇后问题研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。 上图为 8 皇后问题的一种解法。 给定一个整数 n,返回所有不同的 n 皇后问题的解决方案。 每一种解法包含一个明确的 n 皇后问题的棋子放置方案,该方案中 'Q' 和 '.' 分别代表了皇后和空位。 示例: 输入: 4输出: [ [".Q..", //...

50. Pow(x, n)

2019-12-06
阅读 1 分钟
1k
实现 pow(x, n) ,即计算 x 的 n 次幂函数。示例 1: 输入: 2.00000, 10输出: 1024.00000示例 2: 输入: 2.10000, 3输出: 9.26100示例 3: 输入: 2.00000, -2输出: 0.25000解释: 2-2 = 1/22 = 1/4 = 0.25 先正向想到了递归解法,注意处理n=Integer.MIN_VALUE

49. 字母异位词分组

2019-12-04
阅读 1 分钟
1.5k
给定一个字符串数组,将字母异位词组合在一起。字母异位词指字母相同,但排列不同的字符串。示例: 输入: ["eat", "tea", "tan", "ate", "nat", "bat"],输出:[ ["ate","eat","tea"], ["nat","tan"], ["bat"]] 主要是如何分组效率最高,用hashmap,需要的解决问题是通过什么来判断equal,最简单是使用String

48. 旋转图像

2019-12-01
阅读 1 分钟
955
给定一个 n × n 的二维矩阵表示一个图像。将图像顺时针旋转 90 度。 说明: 你必须在原地旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要使用另一个矩阵来旋转图像。 示例 1: 给定 matrix = [ [1,2,3], [4,5,6], [7,8,9]], 原地旋转输入矩阵,使其变为:[ [7,4,1], [8,5,2], [9,6,3]]示例 2: 给定 matrix =[ [ 5...

47. 全排列 II

2019-11-30
阅读 1 分钟
1.1k
给定一个可包含重复数字的序列,返回所有不重复的全排列。示例: 输入: [1,1,2]输出:[ [1,1,2], [1,2,1], [2,1,1]] 与上道题的区别是有重复的数,结果里很容易重复

46. 全排列

2019-11-30
阅读 1 分钟
892
给定一个没有重复数字的序列,返回其所有可能的全排列。示例: 输入: [1,2,3]输出:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]] dfs的递归方法

45. 跳跃游戏 II

2019-11-30
阅读 2 分钟
1k
给定一个非负整数数组,你最初位于数组的第一个位置。数组中的每个元素代表你在该位置可以跳跃的最大长度。 你的目标是使用最少的跳跃次数到达数组的最后一个位置。 示例: 输入: [2,3,1,1,4]输出: 2解释: 跳到最后一个位置的最小跳跃数是 2。  从下标为 0 跳到下标为 1 的位置,跳 1 步,然后跳 3 步到达数组的最后一个...

44. 通配符匹配

2019-11-29
阅读 2 分钟
1.1k
给定一个字符串 (s) 和一个字符模式 (p) ,实现一个支持 '?' 和 '*' 的通配符匹配。'?' 可以匹配任何单个字符。'*' 可以匹配任意字符串(包括空字符串)。两个字符串完全匹配才算匹配成功。 说明: s 可能为空,且只包含从 a-z 的小写字母。p 可能为空,且只包含从 a-z 的小写字母,以及字符 ? 和 *。示例 1: 输入:s = "a...

43. 字符串相乘

2019-11-29
阅读 2 分钟
855
给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式。示例 1: 输入: num1 = "2", num2 = "3"输出: "6"示例 2: 输入: num1 = "123", num2 = "456"输出: "56088"说明: num1 和 num2 的长度小于110。num1 和 num2 只包含数字 0-9。num1 和 num2 均不以零开头,除...

42. 接雨水

2019-11-25
阅读 4 分钟
743
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。 示例: 输入: [0,1,0,2,1,0,1,3,2,1,2,1]输出: 6

41. 缺失的第一个正数

2019-11-25
阅读 1 分钟
805
给定一个未排序的整数数组,找出其中没有出现的最小的正整数。 示例 1: 输入: [1,2,0]输出: 3示例 2: 输入: [3,4,-1,1]输出: 2示例 3: 输入: [7,8,9,11,12]输出: 1说明: 你的算法的时间复杂度应为O(n),并且只能使用常数级别的空间。 先用HashSet记录所有正数的值,从1开始找,最差情况就是第n个都是 {代码...} 想法是先...

40. 组合总和 II

2019-11-25
阅读 2 分钟
628
给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用一次。 说明: 所有数字(包括目标数)都是正整数。解集不能包含重复的组合。 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8,所求解集为:[ [1, 7], [1, 2...

39. 组合总和

2019-11-24
阅读 1 分钟
889
给定一个无重复元素的数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。candidates 中的数字可以无限制重复被选取。 说明: 所有数字(包括 target)都是正整数。解集不能包含重复的组合。 示例 1: 输入: candidates = [2,3,6,7], target = 7,所求解集为:[ [7], [2,2,3]]示...

38. 报数

2019-11-24
阅读 1 分钟
1.3k
报数序列是一个整数序列,按照其中的整数的顺序进行报数,得到下一个数。其前五项如下: 1 11 21 1211 111221 1 被读作  "one 1"  ("一个一") , 即 11。11 被读作 "two 1s" ("两个一"), 即 21。21 被读作 "one 2",  "one 1" ("一个二" ,  "一个一") , 即 1211。 给定一个正整数 n(1 ≤ n ≤ 30),输出报数序列的第 n ...

37. 解数独

2019-11-24
阅读 2 分钟
1.5k
编写一个程序,通过已填充的空格来解决数独问题。一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。空白格用 '.' 表示。 一个数独。 答案被标成红色。 Note: 给定的数独序列只包含数字 1-9 和字符 '.' 。你可以假...

36. 有效的数独

2019-11-24
阅读 2 分钟
1.1k
判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。 数字 1-9 在每一行只能出现一次。数字 1-9 在每一列只能出现一次。数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 上图是一个部分填充的有效的数独。 数独部分空格内已填入了数字,空白格用 '.' 表示。 示例 1: 输入:[ ["...

35. 搜索插入位置

2019-11-24
阅读 1 分钟
859
给定一个排序数组和一个目标值,在数组中找到目标值,并返回其索引。如果目标值不存在于数组中,返回它将会被按顺序插入的位置。你可以假设数组中无重复元素。 示例 1: 输入: [1,3,5,6], 5输出: 2示例 2: 输入: [1,3,5,6], 2输出: 1示例 3: 输入: [1,3,5,6], 7输出: 4示例 4: 输入: [1,3,5,6], 0输出: 0

34. 在排序数组中查找元素的第一个和最后一个位置

2019-11-24
阅读 1 分钟
1.1k
给定一个按照升序排列的整数数组 nums,和一个目标值 target。找出给定目标值在数组中的开始位置和结束位置。你的算法时间复杂度必须是 O(log n) 级别。 如果数组中不存在目标值,返回 [-1, -1]。 示例 1: 输入: nums = [5,7,7,8,8,10], target = 8输出: [3,4]示例 2: 输入: nums = [5,7,7,8,8,10], target = 6输出: [-1...

33. Search in Rotated Sorted Array

2019-11-24
阅读 1 分钟
851
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e.,[0,1,2,4,5,6,7]might become[4,5,6,7,0,1,2]). You are given a target value to search. If found in the array return its index, otherwise return-1. You may assume no duplicate exists in the array. You...

[Leetcode]32.Longest Valid Parentheses

2019-11-23
阅读 3 分钟
1.1k
Given a string containing just the characters'('and')', find the length of the longest valid (well-formed) parentheses substring.Example 1:Input: "(()"Output: 2Explanation: The longest valid parentheses substring is"()"Example 2:Input: ")()())"Output: 4Explanation: The longest valid parentheses s...

[LeetCode]31. Next Permutation

2019-06-17
阅读 1 分钟
1.1k
Implement next permutation, which rearranges numbers into thelexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as thelowest possible order (ie, sorted in ascending order). The replacement must be in-place and use only constant extra me...

[LeetCode]30.Substring with Concatenation of All Words

2019-06-13
阅读 3 分钟
1k
You are given a string, s, and a list of words, words, that are all ofthe same length. Find all starting indices of substring(s) in s thatis a concatenation of each word in words exactly once and without anyintervening characters.Example 1: Input: s = "barfoothefoobarman", words = ["foo","bar"] O...

[LeetCode]29.Divide Two Integers

2019-06-12
阅读 2 分钟
1.3k
Given two integers dividend and divisor, divide two integers withoutusing multiplication, division and mod operator.Return the quotient after dividing dividend by divisor. The integer division should truncate toward zero. Example 1: Input: dividend = 10, divisor = 3 Output: 3 Example 2: Input: di...

[LeetCode]28. Implement strStr()

2019-06-11
阅读 2 分钟
1k
Implement strStr().Return the index of the first occurrence of needle in haystack, or -1if needle is not part of haystack. Example 1: Input: haystack = "hello", needle = "ll" Output: 2 Example 2: Input: haystack = "aaaaa", needle = "bba" Output: -1 Clarification: What should we return when needle...

[LeetCode]27. Remove Element

2019-06-08
阅读 2 分钟
882
Given an array nums and a value val, remove all instances of thatvalue in-place and return the new length. Do not allocate extra space for another array, you must do this bymodifying the input array in-place with O(1) extra memory. The order of elements can be changed. It doesn't matter what you ...

[LeetCode]26. Remove Duplicates from Sorted Array

2019-06-08
阅读 2 分钟
922
Given a sorted array nums, remove the duplicates in-place such thateach element appear only once and return the new length. Do not allocate extra space for another array, you must do this bymodifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,2], Your function s...

[LeetCode]25. Reverse Nodes in k-Group

2019-06-08
阅读 2 分钟
1k
Given a linked list, reverse the nodes of a linked list k at a timeand return its modified list.k is a positive integer and is less than or equal to the length of thelinked list. If the number of nodes is not a multiple of k thenleft-out nodes in the end should remain as it is. Example: Given thi...

[LeetCode]24. Swap Nodes in Pairs

2019-06-07
阅读 1 分钟
1.1k
Given a linked list, swap every two adjacent nodes and return itshead.You may not modify the values in the list's nodes, only nodes itselfmay be changed. Example: Given 1->2->3->4, you should return the list as 2->1->4->3.