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 ...
随机推荐
- jvm terminated. exit code=13
The -vm option and its value (the path) must be on separate lines. The value must be the full absolu ...
- poj3311 Hie with the Pie (状态压缩dp,旅行商)
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3160 Accepted: 1613 ...
- Tomcat安装及配置教程
工具/原料 Tomcat7.0 eclipse 百度经验:jingyan.baidu.com java环境的配置 1 java环境的配置应该都学过吧,这里简单的讲一下. 下载安装java JDK,注意 ...
- google垂直搜索结果
佰年金融 所谓“垂直”很干净地分离出来,而不是在谷歌算作有机列表的结果,但该行已开始模糊.例如,现在许多视频结果似乎是直接整合为有机(万SERP的例子).我治疗的新的“有深度的文章”作为一个垂直的结果 ...
- javascript照片墙效果
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- PostgreSQL9.2安装和配置指南
本文只介绍PostgreSQL9.2在centos上的安装和配置过程 1.执行yum 命令安装PostgreSQL yum install postgresql-server 2.初始化Postgre ...
- 初步理解JNDI
今天初步学习了jndi的基本原理,主要是 学习了收藏中的一篇博文,讲的很好,现在记录一下自己的理解. 其实jndi就和jdbc很相似, 我们希望通过相同的jdbc api来访问不同的数据库,就必须提供 ...
- windows 安装mysql 步骤
Windows 安装mysql 5.7.12教程 1.在官网下载mysql5.7.12.zip并解压 复制默认配置文件my-default.ini,并命名为my.ini 使用记事本打开,修改如下配置 ...
- 【Python全栈笔记】01 [模块二] 14-15 Oct 运算符和字符串 4-1
编码的问题 中文版 Windows 终端是默认GBK编码格式,python2默认为ASCII编码,虽然我们注释了需要用UTF-8,但是通过命令行运行的文件如果print中文内容还是会出现乱码. Uni ...
- 纯JS实现中国行政区域上下联动选择地址
一.实现目的: 如标题所述,通过JS来实现地址的选取,上一篇博客介绍的方式是通过java读取txt资源文件来实现地址的选择,通过ajax方式访问服务器实现省市区联动.此篇中将介绍如何使用JS实现相同功 ...