WeekTwo

Share LeetCode

题目:

给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
示例:
输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/add-two-numbers

Java代码及详细注释

LideNode类
package com.zhbit.leetcode_testsum.week2;
//自定义链表类ListNode
public class ListNode {
    int val; //当前节点值
    ListNode next; //向下一个节点的指针
    ListNode(int x){
        val = x;
    }
}
实现方法类
package com.zhbit.leetcode_testsum.week2;

import org.springframework.stereotype.Service;

@Service

public class ListNodeTwoSum {
    /**
     * 链表相加
     * @param l1 已知链表l1
     * @param l2 已知链表l2
     * @return 相加后的链表result
     */
    public ListNode listNodeTwoSum(ListNode l1,ListNode l2){
        //相加后的链表result
        ListNode result = new ListNode(0);
        //初始化,指针p为l1链表的head,q为l2链表的head,cur为result链表的head
        ListNode p = l1 , q = l2 , cur = result;
        //定义变量carry表示要进位的值
        int carry = 0;
        //当表头节点p或q有值得时候循环
        while(p != null || q != null) {
            //若p不为空,将p.val(当前节点的值)赋给整形数a,否则将0赋给a
           int a = (p != null) ? p.val : 0;
            //若q不为空,将q.val(当前节点的值)赋给整形数b,否则将0赋给b
           int b = (q != null) ? q.val : 0;
           //将a+b+carry 的和对10求余的值赋给整型变量sum(其实就是要赋予新链表的当前节点的值)
           int sum = (a + b + carry)%10;
           //进位的值显然等于两数相加加carry的十位的数值
            carry = (a + b + carry)/10;
            //将两数相加的值sum赋予cur的下一个节点
            cur.next = new ListNode(sum);
            //将cur.next的值再赋给cur,避免cur.next.next方便使用循环
            cur = cur.next;
            //再次判断p,q是否为空,将非空对象的下一个节点赋予该对象,空对象则保持不变进行下次循环。
            if (p != null){
                p = p.next;
            }
            if (q != null){
                q = q.next;
            }
        }
        //判断,如果最后加完还有进位,那么将进位存入链表的下一个节点
        if (carry == 1){
            cur.next = new ListNode(carry);
        }
        //返回
        return result.next;
    }
}
Test测试类
package com.zhbit.leetcode_testsum.week2;

import lombok.extern.slf4j.Slf4j;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class ListNodeTwoSumTest {
    @Autowired
    private ListNodeTwoSum listNodeTwoSum;
//测试链表相加
    @Test
    public void listNodeTwoSum() {
        ListNode l1 = new ListNode(2);
        l1.next = new ListNode(4);
        l1.next.next = new ListNode(3);
        ListNode l2 = new ListNode(5);
        l2.next = new ListNode(6);
        l2.next.next = new ListNode(4);
        ListNode result = listNodeTwoSum.listNodeTwoSum(l1,l2);
        Assert.assertTrue(result != null);
        ListNode p = result;
        int x = 0;
        while (p != null){
            x = p.val;
//            log.info("{}",p.val);
            System.out.println(""+x);
            p = p.next;
        }
        System.out.println("成功");
    }
}
测试

Share Skill

for循环,while循环,do while循环

1.for循环
2.while循环
while(表达式){
语句;
}
当表达式为真,则执行下面的语句;语句执行完之后再判断表达式是否为真,如果为真,再次执行下面的语句;然后再判断表达式是否为真……就这样一直循环下去,直到表达式为假,跳出循环。这个就是 while 的执行顺序。
eg.

代码请参考上文测试类和方法类代码

for循环和while循环可以相互替代,所有for循环都可以转换成while循环,while循环也全都可以转换成for循环。
3.do......while循环

do{
     执行语句;
}while(判断语句);

do......while 循环先执行语句后进行判断
三种循环大部分情况都可以相互转换,当只关注什么时候循环的时候而不关注循环次数一般使用while循环,while和for循环可以不循环而do......while循环至少执行一次执行语句。

Share Article

原文来自(https://www.jianshu.com/p/81cca1027a1a)

这篇文章介绍了人体器官胃
知识点

1.胃,也叫胃囊,最外层的胃壁是由好几层肌肉组织构成,具有很强的伸缩性。
2.胃囊扩张后能容纳食物的量称为“胃容量”
3.胃囊大小可能会随着你的进食量改变,但是胃容量却不会发生变化。
4.人体食欲的大小并不能单纯看胃容量,吃多少,与我们胃肠道、饱食和饥饿中枢、大脑皮质之间的神经调节都有关系,而这三者又会因为体质、环境、遗传等多方面原因互相调节、制约,保持相对的平衡。
5.长期吃很多食物,胃壁增厚,蠕动加快,饭量自然就大。
6.若感觉自己吃的越来越少,不是胃变小了,而是你的大脑发出指令来压抑住了你的饥饿感,应对这种饥荒模式,降低食欲。一部分严重的人甚至会出现厌食症。
7.吃太多——胃肠功能障碍
8.吃太快——吸收困难
9.长期饥饿——黏膜腺体萎缩

Share English Literature

原文来自(http://pages.cs.wisc.edu/~remzi/OSTEP/preface.pdf)

To Students
If you are a student reading this book, thank you! It is an honor for us to
provide some material to help you in your pursuit of knowledge about operating systems. We both think back fondly towards some textbooks of our undergraduate days (e.g., Hennessy and Patterson [HP90], the classic book on computer architecture) and hope this book will become one of those positive memories for you.
You may have noticed this book is free and available online2. There is one major reason for this: textbooks are generally too expensive. This book, we hope, is the first of a new wave of free materials to help those in pursuit of their education, regardless of which part of the world they come from or how much they are willing to spend for a book. Failing that, it is one free book, which is better than none.
We also hope, where possible, to point you to the original sources of much of the material in the book: the great papers and persons who have shaped the field of operating systems over the years. Ideas are not pulled out of the air; they come from smart and hard-working people (including numerous Turing-award winners3), and thus we should strive to celebrate those ideas and people where possible. In doing so, we hopefully can better understand the revolutions that have taken place, instead of writing texts as if those thoughts have always been present [K62]. Further, perhaps such references will encourage you to dig deeper on your own; reading the famous papers of our field is certainly one of the best ways to learn.
感悟:这篇文章,是这本书向学生讲述这本书的概述,首先说明了这本书是免费的,并且说明了这本书记载了许多先辈们挖掘的知识和思想,并对这些先辈们表示了高度的赞扬,并且鼓舞学生真正去理解书本中的知识和思想,并努力挖掘新的知识,思想。通过阅读这篇文章我发现了我的英语阅读能力还是很需要提高的。

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容