LeetCode
文章平均质量分 64
Neko36
这个作者很懒,什么都没留下…
展开
-
[LeetCode]Longest Substring Without Repeating Characters
此题用一个hash table保存每个字符上一次出现过的位置。从前往后扫描,假如发现字符上次出现过,就把当前子串的起始位置start移动到上次出现过的位置之后。同时,由于start移动,当前子串的内容改变,start移动过程中经历的字符都要剔除。class Solution {public: int lengthOfLongestSubstring(string s) {原创 2013-10-08 14:51:33 · 2869 阅读 · 1 评论 -
[LeetCode]Two Sum
Two Sum这题思路很简单,创建一个hash table,用来存放访问过的数值和对应的位置。从头到尾循环,假如hash table里面有匹配的数值,则取出对应的数值的位置并直接返回。Given an array of integers, find two numbers such that they add up to a specific target number.The f原创 2013-10-08 13:36:06 · 1599 阅读 · 0 评论 -
[LeetCode]Median of Two Sorted Arrays
Median of Two Sorted Arrays这题挺难的,边界条件老是考虑不清楚。所以在这里记录一下思路,免得将来忘记。There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time原创 2013-10-08 14:17:59 · 1199 阅读 · 1 评论 -
[LeetCode]Add Two Numbers
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */class Solution {public: ListNode *ad原创 2013-10-09 00:06:50 · 1110 阅读 · 0 评论 -
[LeetCode]Longest Palindromic Substring
Longest Palindromic Substring此题用Manacher算法解答,时间O(n),空间O(n).首先在字符串的首尾,以及每个字符之间加上'#',形成新字符串s1,这样可以统一处理子串为奇数或偶数的情况。我们用一个数组p来保存s1字符串中以每个字符为中心时,左右对称扩展的最大值,即以该字符为中心时的最大对称长度。例如:#a#b#b#a#,最中间的'#'的p值为4。接着我们扫描所原创 2013-10-09 14:09:54 · 1269 阅读 · 0 评论 -
[LeetCode]ZigZag Conversion
这题没什么意思,就是纯粹的找规律。发现所有行的重复周期都是 2 * nRows - 2对于首行和末行之间的行,还会额外重复一次,重复的这一次距离本周期起始字符的距离是 2 * nRows - 2 - 2 * iclass Solution {public: string convert(string s, int nRows) { // Start原创 2013-10-09 15:01:22 · 5705 阅读 · 2 评论 -
[LeetCode]Reverse Integer
这题啥也不说了,简单…… 记一下代码。要通过测试,主要就是处理一下正负号。class Solution {public: int reverse(int x) { // Note: The Solution object is instantiated only once and is reused by each test case.原创 2013-10-10 02:39:48 · 748 阅读 · 0 评论 -
[LeetCode]String to Integer
class Solution {public: int atoi(const char *str) { // Note: The Solution object is instantiated only once and is reused by each test case./* * input: space, sign, digits, int limit */原创 2013-10-11 02:52:59 · 819 阅读 · 0 评论