LeetCode Blog for course "Algorithms" -- Problem 3 & 5
来源:互联网 发布:三星系统升级软件 编辑:程序博客网 时间:2024/06/01 20:02
Given a string, find the length of the longest substring without repeating characters.
Examples:
Given
"abcabcbb"
, the answer is "abc"
, which the length is 3.Given
"bbbbb"
, the answer is "b"
, with the length of 1.Given
"pwwkew"
, the answer is "wke"
, with the length of 3. Note that the answer must be a substring, "pwke"
is a subsequence and not a substring.My solution in Python:
1 2 3 4 5 6 7 8 9101112131415
class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ answer = 0; left = 0; last = {}; for i in range(len(s)): if s[i] in last and last[s[i]] >= left: left = last[s[i]] +1; last[s[i]] = i; answer = max(answer, i - left + 1); return answer;
Review:
We make good use of the “dictionary” in Python here. Starting with the first character in the string, we go through the string characters one by one, and store one item in the dictionary, with the character being the key, and the place it appears in the string being the value. When we encounter a character that has already appeared previously, we immediately know because this particular key (the character) is already in the dictionary. Thus we move the starting character of the substring to the next position of which the repeated character first appeared in the original string. We then compare the length of the current non-repeated-character-substring with the longest substring we already know. When “i” reaches the last character of the original string, the “answer” should be the length of the longest substring without repeated character.
Problem 5. Longest Palindromic Substring
Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000.
Example 1:
Input: “babad”
Output: “bab”
Note: “aba” is also a valid answer.
Example 2:
Input: “cbbd”
Output: “bb”
My solution in Python:
1 2 3 4 5 6 7 8 910111213141516171819202122232425
class Solution(object): def longestPalindrome(self, s): """ :type s: str :rtype: str """ ansl, ansr, maxx = 0, 0, 0 length = len(s) for i in range(1, length * 2): if i & 1 : left = i / 2 right = left else : left = i / 2 - 1 right = left + 1 while (left >= 0) and (right < length) and (s[left] == s[right]): left -= 1 right += 1 left += 1 right -= 1 if right - left > maxx: maxx = right - left ansl = left ansr = right return s[ansl: ansr + 1]
Review:
We observe that a palindrome mirrors around its center. Therefore, a palindrome can be expanded from its center, and there are only 2n-1 such centers. By treating odd “i” and even “i” differently, we are able to examine both palindromes with even length and with odd length. This question is actually very interesting and has many (at least 5) different solutions, each with different time and space efficiency. The most advanced one, the Manacher’s algorithm, can be found here.
- LeetCode Blog for course "Algorithms" -- Problem 3 & 5
- LeetCode Blog for course "Algorithms" -- Problem 6
- LeetCode Blog for course "Algorithms" -- Problem 7
- LeetCode Blog for course "Algorithms" -- Problem 8
- LeetCode Blog for course "Algorithms" -- Problem 9
- LeetCode Blog for course "Algorithms" -- Problem 11
- LeetCode Blog for course "Algorithms" -- Problem 1 & 2
- 算法设计Week4 LeetCode Algorithms Problem #207 Course Schedule
- Leetcode Blog Post Algorithms
- LeetCode Algorithms 210. Course Schedule II
- LeetCode-algorithms 34. Search for a Range
- LeetCode Algorithms 34. Search for a Range
- Machine Learning Algorithms: Which One to Choose for Your Problem
- 算法设计Week1 LeetCode Algorithms Problem #1 Two Sum
- 算法设计Week1 LeetCode Algorithms Problem #2 Add Two Numbers
- 算法设计Week3 LeetCode Algorithms Problem #133 Clone Graph
- 算法设计Week6 LeetCode Algorithms Problem #455 Assign Cookies
- 算法设计Week7 LeetCode Algorithms Problem #53 Maximum Subarray
- HDOJ2031(十进制变X进制)
- Hql
- [python]leetcode(22). Generate Parentheses
- Python练手程序-04
- 图片上传到网络上
- LeetCode Blog for course "Algorithms" -- Problem 3 & 5
- windows 保存git的账号密码
- CodeForces
- 开源C++服务端知多少
- Java-比较操作
- JZOJ 5389. 【NOIP2017提高A组模拟9.26】解梦
- 《深度学习Ng》课程学习笔记03week1——机器学习(ML)策略(1)
- bzoj 3932 [CQOI2015]任务查询系统 主席树
- 普通Java程序员学习使用的6个JDK内建工具