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 ...
随机推荐
- Spark-Mllib(二)基本统计
一.基本统计量 统计向量的长度,最大值,最小值,非0个数,模1和模2,方差等 import org.apache.spark.mllib.linalg.{Vector,Vectors} import ...
- Samba安装配置
Samba简介 Samba官网:http://www.samba.orgSMB(Server Messages Block,信息服务块)是一种在局域网上共享文件和打印机的一种通信协议,它为局域网内的不 ...
- Office 365 系列五 -------- Imap邮箱迁移步骤
当客户购买了我们的Office 365之后,第一个功能必然是会用我们的企业邮箱,如果企业之前是用 263.腾讯.网易等的邮件厂商的话,必然会涉及到邮件的迁移, 其实说到邮箱迁移,我们办法很多,如果人数 ...
- C#连接Sql Serve数据库及增,删,改操作
一:连接. string sqlconn = "server=主机名;database=数据名;integrated security=true" //integrated sec ...
- 破解excel密码保护
破解excel密码保护 录制一个新宏.内容如下.保存后运行,点几次确定,过一分钟还会再弹出来,再点确定,然后就好了. Public Sub AllInternalPasswords() ' Break ...
- laravel(一):如何安装laravel
1.前提条件 本文针对想从零开始开发 Laravel 程序的初学者,不需要预先具备任何的 Laravel 使用经验.不过,为了能顺利阅读,还是需要事先安装好一些软件: PHP 5.4 及以上版本 包管 ...
- Python SQLAlchemy --3
本文為 Python SQLAlchemy ORM 一系列教學文: 刪除 學會如何查詢之後,就能夠進行後續的刪除.更新等操作. 同樣地,以幾個範例做為學習的捷徑. 123456789 user_1 = ...
- 判断当前VC是不是正在展示的活跃页面
viewController.isViewLoaded && viewController.view.window
- 由Struts return SUCCESS引发的基础问题
该问题的最初来源,是源于Struts中的 return SUCCESS; 和 return "success"; 在Struts的配置文件struts.xml我们可以找到" ...
- Dapper关联查询
1.一对一: using (IDbConnection connecton = new MySqlConnection(ConfigurationManager.ConnectionStrings[& ...