leetcode31.Next Permutation
题目要求
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place, do not allocate extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
翻译过来就是说,输入一个整数数组,该整数数组按照下标顺序代表一个数字,例如[1,2,3]代表整数123,要求改变数组中元素的顺序,找到比当前数字大的生成数中的最小值。如果当前数字代表的整数值已经是所有排列组合中的最大值,则返回当前数字组成的最小值。
思路分析
如何才能找到一个值,不是过大也不是过小呢?其实,找到这几个数字所有排列组合的可能性,然后再和当前数字一一进行比较其实也是一个思路。可是这意味着大量无用的数字的生成和比较。想要知道如何生成所有排列组合结果,可以参考我的这篇博客
换句话说,我们要尽量用最少的比较的到最小的结果。一个数字中的各个位上的数如何调整顺序才能获得一个最小的更大值。首先,可以将低位移到高位,只要低位的数字比高位上的数字大。但是,为了确保的到尽可能小的最大值,一定要将移动的位确保越小越好。其次,要保证移动之后,高位以后的值为最小值。
综上所述,思路如下:
- 由最低位(下表为nums.length-2)开始,由低往高遍历,找到可以进行替换的最小位。
- 可以替换的条件是,比当前位低的位上存在一个数,该数比当前位上的值大,且不存在另一个比该值小且比当前位上值大的数
- 替换数值后,该进行替换的位后序的位上的值应当保证为最小值。
例如[1,3,2],将百位上的值和个位上的值替换以后,还要保证百位以后的值为最小值,所以最后的结果为[2,1,3]
实现代码如下
1 | public void nextPermutation(int[] nums) { |
leetcode47.Permutations II
题目要求
1 | Given a collection of numbers that might contain duplicates, return all possible unique permutations. |
对于其基础题PermutationsI请参考我的另一篇博客
这里添加的难度在于,排列组合的数字中可能存在重复。这就需要想方法,将结果集中重复的结果删去。而这里,我参考了另一名答题者的答案,在试图将数字添入结果集中时,就判断会不会产生重复的结果,从而使避免重复。
leetcode30.Substring with Concatenation of All Words
题目要求
1 | 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(s) in s that is a concatenation of each word in words exactly once and without any intervening characters. |
输入一个字符串s和一个字符串数组words,其中words中的每个word的长度都相等。在字符串中找到所有子字符串的起始下标,只要该子字符串满足words中所有单词的连接结果(顺序无关)
leetcode22.Generate Parentheses
leetcode88.Merge Sorted Array
题目要求
1 | Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. |
将两个有序数组合并至其中一个数组并且该新数组仍然有序。
leetcode55.Jump Game
题目要求
1 | Given an array of non-negative integers, you are initially positioned at the first index of the array. |
翻译过来就是,假设有一个数组,该数组中的元素全部都是非负整数。当前起点为数组中下标为零的位置,要走到数组的最后一个下标。其中,数组中每一个元素代表当前下标下可以前进的最大步数。如果可以从起点走向终点,那么返回true,否则返回false
leetcode56.Merge Intervals
leetcode61.Rotate List
题目要求
1 | Given a list, rotate the list to the right by k places, where k is non-negative. |
其实这题的描述有些不妥当,确切的说是将最右侧的节点依次移到最左侧作为头结点,一直移动到右侧第k个节点后结束。
还是以1->2->3->4->5->NULL为例
k=1 结果为:5->1->2->3->4->NULL
k=2 结果为:4->5->1->2->3->NULL
k=3 结果为:3->4->5->1->2->NULL
k=4 结果为:2->3->4->5->1->NULL
k=5 结果为:1->2->3->4->5->NULL
k=6 结果为:5->1->2->3->4->NULL
换句话说 当k的值超过了list的长度len的时候,移动k次等价于移动k%len次
leetcode42.Trapping Rain Water
题目要求
1 | Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining. |
假设这些是一些间隔的木板,问最多能够装多少水。也就是一个区域性的短板问题。其实一个区间能够乘的最大水量,取决于它的左右最近且最高的木板的长度。当然除了通过多个区间的和来计算总体的盛水量,还可以通过横向的划分来计算盛水量。这些将在接下来中的代码一一分析。官方也提供了一些答案,这里将给出相应的java实现的版本。