1. 程式人生 > >leetcode第31題(triangle)

leetcode第31題(triangle)

題目:

Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below. 

For example, given the following triangle 

[
     [2],
    [3,4],
   [6,5,7],
  [4,1,8,3]
]

The minimum path sum from top to bottom is11(i.e., 2 + 3 + 5 + 1 = 11). 

Note:

 
Bonus point if you are able to do this using only O(n) extra space, where n is the total number of rows in the triangle. 

思路:可以發現第l排的第k個元素的下一排只可能是第l+1排的第k個元素或者是第l+1排的第k+1個元素。遞迴即可實現 程式碼:
importjava.util.ArrayList;

public class Solution {
    public int minimumTotal(ArrayList<ArrayList<Integer>> triangle) {
        int 
sum; sum = Result(triangle,0,0); return sum; } public int Result(ArrayList<ArrayList<Integer>> triangle,int l ,int k){ int sum = triangle.get(l).get(k); if(l<triangle.size()-1){ sum = sum+Math.min(Result(triangle,l+1,k),Result(triangle,l+1
,k+1)); } return sum; } }

相關推薦

leetcode31triangle

題目: Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.  For e

leetcode383. Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the rans

LeetCode87擾亂字串

原題如下: 給定一個字串 s1,我們可以把它遞迴地分割成兩個非空子字串,從而將其表示為二叉樹。 下圖是字串 s1 = “great” 的一種可能的表示形式。 great / gr eat / \ / g r e at / a

leetcode 105從前序與中序遍歷序列構造二叉樹106從中序與後序遍歷序列構造二叉樹python解法用時40ms

leetcode 第105題(從前序與中序遍歷序列構造二叉樹) ,第106題(從中序與後序遍歷序列構造二叉樹)python解法(用時40ms) 先從105題開始: 第105題是利用前序和中序恢復二叉樹,主要還是應用遞迴的思想。 首先看一個簡單的例子,在如下樹中: 由於前序遍歷第一個

leetcode37path-sum-ii

題目: Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum.  For example:Given the below

leetcode47binary-tree-level-order-traversal

題目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).  For example

LeetCode85最大矩形

原題如下: 給定一個僅包含 0 和 1 的二維二進位制矩陣,找出只包含 1 的最大矩形,並返回其面積。 示例: 輸入: [ [“1”,“0”,“1”,“0”,“0”], [“1”,“0”,“1”,“1”,“1”], [“1”,“1”,“1”,“1”,“1”],

leetcode50recover-binary-search-tree

題目: Two elements of a binary search tree (BST) are swapped by mistake.  Recover the tree without changing its structure.  Note:A soluti

leetcode49same-tree

題目: Given two binary trees, write a function to check if they are equal or not.  Two binary trees are considered equal if they are stru

leetcode43binary-tree-level-order-traversal

題目: Given inorder and postorder traversal of a tree, construct the binary tree.  Note:  You may assume that duplicates do not exist in

LeetCode101對稱二叉樹

原題如下: 給定一個二叉樹,檢查它是否是映象對稱的。 例如,二叉樹 [1,2,2,3,4,4,3] 是對稱的。 1 / 2 2 / \ / 3 4 4 3 但是下面這個 [1,2,2,null,3,null,3] 則不是映象對稱的: 1 / 2

2018春招-今日頭條筆試題-python

font val gpo -s nbsp blog eva 字符 今日頭條 題目描述:2018春招-今日頭條筆試題5題(後附大佬答案-c++版) 解題思路: 本題的做法最重要的應該是如何拼出‘1234567890’,對於輸入表達試獲得對應的結果利用python內置函數eva

LeetCode 31 下一個排列

(一)題目描述   實現獲取下一個排列的函式,演算法需要將給定數字序列重新排列成字典序中下一個更大的排列。   如果不存在下一個更大的排列,則將數字重新排列成最小的排列(即升序排列)。   必須原地修改,只允許使用額外常數空間。   以下是一些例子,輸入位於左側列,其相應輸出位於右側列。  1,2,3

2018綠色計算大賽預賽第二階段TeamBuildingjava

挑戰任務 “綠盟杯”決賽完美落幕之後,賽事團隊組織去一個風景優美的山區進行團建。由於人數眾多必須選擇一塊較大的場地。他們找到了一塊足夠大的地方,但是場地上卻散佈著許多石頭,為了方便活動,必須把這些石頭挪開。現在我們假設整個場地是一塊矩形的地圖,地圖座標的橫縱座標

LeetCode】第一C++

問題  給定一個整數陣列和一個目標值,找出陣列中和為目標值的兩個數。 你可以假設每個輸入只對應一種答案,且同樣的元素不能被重複利用。 示例: 給定 nums = [2, 7, 11, 15], target = 9 因為 nums[0] +

leetcode32:最長有效括號遇到一個奇葩的錯誤

問題描述: 給一個只包含 '(' 和 ')' 的字串,找出最長的有效(正確關閉)括號子串的長度。 對於 "(()",最長有效括號子串為 "()" ,它的長度是 2。 另一個例子 ")()())",最長有效括號子

合併兩個有序的連結串列LeetCode21

方法一: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None clas

有效的括號Leetcode20

方法一: class Solution: def isValid(self, s): """ :type s: str :rtype: bool """ if (len(s) % 2

LeetCode29 Divide Two Integers兩數相除

class Solution { public:     int divide(int dividend, int divisor) {         if(!divisor || (dividend == INT_MIN && divisor == -1)

LeetCode23:合併K個有序連結串列JAVA實現

題目: 我的解答: 思路很簡單,把所有的資料先讀到ArrayList中然後轉到陣列中,然後排序,然後構建新連結串列 程式碼: /** * Definition for singly-linked list. * public class ListNode {