前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >rust leetcode Add Two Numbers #2

rust leetcode Add Two Numbers #2

作者头像
用户2436820
发布2019-11-20 21:32:23
5280
发布2019-11-20 21:32:23
举报
文章被收录于专栏:奔跑的蛙牛技术博客

每日小刷

Add Two Numbers

Runtime

Memory

4ms

2.4m

代码语言:javascript
复制
// Definition for singly-linked list.
// #[derive(PartialEq, Eq, Clone, Debug)]
// pub struct ListNode {
//   pub val: i32,
//   pub next: Option<Box<ListNode>>
// }
// 
// impl ListNode {
//   #[inline]
//   fn new(val: i32) -> Self {
//     ListNode {
//       next: None,
//       val
//     }
//   }
// }
impl Solution {
    pub fn add_two_numbers_1(
        l1: Option<Box<ListNode>>,
        l2: Option<Box<ListNode>>,
    ) -> Option<Box<ListNode>> {
        let mut dump_head = ListNode::new(0);
        let mut current = &mut dump_head;
        let mut carry = 0;
        let mut p = l1.as_ref();
        let mut q = l2.as_ref();
        while p.is_some() || q.is_some() {
            let sum = match (&p, &q) {
                (Some(l1), Some(l2)) => l1.val + l2.val + carry,
                (Some(l1), None) => l1.val + carry,
                (None, Some(l2)) => l2.val + carry,
                (None, None) => 0 + carry,
            };
            carry = sum / 10;
            current.next = Some(Box::new(ListNode::new(sum % 10)));
            current = current.next.as_mut().unwrap();
            
            if p.is_some() && p.unwrap().next.is_some() {
                p = p.unwrap().next.as_ref()
            }else{
                p = None;
            }
            if q.is_some() && q.unwrap().next.is_some() {
                println!("cccc");
                q = q.unwrap().next.as_ref();
            }else{
                q = None;
            }
        }
        if (carry > 0) {
            current.next = Some(Box::new(ListNode::new(carry)));
        }
        dump_head.next
    }

// 闭包解决
    pub fn add_two_numbers(
        l1: Option<Box<ListNode>>,
        l2: Option<Box<ListNode>>,
    )->Option<Box<ListNode>>{
        // 声明变量
        let mut head = ListNode::new(0);
        let mut cur = &mut head.next;
        let (mut x,mut y) = (l1,l2);
        let mut carry = 0;
        let node_val = |node:&Option<Box<ListNode>>| node.as_ref().map_or(0, |x| x.val);
        let node_next = |node:Option<Box<ListNode>>| node.map_or(None, |x| x.next);
        // 创建闭包
        while x.is_some() || y.is_some() || carry!=0 {
            let sum = node_val(&x)+node_val(&y)+carry;
            *cur = Some(Box::new(ListNode::new(sum % 10)));
            cur = &mut cur.as_mut().unwrap().next;
            carry = sum  / 10;
            x = node_next(x);
            y = node_next(y);
        }
        // while 循环比较
        
        head.next
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 每日小刷
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档