日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)

發布時間:2025/4/5 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.題目

一道典型的Dynamic Programming的題目。

You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security system connected and?it will automatically contact the police if two adjacent houses were broken into on the same night.

Given a list of non-negative integers representing the amount of money of each house, determine the maximum amount of money you can rob tonight?without alerting the police.

2.代碼

public class Solution {public int rob(int[] nums) {int[][] num=new int[nums.length+1][2];for(int i=1;i<=nums.length;i++){num[i][0]=Math.max(num[i-1][0],num[i-1][1]);num[i][1]=nums[i-1]+num[i-1][0];}return Math.max(num[nums.length][0],num[nums.length][1]);} }





1.題目

A message containing letters from?A-Z?is being encoded to numbers using the following mapping:

'A' -> 1 'B' -> 2 ... 'Z' -> 26

Given an encoded message containing digits, determine the total number of ways to decode it.

For example,
Given encoded message?"12", it could be decoded as?"AB"?(1 2) or?"L"?(12).

The number of ways decoding?"12"?is 2.

2.代碼

public class Solution {public int numDecodings(String s) {int n = s.length();if (n == 0) return 0;int[] memo = new int[n+1];memo[n] = 1;memo[n-1] = s.charAt(n-1) != '0' ? 1 : 0;for (int i = n - 2; i >= 0; i--)if (s.charAt(i) == '0') continue;else memo[i] = (Integer.parseInt(s.substring(i,i+2))<=26) ? memo[i+1]+memo[i+2] : memo[i+1];return memo[0];} }



1.題目

Given a string?s?and a dictionary of words?dict, determine if?s?can be segmented into a space-separated sequence of one or more dictionary words.

For example, given
s?=?"leetcode",
dict?=?["leet", "code"].

Return true because?"leetcode"?can be segmented as?"leet code".

2.代碼

public class Solution {public boolean wordBreak(String s, Set<String> dict) {boolean[] f = new boolean[s.length() + 1];Arrays.fill(f, false);f[0] = true;for(int i=1; i <= s.length(); i++){for(int j=0; j < i; j++){if(f[j] && dict.contains(s.substring(j, i))){f[i] = true;break;}}}return f[s.length()];} }


1.題目

A robot is located at the top-left corner of a?m?x?n?grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?


Above is a 3 x 7 grid. How many possible unique paths are there?

2.代碼

public class Solution {public int uniquePaths(int m, int n) {Integer[][] map = new Integer[m][n];for(int i = 0; i<m;i++){map[i][0] = 1;}for(int j= 0;j<n;j++){map[0][j]=1;}for(int i = 1;i<m;i++){for(int j = 1;j<n;j++){map[i][j] = map[i-1][j]+map[i][j-1];}}return map[m-1][n-1];} }



/********************************

* 本文來自博客 ?“李博Garvin“

* 轉載請標明出處:http://blog.csdn.net/buptgshengod

******************************************/





總結

以上是生活随笔為你收集整理的【LeetCode从零单排】No198.House Robber No91.Decode Ways139 word break(动态规划典型应用)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。