leetcode: add two numbers problem

This commit is contained in:
Dmitriy Pleshevskiy 2023-05-15 11:26:58 +03:00
parent 6b0cdf9561
commit 5f7bcbc2f0
Signed by: pleshevskiy
GPG Key ID: 79C4487B44403985
3 changed files with 258 additions and 0 deletions

39
leetcode/2/README.md Normal file
View File

@ -0,0 +1,39 @@
# 2. Add Two Numbers (Medium)
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 contains a
single digit. Add the two numbers and return the sum as a linked list.
You may assume the two numbers do not contain any leading zero, except the
number 0 itself.
## Example 1:
![Add two number](assets/addtwonumber1.jpg)
```
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
```
## Example 2:
```
Input: l1 = [0], l2 = [0]
Output: [0]
```
## Example 3:
```
Input: l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]
Output: [8,9,9,9,0,0,0,1]
```
## Constraints:
- The number of nodes in each linked list is in the range [1, 100].
- 0 <= Node.val <= 9
- It is guaranteed that the list represents a number that does not have leading
zeros.

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

219
leetcode/2/rust.ipynb Normal file
View File

@ -0,0 +1,219 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"id": "feab2236-6685-4f83-9d50-054bf952f66e",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"// Definition for singly-linked list.\n",
"#[derive(PartialEq, Eq, Clone, Debug)]\n",
"pub struct ListNode {\n",
" pub val: i32,\n",
" pub next: Option<Box<ListNode>>\n",
"}\n",
"\n",
"impl ListNode {\n",
" #[inline]\n",
" fn new(val: i32) -> Self {\n",
" ListNode {\n",
" next: None,\n",
" val\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "65dd529b-49f6-4a98-b69d-12871b081a40",
"metadata": {},
"source": [
"# Idea 1: O(n + m)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "167e31a3-7844-4cdf-a563-241a40b07212",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"pub fn internal_add_two_numbers(extra_val: i32, l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n",
" match (extra_val, l1.clone(), l2.clone()) {\n",
" (0, None, None) => None,\n",
" (_, l1, l2) => {\n",
" let val =\n",
" l1.as_ref().map(|n1| n1.val).unwrap_or_default()\n",
" + l2.as_ref().map(|n2| n2.val).unwrap_or_default()\n",
" + extra_val;\n",
" let mut nn = ListNode::new(val % 10);\n",
" let extra_val = if val > 9 { 1 } else { 0 };\n",
" nn.next = internal_add_two_numbers(\n",
" extra_val,\n",
" l1.and_then(|n1| n1.next),\n",
" l2.and_then(|n2| n2.next)\n",
" );\n",
" Some(Box::new(nn))\n",
" }\n",
" }\n",
"}\n",
"\n",
"pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n",
" internal_add_two_numbers(0, l1, l2)\n",
"}"
]
},
{
"cell_type": "markdown",
"id": "f9dab3ca-0d78-4050-b94b-ad0d19348b09",
"metadata": {},
"source": [
"# Test Utils"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "33f9c78d-98f5-4d95-9ed0-e0c1961dc7b6",
"metadata": {
"tags": []
},
"outputs": [
{
"ename": "Error",
"evalue": "duplicate definitions with name `with_next`",
"output_type": "error",
"traceback": [
"\u001b[31m[E0592] Error:\u001b[0m duplicate definitions with name `with_next`",
" \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_17:1:1\u001b[38;5;246m]\u001b[0m",
" \u001b[38;5;246m│\u001b[0m",
" \u001b[38;5;246m3 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mw\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54mh\u001b[0m\u001b[38;5;54m_\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54m(\u001b[0m\u001b[38;5;54mv\u001b[0m\u001b[38;5;54ma\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54m3\u001b[0m\u001b[38;5;54m2\u001b[0m\u001b[38;5;54m,\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mL\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54ms\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54mN\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54md\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54m)\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54m-\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mS\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m",
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m ",
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m other definition for `with_next`",
"\u001b[38;5;246m───╯\u001b[0m"
]
},
{
"ename": "Error",
"evalue": "duplicate definitions with name `fromVec`",
"output_type": "error",
"traceback": [
"\u001b[31m[E0592] Error:\u001b[0m duplicate definitions with name `fromVec`",
" \u001b[38;5;246m╭\u001b[0m\u001b[38;5;246m─\u001b[0m\u001b[38;5;246m[\u001b[0mcommand_17:1:1\u001b[38;5;246m]\u001b[0m",
" \u001b[38;5;246m│\u001b[0m",
" \u001b[38;5;246m10 │\u001b[0m \u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54mr\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54mm\u001b[0m\u001b[38;5;54mV\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mc\u001b[0m\u001b[38;5;54m(\u001b[0m\u001b[38;5;54mv\u001b[0m\u001b[38;5;54m:\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mV\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54mc\u001b[0m\u001b[38;5;54m<\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54m3\u001b[0m\u001b[38;5;54m2\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m)\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54m-\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m \u001b[0m\u001b[38;5;54mO\u001b[0m\u001b[38;5;54mp\u001b[0m\u001b[38;5;54mt\u001b[0m\u001b[38;5;54mi\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54mn\u001b[0m\u001b[38;5;54m<\u001b[0m\u001b[38;5;54mB\u001b[0m\u001b[38;5;54mo\u001b[0m\u001b[38;5;54mx\u001b[0m\u001b[38;5;54m<\u001b[0m\u001b[38;5;54mS\u001b[0m\u001b[38;5;54me\u001b[0m\u001b[38;5;54ml\u001b[0m\u001b[38;5;54mf\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;54m>\u001b[0m\u001b[38;5;249m \u001b[0m\u001b[38;5;249m{\u001b[0m",
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m┬\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m ",
" \u001b[38;5;246m ·\u001b[0m \u001b[38;5;54m╰\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m\u001b[38;5;54m─\u001b[0m other definition for `fromVec`",
"\u001b[38;5;246m────╯\u001b[0m"
]
}
],
"source": [
"impl ListNode {\n",
" #[inline]\n",
" fn with_next(val: i32, next: ListNode) -> Self {\n",
" ListNode {\n",
" next: Some(Box::new(next)),\n",
" val,\n",
" }\n",
" }\n",
" \n",
" fn fromVec(v: Vec<i32>) -> Option<Box<Self>> {\n",
" v.into_iter()\n",
" .rev()\n",
" .fold(None, |acc, val| {\n",
" match acc {\n",
" None => Some(Box::new(ListNode::new(val))),\n",
" Some(node) => Some(Box::new(ListNode::with_next(val, *node)))\n",
" }\n",
" })\n",
" }\n",
"}\n",
"\n",
"assert_eq!(\n",
" ListNode::fromVec(vec![7, 0, 8]),\n",
" Some(Box::new(ListNode::with_next(7, ListNode::with_next(0, ListNode::new(8)))))\n",
");"
]
},
{
"cell_type": "markdown",
"id": "69eca992-8fc9-4507-b636-d64b5561be21",
"metadata": {},
"source": [
"# Test Cases"
]
},
{
"cell_type": "code",
"execution_count": 18,
"id": "30adc3ca-7327-4336-b294-3e8fd3380c01",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"assert_eq!(\n",
" add_two_numbers(\n",
" ListNode::fromVec(vec![2,4,3]),\n",
" ListNode::fromVec(vec![5,6,4])\n",
" ),\n",
" ListNode::fromVec(vec![7,0,8])\n",
");\n",
"assert_eq!(\n",
" add_two_numbers(\n",
" ListNode::fromVec(vec![0]),\n",
" ListNode::fromVec(vec![0])\n",
" ),\n",
" ListNode::fromVec(vec![0])\n",
");\n",
"\n",
"assert_eq!(\n",
" add_two_numbers(\n",
" ListNode::fromVec(vec![9,9]),\n",
" ListNode::fromVec(vec![9])\n",
" ),\n",
" ListNode::fromVec(vec![8,0,1])\n",
");\n",
"assert_eq!(\n",
" add_two_numbers(\n",
" ListNode::fromVec(vec![9,9,9,9,9,9,9]),\n",
" ListNode::fromVec(vec![9,9,9,9])\n",
" ),\n",
" ListNode::fromVec(vec![8,9,9,9,0,0,0,1])\n",
");"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "9377916a-51fc-4534-b53e-ec9ac5e3c81e",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "rust-Rust kernel",
"language": "rust",
"name": "rust-rust"
},
"language_info": {
"codemirror_mode": "rust",
"file_extension": ".rs",
"mimetype": "text/rust",
"name": "Rust",
"pygment_lexer": "rust",
"version": ""
}
},
"nbformat": 4,
"nbformat_minor": 5
}