LeetCode - Easy - 118. Pascal‘s Triangle

Topic

  • Array

Description

https://leetcode.com/problems/pascals-triangle/

Given a non-negative integer numRows, generate the first numRows of Pascal’s triangle.

In Pascal’s triangle, each number is the sum of the two numbers directly above it.

Example:

Input: 5
Output:
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]
]

Analysis

方法一:我寫的

方法二:別人寫的

Submission

import java.util.ArrayList;
import java.util.List;public class PascalsTriangle {//方法一:我寫的public List<List<Integer>> generate1(int numRows) {List<List<Integer>> result = new ArrayList<>();if (numRows <= 0)return result;for (int i = 1; i <= numRows; i++) {List<Integer> tempList = new ArrayList<>();for (int j = 0; j < i; j++) {if (j == 0 || j == i - 1) {tempList.add(1);continue;}List<Integer> lastList = result.get(i - 2);tempList.add(lastList.get(j) + lastList.get(j - 1));}result.add(tempList);}return result;}//方法二:別人寫的public List<List<Integer>> generate2(int numRows) {List<List<Integer>> allrows = new ArrayList<List<Integer>>();ArrayList<Integer> row = new ArrayList<Integer>();for (int i = 0; i < numRows; i++) {row.add(0, 1);for (int j = 1; j < row.size() - 1; j++)row.set(j, row.get(j) + row.get(j + 1));allrows.add(new ArrayList<Integer>(row));}return allrows;}
}

Test

import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;import org.junit.Test;public class PascalsTriangleTest {@Testpublic void test() {PascalsTriangle obj = new PascalsTriangle();List<List<Integer>> expected = new ArrayList<List<Integer>>();expected.add(Arrays.asList(1));expected.add(Arrays.asList(1,1));expected.add(Arrays.asList(1,2,1));expected.add(Arrays.asList(1,3,3,1));expected.add(Arrays.asList(1,4,6,4,1));assertThat(obj.generate1(5), is(expected));assertThat(obj.generate2(5), is(expected));}
}

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/445799.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/445799.shtml
英文地址,請注明出處:http://en.pswp.cn/news/445799.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

LeetCode - Easy - 119. Pascal‘s Triangle II

Topic Array Description https://leetcode.com/problems/pascals-triangle-ii/ Given an integer rowIndex, return the rowIndexth row of the Pascal’s triangle. Notice that the row index starts from 0. In Pascal’s triangle, each number is the sum of the tw…

jenv java_mac 上使用jenv 管理的多個java 版本

由于服務器是java1.7&#xff0c; mac上是1.8&#xff0c;因此mac編譯的java代碼會在服務器上報錯。因此&#xff0c;需要修改mac上java版本&#xff0c;自己折騰了很久&#xff0c;放棄&#xff0c;決定使用jenv 管理&#xff01; 結果是非常方便使用步驟&#xff1a;1、安裝 …

mysql 源碼 緩存_MySQL源碼:MYSQL存儲過程/函數的分析原理及緩存機制

前言&#xff1a;我個人認為&#xff0c;有關MYSQL存儲過程/函數在MYSQL中的實現比較粗糙&#xff0c;可擴展性不夠好&#xff0c;其實現的耦合性太高&#xff0c;所以主要講一些它的原理方面的內容&#xff0c;但有可能在某些方面理解不夠好或者有些不正確的地方&#xff0c;歡…

如何單元測試Java的private方法

問題 Java類中private方法通常只能被其所屬類的調用&#xff0c;其他類只能望而卻步&#xff0c;單元測試private方法也就一籌莫展。 嘗試解法&#xff1a; 在測試時&#xff0c;手動將private改為public&#xff0c;測試完后再將其改回。將測試方法寫進private方法的所屬類…

圖論與java_算法筆記_150:圖論之雙連通及橋的應用(Java)

1 問題描述DescriptionIn order to get from one of the F (1 < F < 5,000) grazing fields (which are numbered 1..F) to another field, Bessie and the rest of the herd are forced to cross near the Tree of Rotten Apples. The cows are now tired of often bein…

如何用JUnit單元測試List

問題 JUnit測試List時差強人意。 解法 引入依賴 hamcrest-library包含許多有用方法來測試List數據類型。 <dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version>&l…

java數據包解析_請教http請求數據包如何解析 重組

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓下面是我捕獲到的請求報文片段dst_ip:/121.52.228.134ack:trueack_num:3064957366date:POST /messagebroker/amf HTTP/1.1Host: s16.xxhzw.game.yy.comUser-Agent: Mozilla/5.0 (Windows NT 5.1; rv:13.0) Gecko/20100101 Firefox/…

webqq java_WebQQ登錄詳解

第二次登錄請求方式:POST地址:http://d.web2.qq.com/channel/login2POST正文:r%7B%22status%22%3A%22online%22%2C%22ptwebqq%22%3A%22{0}%22%2C%22passwd_sig%22%3A%22%22%2C%22clientid%22%3A%22{1}%22%2C%22  psessionid%22%3Anull%7D&clientid{2}&psessionidnull…

LeetCode - Easy - 155. Min Stack

Topic StackDesign Description https://leetcode.com/problems/min-stack/ Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. push(x) – Push element x onto stack.pop() – Removes the element on top of the st…

java judgefilecode_VScode出現無法打開“X”: 找不到文件(file:///XXXX) 的解決辦法

如標題&#xff0c;被這個問題整了好長時間了&#xff0c;調試的時候如果有語法錯誤只能顯示相應的的行數&#xff0c;沒有辦法定位到出錯的行數上。(由于用處不是很大并且沒有找到解決辦法&#xff0c;所以就一直放著沒管23333)直到最近看到一位大佬的解決辦(重寫正則表達式)法…

LeetCode - Easy - 169. Majority Element

Topic ArrayDivide and ConquerBit Manipulation Description https://leetcode.com/problems/majority-element/ Given an array of size n, find the majority element. The majority element is the element that appears more than ? n/2 ? times. You may assume t…

java 靜態方法 異常_java空指針異常與靜態方法

從一道經典面試題說起&#xff0c;public class HaHa {public static void haha(){System.out.println("haha");}public static void main(String[] args){((HaHa)null).haha();}}打印結果 haha。這段題考查兩點知識&#xff0c;java的空指針異常和靜態方法。1&#…

java中的asList_Java中的Arrays.asList()方法

Arrays.asList()返回一個List&#xff0c;但是這種情況下&#xff0c;其底層的實現是一個final數組&#xff0c;因此不能調整其尺寸如下代碼片段&#xff1a;package chapter11.t1;import java.util.*;public class AddingGroups {public static void main(String[] args) {Lis…

java控制面板作用_Java

1. JAVA 的特性和優勢(1) Java的核心優勢 跨平臺/可移植性(2) 其他特性 安全性&#xff1b;面對對象&#xff1b;簡單性&#xff1b;高性能&#xff1b;分布式&#xff1b;多線程&#xff1b;健壯性&#xff1b;① 強大的生態系統(3) Java與C的關系 Java是C的簡化版(C—)2. JAV…

java es 數據批量導入_ElasticSearch—Java批量導入導出

網上找了很多&#xff0c;我的es是2.3.5版本&#xff0c;網上的客戶端最少都是5.x版本&#xff0c;所以沒有能用的。自己整合了一下 2.3.5版本的。pom文件&#xff1a;org.elasticsearchelasticsearch2.3.5com.alibabafastjson1.1.35org.apache.commonscommons-io1.3.2org.apac…

java原始模型模式_java設計模式--原始模型模式

簡介原始模型模式屬于對象的創建模式。通過一個原型對象來指明要創建對象的類型&#xff0c;然后用復制原型對象的方法來創建出更多同類型的對象。Java所有的類都是從java.lang.Object類繼承來的&#xff0c;Object類提供clone()方法對對象進行復制。一般調用clone()方法需要滿…

Windows的命令行窗口運行Python時,如何清屏?

問題 如標題 解法 import os os.system("cls")參考 python實現清屏

手寫文字識別java_java 手寫文字圖片識別提取 百度API

package org.fh.util;import org.json.JSONObject;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.URL;import java.util.List;import java.util.Map;/*** 說明&#xff1a;獲取文字識別token類* from&am…

LeetCode - Easy - 191. Number of 1 Bits

Topic Bit Manipulation Description https://leetcode.com/problems/number-of-1-bits/ Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight). Note: Note that in some languages such …

java并行計算同步返回_Java大文本并行計算實現過程解析

Java大文本并行計算實現過程解析簡單提高文本讀取效率&#xff0c;使用BufferedReader是個不錯的選擇。速度最快的方法是MappedByteBuffer&#xff0c;但是&#xff0c;相比BufferedReader而言&#xff0c;效果不是非常明顯。也就是說&#xff0c;后者雖然快&#xff0c;但也快…