leetcode5.Longest Palindromic Substring
题目要求
1 | Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. |
翻译过来就是说:在一个字符串中寻找最长的子字符串,该字符串是回数(即从左往右和从右往左读的结果是相同的)。该字符串的最大长度为1000
1 | Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. |
翻译过来就是说:在一个字符串中寻找最长的子字符串,该字符串是回数(即从左往右和从右往左读的结果是相同的)。该字符串的最大长度为1000
1 | The count-and-say sequence is the sequence of integers beginning as follows: |
英文的题目有点绕口,所以去网上找了一下题目的意思。
题目的核心逻辑在于将口语化的数数字转化为字符串。
例如
如果一串数字是1,则是1个1,对应的字符串为11
如果一串数字是11,则是2个1,对应的字符串为21
如果一串数字是1211,则是1个1,接着1个2,接着2个1,对应的字符串为111221
题目中输入n,则方法对第n-1个数数字结果再一次进行数数字,并返回字符串
所以输入1对应1,输入2则是对前一个1进行数数字得到11(1个1),输入3则是对11进行数数字得到21(2个1),并以此类推下去
1 | Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. |
输入一个整数数组,从中找到所有的三个整数组成一个数组,这三个整数的和为0。要求不包括重复的结果
1 | Given a linked list, remove the nth node from the end of list and return its head. |
题意就是,从链表中移除倒数第n个节点(前提是这个被移除的节点一定存在)
鉴于这一题的核心思路和leetcode15的思路相同,可以先写一下15题并参考一下我之前的一篇博客
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
1 | For example, given array S = {-1 2 1 -4}, and target = 1. |
翻译过来就是,假设一个有n的元素的整数数组S,从S中找到三个值,这三个值的距离输入的目标值最近。返回这三个值的和。
1 | Given a digit string, return all possible letter combinations that the number could represent. |
1 | Input:Digit string "23" |
也就是说,将数字对应的字母的排列组合的的所有可能结果都枚举出来,顺序不唯一。
leetcode给这道题目加了一个标签很有意思,叫做Backtracking。点进去之后又很多类似的题目。这种类型的题目一般需要求出上一种情况的前提下才可以得知下一种情况。比如我必须先得知按下“2”后所有字母的可能值,才能在计算出“22”对应的可能值
1 | Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. |
翻译过来就是说:一个由小到大有序排列的数组被分为两个子数组,这两个子数组调换前后顺序生成一个新数组。在新数组中找到目标值并返回下标
左右同时比较,直至找到目标值或是左右指针相遇
1 | public int search(int[] nums, int target){ |
思路一并没有充分利用条件,及这个数组是由两个有序的子数组合成的。这里我们可以使用二分法的一个变形的算法。先找到中间节点,这个中间节点如果不是在左顺序子数组,就一定在右顺序子数组,反之亦成立。这样我们就可以变相的使用二分法将区间范围逐渐缩小,直至找到目标值。
这里相比于思路一,更适用于目标节点在中间的情况,而思路一在目标节点分布在数组两侧会效率更高。
1 | public int search(int[] nums, int target){ |
1 | Given a linked list, swap every two adjacent nodes and return its head. |
翻译过来就是:将链表中相邻两个节点交换顺序,并返回最终的头节点。