镜像站点
· 本页由第三方 GitHub 只读镜像提供,
非 GitHub 官方站点
,不接受任何登录或凭据输入。
前往 github.com
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
LinXin04
LeetCode-Java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode-Java
/
HouseRobber02.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
46 lines (40 loc) · 1.21 KB
master
Breadcrumbs
LeetCode-Java
/
HouseRobber02.java
Copy path
Top
File metadata and controls
Code
Blame
46 lines (40 loc) · 1.21 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/**
* 题目:打家劫舍02.假设房子的首尾是相连的,因此,在这种情况下,要么首部的房子与尾部的房子都不打劫,要么只打劫其中的一个。
* 解题思路:
* 动态规划,我们把第一家和最后一家分别去掉,求两种情况的最大值,即为所求。
*/
import java.util.Scanner;
public class HouseRobber02 {
public static void main(String[] args) {
System.out.println("请输入数组:");
Scanner sc=new Scanner(System.in);
String s=sc.nextLine();
String []st=s.split(" ");
int []nums=new int[st.length];
for(int i=0;i<st.length;i++)
nums[i]=Integer.parseInt(st[i]);
Solution264 sl=new Solution264();
System.out.println("结果是:"+sl.rob(nums));
}
}
class Solution264
{
public int rob(int []nums)
{
if(nums==null || nums.length==0) return 0;
int n=nums.length;
if(n==1) return nums[0];
return Math.max(helper(nums,0,n-2), helper(nums,1,n-1));
}
public int helper(int []nums,int start,int end)
{
if(end-start<1) return nums[start];
int len=end-start+1;
int[] dp=new int[len+1];
dp[0]=0;
dp[1]=nums[start];
for(int i=2;i<=len;i++)
dp[i]=Math.max(dp[i-2]+nums[start+i-1], dp[i-1]);
return dp[len];
}
}
You can’t perform that action at this time.