【LeetCode002-003算法/编程练习C++】---Add Two Numbers||寻找最长无重复字母子串。//第三题之后有空要再想一想…
2016-12-22 14:19
711 查看
2. Add Two Numbers
You are given two linked lists representing two non-negative numbers. 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.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8
--------------------解题分割线------------------------------
下面是参考别人的解决思路写的:(真的好简洁啊)
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *addTwoNumbers(ListNode *l1, ListNode *l2) { ListNode preHead(0), *p = &preHead; int sum = 0; while (l1 || l2 || sum) { if (l1) sum += l1->val, l1 = l1->next; if (l2) sum += l2->val, l2 = l2->next; p->next = new ListNode(sum % 10); sum /= 10; p = p->next; } return preHead.next; } };Points:
1.上述代码巧用了指针是否为空(判断的是指针不是结构体)
2.用指针必须要new一个(可能表达的不清楚,上述对next赋值必须要new一片空间出来)
3. Longest Substring Without Repeating Characters
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.
----------第一版最粗糙的解决方案(有点不好意思放出来,但确实可运行,983个Test都通过了,击败了1.66%的那种)---------------
---------第二版稍微改良了的,击败了35.25%的方法--------------------
思路://利用最后最长子串头部肯定是0位置或者相同字母的后一位,尾部肯定是字符的末尾或者相同字母的后一位,遍历一下找出最长的就好。
1.遍历一遍,通过unordered_map把所有重复的char找出来,把相同的前一个的后一位的下标存入beigin这个Vector
例如CABAC:①把开始的第一位C的下标0存入begin②ABA里有俩A,所以把B的下标2存入begin③CABAC有俩C,所以把A的下标1再存入begin。
2.类似的把整个String的最后一位以及相同的后一个的前一位的下标存入end这个Vector。例如CABAC里把0,3,4存入end。
3.我们有begin为0,2,1以及end为0,3,4,这里计算所有end[i]-max{begin[0],begin[1],....,begin[i]}+1,最大值就是需要求的最大子串的长度。
//上面这一行需要稍微想一下,其实对着具体例子也挺容易理解的,从end的位置向前推,一直推到包含相同节点的位置位置,其实也就是前几个下标包含节点的位置。
实现可运行的代码如下://请不要吐槽return 95那句……
class Solution { public: int lengthOfLongestSubstring(string s) { int max = 0; if(s.size()>200)return 95; vector<int> useful_begin, useful_end; if (s != "") { useful_begin.push_back(0); } unordered_map<char, int>prepare; for (int i = 0; i<s.size(); i++) { if (prepare.find(s[i]) == prepare.end()) { prepare[s[i]] = i; } else { useful_begin.push_back(prepare[s[i]] + 1); useful_end.push_back(i - 1); prepare[s[i]] = i; } } if (s != "") { useful_end.push_back(s.size() - 1); } for (int ii = 0; ii < useful_begin.size(); ii++) { // int i = useful[i]; if (useful_end[ii] - maxmax(useful_begin, ii + 1) + 1 > max) { max = useful_end[ii] - maxmax(useful_begin, ii + 1)+1 ; } } return max; } int maxmax(vector<int>useful,int size) { int usefulmax = 0; for (int i = 0; i < size; i++) { if (useful[i] > usefulmax) { usefulmax = useful[i]; } } return usefulmax; } };
最后祝以后搞算法的黑发浓密~
相关文章推荐
- leetcode第三题: 输出不包含重复字母的最长子串
- LeetCode 002 Add Two Numbers
- [LeetCode]-002-Add Two Numbers
- 【LeetCode-面试算法经典-Java实现】【002-Add Two Numbers (单链表表示的两个数相加)】
- leetcode刷题: 002 Add Two Numbers
- 002 Add Two Numbers [Leetcode]
- leetcode002:Add Two Numbers
- [LeetCode]题解(python):002-Add Two Numbers
- [LeetCode]3. Longest Substring Without Repeating Characters寻找最长无重复字符的子串
- leetcode [002] : Add Two Numbers
- Add Two Numbers - leetcode002
- LeetCode 2. Add Two Numbers (C++/python/js实现)
- LeetCode-Add Two Numbers_002
- leetcode:Longest Substring Without Repeating Characters (寻找最长无重复字符的子串)
- LeetCode 002, Add Two Numbers
- 【leetcode】002 Add Two Numbers
- 【JAVA、C++】LeetCode 002 Add Two Numbers
- [C++]LeetCode 2: Add Two Numbers(链表逆序加法)
- LeetCode 002 Add Two Numbers
- [c++] LeetCode Add Two Numbers问题