WeekTwo

Share LeetCode

題目:

給出兩個(gè) 非空 的鏈表用來(lái)表示兩個(gè)非負(fù)的整數(shù)。其中,它們各自的位數(shù)是按照 逆序 的方式存儲(chǔ)的,并且它們的每個(gè)節(jié)點(diǎn)只能存儲(chǔ) 一位 數(shù)字。如果,我們將這兩個(gè)數(shù)相加起來(lái),則會(huì)返回一個(gè)新的鏈表來(lái)表示它們的和。您可以假設(shè)除了數(shù)字 0 之外,這兩個(gè)數(shù)都不會(huì)以 0 開頭。
示例:
輸入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
輸出:7 -> 0 -> 8
原因:342 + 465 = 807
來(lái)源:力扣(LeetCode)
鏈接:https://leetcode-cn.com/problems/add-two-numbers

Java代碼及詳細(xì)注釋

LideNode類
package com.zhbit.leetcode_testsum.week2;
//自定義鏈表類ListNode
public class ListNode {
    int val; //當(dāng)前節(jié)點(diǎn)值
    ListNode next; //向下一個(gè)節(jié)點(diǎn)的指針
    ListNode(int x){
        val = x;
    }
}
實(shí)現(xiàn)方法類
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表示要進(jìn)位的值
        int carry = 0;
        //當(dāng)表頭節(jié)點(diǎn)p或q有值得時(shí)候循環(huán)
        while(p != null || q != null) {
            //若p不為空,將p.val(當(dāng)前節(jié)點(diǎn)的值)賦給整形數(shù)a,否則將0賦給a
           int a = (p != null) ? p.val : 0;
            //若q不為空,將q.val(當(dāng)前節(jié)點(diǎn)的值)賦給整形數(shù)b,否則將0賦給b
           int b = (q != null) ? q.val : 0;
           //將a+b+carry 的和對(duì)10求余的值賦給整型變量sum(其實(shí)就是要賦予新鏈表的當(dāng)前節(jié)點(diǎn)的值)
           int sum = (a + b + carry)%10;
           //進(jìn)位的值顯然等于兩數(shù)相加加carry的十位的數(shù)值
            carry = (a + b + carry)/10;
            //將兩數(shù)相加的值sum賦予cur的下一個(gè)節(jié)點(diǎn)
            cur.next = new ListNode(sum);
            //將cur.next的值再賦給cur,避免cur.next.next方便使用循環(huán)
            cur = cur.next;
            //再次判斷p,q是否為空,將非空對(duì)象的下一個(gè)節(jié)點(diǎn)賦予該對(duì)象,空對(duì)象則保持不變進(jìn)行下次循環(huán)。
            if (p != null){
                p = p.next;
            }
            if (q != null){
                q = q.next;
            }
        }
        //判斷,如果最后加完還有進(jìn)位,那么將進(jìn)位存入鏈表的下一個(gè)節(jié)點(diǎn)
        if (carry == 1){
            cur.next = new ListNode(carry);
        }
        //返回
        return result.next;
    }
}
Test測(cè)試類
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;
//測(cè)試鏈表相加
    @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("成功");
    }
}
測(cè)試

Share Skill

for循環(huán),while循環(huán),do while循環(huán)

1.for循環(huán)
2.while循環(huán)
while(表達(dá)式){
語(yǔ)句;
}
當(dāng)表達(dá)式為真,則執(zhí)行下面的語(yǔ)句;語(yǔ)句執(zhí)行完之后再判斷表達(dá)式是否為真,如果為真,再次執(zhí)行下面的語(yǔ)句;然后再判斷表達(dá)式是否為真……就這樣一直循環(huán)下去,直到表達(dá)式為假,跳出循環(huán)。這個(gè)就是 while 的執(zhí)行順序。
eg.

代碼請(qǐng)參考上文測(cè)試類和方法類代碼

for循環(huán)和while循環(huán)可以相互替代,所有for循環(huán)都可以轉(zhuǎn)換成while循環(huán),while循環(huán)也全都可以轉(zhuǎn)換成for循環(huán)。
3.do......while循環(huán)

do{
     執(zhí)行語(yǔ)句;
}while(判斷語(yǔ)句);

do......while 循環(huán)先執(zhí)行語(yǔ)句后進(jìn)行判斷
三種循環(huán)大部分情況都可以相互轉(zhuǎn)換,當(dāng)只關(guān)注什么時(shí)候循環(huán)的時(shí)候而不關(guān)注循環(huán)次數(shù)一般使用while循環(huán),while和for循環(huán)可以不循環(huán)而do......while循環(huán)至少執(zhí)行一次執(zhí)行語(yǔ)句。

Share Article

原文來(lái)自(http://www.itdecent.cn/p/81cca1027a1a)

這篇文章介紹了人體器官胃
知識(shí)點(diǎn)

1.胃,也叫胃囊,最外層的胃壁是由好幾層肌肉組織構(gòu)成,具有很強(qiáng)的伸縮性。
2.胃囊擴(kuò)張后能容納食物的量稱為“胃容量”
3.胃囊大小可能會(huì)隨著你的進(jìn)食量改變,但是胃容量卻不會(huì)發(fā)生變化。
4.人體食欲的大小并不能單純看胃容量,吃多少,與我們胃腸道、飽食和饑餓中樞、大腦皮質(zhì)之間的神經(jīng)調(diào)節(jié)都有關(guān)系,而這三者又會(huì)因?yàn)轶w質(zhì)、環(huán)境、遺傳等多方面原因互相調(diào)節(jié)、制約,保持相對(duì)的平衡。
5.長(zhǎng)期吃很多食物,胃壁增厚,蠕動(dòng)加快,飯量自然就大。
6.若感覺自己吃的越來(lái)越少,不是胃變小了,而是你的大腦發(fā)出指令來(lái)壓抑住了你的饑餓感,應(yīng)對(duì)這種饑荒模式,降低食欲。一部分嚴(yán)重的人甚至?xí)霈F(xiàn)厭食癥。
7.吃太多——胃腸功能障礙
8.吃太快——吸收困難
9.長(zhǎng)期饑餓——黏膜腺體萎縮

Share English Literature

原文來(lái)自(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.
感悟:這篇文章,是這本書向?qū)W生講述這本書的概述,首先說(shuō)明了這本書是免費(fèi)的,并且說(shuō)明了這本書記載了許多先輩們挖掘的知識(shí)和思想,并對(duì)這些先輩們表示了高度的贊揚(yáng),并且鼓舞學(xué)生真正去理解書本中的知識(shí)和思想,并努力挖掘新的知識(shí),思想。通過閱讀這篇文章我發(fā)現(xiàn)了我的英語(yǔ)閱讀能力還是很需要提高的。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容