leetcode 377. 組合總和 Ⅳ(dp)

給你一個由 不同 整數組成的數組 nums ,和一個目標整數 target 。請你從 nums 中找出并返回總和為 target 的元素組合的個數。

題目數據保證答案符合 32 位整數范圍。

示例 1:

輸入:nums = [1,2,3], target = 4
輸出:7
解釋:
所有可能的組合為:
(1, 1, 1, 1)
(1, 1, 2)
(1, 2, 1)
(1, 3)
(2, 1, 1)
(2, 2)
(3, 1)
請注意,順序不同的序列被視作不同的組合。
示例 2:

輸入:nums = [9], target = 3
輸出:0

提示:

1 <= nums.length <= 200
1 <= nums[i] <= 1000
nums 中的所有元素 互不相同
1 <= target <= 1000

解題思路

數組含義

枚舉1至target的值,使用一個一維數組記錄可以組成當前元素的組合個數

狀態轉移方程就是

		for _, num := range nums {//枚舉nums數組if i-num>0&&dp[i-num]>0{dp[i]+=dp[i-num]}}

dp[i-num]大于0,說明i-num可以由nums數組中的元素組成,并且組合個數為dp[i-num],因此i同樣也能由nums數組元素組合而來,并且組合個數為dp[i-num]

初始化

	for _, num := range nums {if num>target {continue  }dp[num]=1}

枚舉nums,dp[num]=1,即nums數組中元素可以直接組成一個target值而且組合個數為1

代碼

func combinationSum4(nums []int, target int) int {dp := make([]int, target+1)for _, num := range nums {if num>target {continue  }dp[num]=1}for i := 1; i <=target; i++ {for _, num := range nums {if i-num>0&&dp[i-num]>0{dp[i]+=dp[i-num]}}}return dp[target]}

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

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

相關文章

1.4- 定時任務總結之九句箴言

1.4定時任務之九句箴言九句箴言---- 不會九句箴言別做運維1.定時任務規則之前加注釋2.使用腳本代替命令行制定定時任務3.定時任務中date命令%的特殊含義定時任務中,%表示回車 -----可以使用\轉義4.運行腳本一定要用/bin/sh或sh腳本不必須有x權限5.定時任務中-命令或腳本的輸出…

ubuntu 18.04 vi里面方向鍵變成abcd 處理辦法

sudo apt-get remove vim-common sudo apt-get install vim 轉載于:https://www.cnblogs.com/testing-BH/p/11506400.html

知識力量_網絡分析的力量

知識力量The most common way to store data is in what we call relational form. Most systems get analyzed as collections of independent data points. It looks something like this:存儲數據的最常見方式是我們所謂的關系形式。 大多數系統作為獨立數據點的集合進行分析…

python里的apply,applymap和map的區別

apply,applymap和map的應用總結:apply 用在dataframe上&#xff0c;用于對row或者column進行計算&#xff1b;applymap 用于dataframe上&#xff0c;是元素級別的操作&#xff1b;map &#xff08;其實是python自帶的&#xff09;用于series上&#xff0c;是元素級別的操作。如…

驗證曲線和學習曲線_如何擊敗技術學習曲線的怪物

驗證曲線和學習曲線Doing what I do for a living, which these days mostly involves creating technology books and courseware, I’m constantly learning new technologies. In a way, my new tech adventures are not much different than the ones most IT pros face, e…

234

234 轉載于:https://www.cnblogs.com/Forever77/p/11509588.html

SCCM PXE客戶端無法加載DP(分發點)映像

上一篇文章我們講到了一個比較典型的PXE客戶端無法找到操作系統映像的故障&#xff0c;今天再和大家一起分享一個關于 PXE客戶端無法加載分發點映像的問題。具體的報錯截圖如下&#xff1a;從報錯中我們可以看到&#xff0c;PXE客戶端已經成功的找到了SCCM服務器&#xff0c;并…

Docker 入門(2)技術實現和核心組成

1. Docker 的技術實現 Docker 的實現&#xff0c;主要歸結于三大技術&#xff1a; 命名空間 ( Namespaces )控制組 ( Control Groups )聯合文件系統 ( Union File System ) 1.1 Namespace 命名空間可以有效地幫助Docker分離進程樹、網絡接口、掛載點以及進程間通信等資源。L…

marlin 三角洲_帶火花的三角洲湖:什么和為什么?

marlin 三角洲Let me start by introducing two problems that I have dealt time and again with my experience with Apache Spark:首先&#xff0c;我介紹一下我在Apache Spark上的經歷反復解決的兩個問題&#xff1a; Data “overwrite” on the same path causing data l…

環境變量的作用

1. PATH環境變量。作用是指定命令搜索路徑&#xff0c;在shell下面執行命令時&#xff0c;它會到PATH變量所指定的路徑中查找看是否能找到相應的命令程序。我們需要把 jdk安裝目錄下的bin目錄增加到現有的PATH變量中&#xff0c;bin目錄中包含經常要用到的可執行文件如javac/ja…

WeWork通過向225,000個社區征稅來拼命地從Meetup.com榨取現金

Update: A few hours after I published this article, Meetup quietly added a note to the top of their announcement. They have not tweeted or done anything else to publicize this note, but some people noticed it and shared it with me.更新&#xff1a;在我發布本…

eda分析_EDA理論指南

eda分析Most data analysis problems start with understanding the data. It is the most crucial and complicated step. This step also affects the further decisions that we make in a predictive modeling problem, one of which is what algorithm we are going to ch…

leetcode 897. 遞增順序搜索樹(中序遍歷)

給你一棵二叉搜索樹&#xff0c;請你 按中序遍歷 將其重新排列為一棵遞增順序搜索樹&#xff0c;使樹中最左邊的節點成為樹的根節點&#xff0c;并且每個節點沒有左子節點&#xff0c;只有一個右子節點。 示例 1&#xff1a; 輸入&#xff1a;root [5,3,6,2,4,null,8,1,null…

【一針見血】 JavaScript this

JavaScript this 指向一站式解決轉載于:https://www.cnblogs.com/xueyejinghong/p/8403987.html

基于ssm框架和freemarker的商品銷售系統

項目說明 1、項目文件結構 2、項目主要接口及其實現 &#xff08;1&#xff09;Index&#xff1a; 首頁頁面&#xff1a;展示商品功能&#xff0c;可登錄或查看商品詳細信息 &#xff08;2&#xff09;登錄&#xff1a;/ApiLogin 3、dao層 數據持久化層&#xff0c;把商品和用戶…

c++飛揚的小鳥游戲_通過建立一個飛揚的鳥游戲來學習從頭開始

c飛揚的小鳥游戲Learn how to use Scratch 3.0 by building a flappy bird game in this course developed by Warfame. Scratch is a free programming language and online community where you can create your own interactive stories, games, and animations. Scratch is…

345

345 轉載于:https://www.cnblogs.com/Forever77/p/11512701.html

簡·雅各布斯指數第二部分:測試

In Part I, I took you through the data gathering and compilation required to rank Census tracts by the four features identified by Jane Jacobs as the foundation of a great neighborhood:在第一部分中 &#xff0c;我帶您完成了根據簡雅各布斯(Jacobs Jacobs)所確定…

Docker 入門(3)Docke的安裝和基本配置

1. Docker Linux下的安裝 1.1 Docker Engine 的版本 社區版 ( CE, Community Edition ) 社區版 ( Docker Engine CE ) 主要提供了 Docker 中的容器管理等基礎功能&#xff0c;主要針對開發者和小型團隊進行開發和試驗企業版 ( EE, Enterprise Edition ) 企業版 ( Docker Engi…

python:單元測試框架pytest的一個簡單例子

之前一般做自動化測試用的是unitest框架&#xff0c;發現pytest同樣不錯&#xff0c;寫一個例子感受一下 test_sample.py import cx_Oracle import config from send_message import send_message from insert_cainiao_oracle import insert_cainiao_oracledef test_cainiao_mo…