学而不思则罔,思而不学则殆。学习如逆水行舟,不进则退。平时工作比较繁忙,利用周六与周日空闲时间刷刷算法题,一方面保持编程的手感,一方面可以通过AC系统性地归纳总结,找到解决算法题目的一般思路和方法,当然,最重要的是,时刻准备战斗姿势,可以在机会到来时找到一份心仪的工作。话不多说,进入正题。
leetcode题目地址:https://leetcode.com/problemset/top-interview-questions/
2. Add Two Numbers
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
Example:
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
Explanation: 342 + 465 = 807.
解决思路:
常规比较即可。注意进位等操作。本题即实现一个链表形式的大数加法操作。类似的有字符串形式的大数加法操作。
代码省略。
--------------------------------------------------------------------------------------------------------------------------------------------------------
. 3、Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
解决思路:
思路比较简单。因为字符最多有256个,我们可以用char[256] 表示字符在子串中出现的个数。比如char['a']=2表示子串中a字符出现了2次。只有子串中有字符出现了2次,则记录该子串的长度。接着寻找下一个子串。
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int max_len = 0;
int max_index_first = 0;
int max_index_last = 0;
int cur_len = 0;
int cur_index_first = 0;
int cur_index_last = 0;
int string_count[256] = {0};
for (int i = 0; i < s.size(); i++)
{
string_count[s[i]]++;
if(string_count[s[i]] < 2)
{
cur_len++;
cur_index_last++;
}
else
{
if (cur_len > max_len)
{
max_len = cur_len;
max_index_first = cur_index_first;
max_index_last = cur_index_last;
}
while(string_count[s[i]] == 2)
{
int k = s[cur_index_first];
string_count[k]--;
cur_index_first++;
cur_len--;
}
cur_index_last++;
cur_len++;
}
}
if(cur_len > max_len)
{
max_len = cur_len;
}
return max_len;
}
};