leetcode problem sum
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) {
int sum = ;
ListNode a();
ListNode* NODE = &a;
while(l1 || l2 || sum){
if(l1){
sum += l1->val;
l1 = l1->next;
}
if(l2){
sum += l2->val;
l2 = l2->next;
}
NODE->next = new ListNode(sum % );
NODE = NODE->next;
sum /= ;
}
return a.next;
}
};
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.
c++:
暴力O(N2):Your runtime beats 24.22% of cpp submissions.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int repeat[], max = -, temp = ;
int len = s.size();
if(len == ) return ;
for(int i = ; i < len; i++){
temp = ;
for(int j = ; j < ; j++) repeat[j] = ;
for(int k = i; k < len; k++){
int l = s[k] - ' ';
//cout<< " l " << l << endl;
if(repeat[l] >= ) break;
if(repeat[l] == ) {temp++;repeat[l]++;}
}
//cout<< temp <<endl;
if(temp >= max) max = temp;
}
return max;
}
};
O(n):Your runtime beats 87.46% of cpp submissions.
class Solution {
public:
int lengthOfLongestSubstring(string s) {
int nmap[];
memset(nmap, -, sizeof(nmap));
int maxLength = , lastRepeatPosition = -, len = s.size();
for (int curPosition = ; curPosition != len; curPosition++) {
int position = nmap[s[curPosition]];
if (position > lastRepeatPosition)
lastRepeatPosition = position;
nmap[s[curPosition]] = curPosition;
maxLength = max(maxLength, curPosition - lastRepeatPosition);
}
return maxLength;
}
};
129. Sum Root to Leaf Numbers
- Total Accepted: 93320
- Total Submissions: 269426
- Difficulty: Medium
- Contributors: Admin
Given a binary tree containing digits from 0-9 only, each root-to-leaf path could represent a number.
An example is the root-to-leaf path 1->2->3 which represents the number 123.
Find the total sum of all root-to-leaf numbers.
For example,
1
/ \
2 3
The root-to-leaf path 1->2 represents the number 12.
The root-to-leaf path 1->3 represents the number 13.
Return the sum = 12 + 13 = 25.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int sumNumbers(TreeNode* root) {
int sum_current = , total = ;
if(!root) return ;
return DFS(root, sum_current, total);
} int DFS(TreeNode* current, int sum, int total){
if(current == nullptr)
return ;
if(current != nullptr)
sum = sum * + current->val;
if(current->left == nullptr && current->right == nullptr){
total += sum;
return total;
}
return DFS(current->left, sum, total) + DFS(current->right, sum, total);
}
};
leetcode problem sum的更多相关文章
- LeetCode:Path Sum I II
LeetCode:Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- 剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers)
剑指offer 65. 不用加减乘除做加法(Leetcode 371. Sum of Two Integers) https://leetcode.com/problems/sum-of-two-in ...
- LeetCode Problem 90. Subsets II
python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...
- LeetCode Problem 2:Two Sum
描述: Given an array of integers, find two numbers such that they add up to a specific target number. ...
- [LeetCode] Combination Sum IV 组合之和之四
Given an integer array with all positive numbers and no duplicates, find the number of possible comb ...
- [LeetCode] Max Sum of Rectangle No Larger Than K 最大矩阵和不超过K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
- [LeetCode] Combination Sum III 组合之和之三
Find all possible combinations of k numbers that add up to a number n, given that only numbers from ...
- Leetcode: Max Sum of Rectangle No Larger Than K
Given a non-empty 2D matrix matrix and an integer k, find the max sum of a rectangle in the matrix s ...
随机推荐
- 设计winform自带动态加载工具按钮和实现热键响应
1.初衷 主要是想设计一个自带添加工具按钮和按钮的快捷键的基窗体.这样以后所设计的窗体只要继承自这个窗体就可以实现热键响应和动态加工具按钮的功能了 写这边文章主要是为了以后使用的时候有个参考,因为这只 ...
- ReaderWriterLock的UpgradeToWriterLock方法的一种使用场景
ReaderWriterLock对比互斥锁(lock)的优势是,读锁和写锁的分离,读锁之间互不排斥. 当然,本文重点不是讲ReaderWriterLock本身,而是讲它的UpgradeToWriter ...
- 高级php面试题
在网上看到一些高级php 的面试题目.. 最近接连面试了几家公司,有些重要问题记录一下,督促自己学习提高,同时希望给朋友们一些帮助.内容很多,一点点完善,一步步学习..有些是面试被问,有些是招聘要求, ...
- select标签非空验证,第一个option value=""即可
select标签非空验证,第一个option value=""即可,否则不能验证
- 在Action 中访问web资源
1.什么是web资源: HttpServletRequest,HttpSession,ServletContext等原生的Servlet API. 2.为什么要访问web资源? B/S应用的Contr ...
- PHP开启cURL功能
PHP开启cURL功能 在php.ini中开启 确定php扩展目录下有php_curl.dll类库 在php.int中找到扩展库所在目录 判断目录下是否有php_curl.dll 没有的话去搜索下载 ...
- SVN冲突解决:当次提交的和上次提交的出现冲突
转载于知蚁博客,文章地址:http://www.letuknowit.com/archives/svn-conflict-resolution 看到那个*了吧,出现这个标记就说明本地副本的文件已经过期 ...
- 说说JSON和JSONP,也许你会豁然开朗(转)
前言 由于Sencha Touch 2这种开发模式的特性,基本决定了它原生的数据交互行为几乎只能通过AJAX来实现. 当然了,通过调用强大的PhoneGap插件然后打包,你可以实现100%的Socke ...
- 【POJ2828】Buy Tickets(线段树)
题意:有一个输入序列,每次操作要把b[i]插入到第a[i]个,在第a[i]个后面的要后移,问最后序列. n<=200000 思路:顺序来只能用splay维护 考虑倒序,对于插入到第K个位置,在线 ...
- Html+Ajax+Springmvc+Mybatis,不用JSP
有一个原因如下很合本人观点: http://bbs.csdn.net/topics/390939813 前端使用HTML+Ajax,后端使用Java Servlet,这样完全可以做到前后端分离,前端那 ...