braingym/leetcode/2/rust.ipynb

200 lines
4.8 KiB
Text
Raw Permalink Normal View History

2023-05-15 11:26:58 +03:00
{
"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": 22,
2023-05-15 11:26:58 +03:00
"id": "167e31a3-7844-4cdf-a563-241a40b07212",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"pub fn add_two_numbers(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n",
" let mut carry = 0;\n",
" let mut res = None;\n",
" let mut p = &mut res;\n",
" let mut p1 = &l1;\n",
" let mut p2 = &l2;\n",
" \n",
" while p1.is_some() || p2.is_some() || carry > 0 {\n",
" let mut val = carry;\n",
" if let Some(ref n1) = p1 {\n",
" val += n1.val;\n",
" p1 = &n1.next;\n",
" }\n",
" if let Some(ref n2) = p2 {\n",
" val += n2.val;\n",
" p2 = &n2.next;\n",
2023-05-15 11:26:58 +03:00
" }\n",
" carry = val / 10;\n",
" *p = Some(Box::new(ListNode::new(val % 10)));\n",
" p = &mut p.as_mut().unwrap().next;\n",
2023-05-15 11:26:58 +03:00
" }\n",
" \n",
" res\n",
2023-05-15 11:26:58 +03:00
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0f4e6fc2-0fd1-4789-b14f-4c49f5d7a96d",
"metadata": {},
"outputs": [],
"source": []
},
2023-05-15 11:26:58 +03:00
{
"cell_type": "markdown",
"id": "f9dab3ca-0d78-4050-b94b-ad0d19348b09",
"metadata": {},
"source": [
"# Test Utils"
]
},
{
"cell_type": "code",
"execution_count": 4,
2023-05-15 11:26:58 +03:00
"id": "33f9c78d-98f5-4d95-9ed0-e0c1961dc7b6",
"metadata": {
"tags": []
},
"outputs": [],
2023-05-15 11:26:58 +03:00
"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": 23,
2023-05-15 11:26:58 +03:00
"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
}