LeetCode(234) Palindrome Linked List】的更多相关文章

题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) time and O(1) space? 分析 判断一个链表是否为回文. 要求时间复杂度O(n) 空间复杂度O(1) 若是可用辅助空间,则其是一个简单的题目,我们可用利用一个辅助数组存储节点元素val值,然后判断数组是否回文即可. 题目要求O(1)的空间复杂度,不能用辅助空间.我们只能从链表本身下手,…
题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&q…
题目 Reverse a singly linked list. click to show more hints. Hint: A linked list can be reversed either iteratively or recursively. Could you implement both? 分析 反转链表. 一个简单的解法,既然反转该链表,我们把所有节点作为一个输入序列,按照头插法重新构造一个链表即可,它既是所给链表的反转结果. 题目所给提示还有另外两种方法解决,迭代和递归.…
题目 Reverse a linked list from position m to n. Do it in-place and in one-pass. For example: Given 1->2->3->4->5->NULL, m = 2 and n = 4, return 1->4->3->2->5->NULL. Note: Given m, n satisfy the following condition: 1 ≤ m ≤ n ≤…
题目: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could…
Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 一开始我的思路如下:回文子串的特点是首尾字母相同,所以我对每一个字母都找到位于它后面的相同字母,利用切片判断这一段是否为回文子串(str[i:j]==str[i:j][::-1]).时间复杂度很高,主要是因为str.find操作非常耗时. class Solution(object): def lo…
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimize your algorithm? 分析 同LeetCode(274)H-Index第二个版本,给定引用数量序列为递增的:这就省略了我们的第一个排序步骤: O(n)的时间复杂度,遍历一次即可. AC代码 class Solution { public: int hIndex(vector<int>…
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array such that the difference between nums[i] and nums[j] is at most t and the difference between i and j is at most k. 分析 题目描述:给定一个整数序列,查找是否存在两个下标分别为i和j的元…
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh…
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times).…