leetcode/2/rust: mutate the res to improve memory usage
This commit is contained in:
parent
5a8e8e6f8f
commit
c421822ded
1 changed files with 34 additions and 57 deletions
|
@ -37,41 +37,47 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 23,
|
"execution_count": 22,
|
||||||
"id": "167e31a3-7844-4cdf-a563-241a40b07212",
|
"id": "167e31a3-7844-4cdf-a563-241a40b07212",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"pub fn internal_add_two_numbers(carry: i32, l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> {\n",
|
|
||||||
" match (carry, l1, l2) {\n",
|
|
||||||
" (0, None, None) => None,\n",
|
|
||||||
" (_, l1, l2) => {\n",
|
|
||||||
" let mut new_val = carry;\n",
|
|
||||||
" if let Some(ref n1) = l1 {\n",
|
|
||||||
" new_val += n1.val;\n",
|
|
||||||
" }\n",
|
|
||||||
" if let Some(ref n2) = l2 {\n",
|
|
||||||
" new_val += n2.val;\n",
|
|
||||||
" }\n",
|
|
||||||
" let mut nn = ListNode::new(new_val % 10);\n",
|
|
||||||
" let carry = if new_val > 9 { 1 } else { 0 };\n",
|
|
||||||
" nn.next = internal_add_two_numbers(\n",
|
|
||||||
" carry,\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",
|
"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",
|
" 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",
|
||||||
|
" }\n",
|
||||||
|
" carry = val / 10;\n",
|
||||||
|
" *p = Some(Box::new(ListNode::new(val % 10)));\n",
|
||||||
|
" p = &mut p.as_mut().unwrap().next;\n",
|
||||||
|
" }\n",
|
||||||
|
" \n",
|
||||||
|
" res\n",
|
||||||
"}"
|
"}"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "0f4e6fc2-0fd1-4789-b14f-4c49f5d7a96d",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "f9dab3ca-0d78-4050-b94b-ad0d19348b09",
|
"id": "f9dab3ca-0d78-4050-b94b-ad0d19348b09",
|
||||||
|
@ -82,41 +88,12 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 24,
|
"execution_count": 4,
|
||||||
"id": "33f9c78d-98f5-4d95-9ed0-e0c1961dc7b6",
|
"id": "33f9c78d-98f5-4d95-9ed0-e0c1961dc7b6",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
},
|
},
|
||||||
"outputs": [
|
"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_24: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_24: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": [
|
"source": [
|
||||||
"impl ListNode {\n",
|
"impl ListNode {\n",
|
||||||
" #[inline]\n",
|
" #[inline]\n",
|
||||||
|
@ -155,7 +132,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 18,
|
"execution_count": 23,
|
||||||
"id": "30adc3ca-7327-4336-b294-3e8fd3380c01",
|
"id": "30adc3ca-7327-4336-b294-3e8fd3380c01",
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"tags": []
|
"tags": []
|
||||||
|
|
Loading…
Reference in a new issue