(Easy) House Robber LeetCode

class Solution {
    public int rob(int[] nums) {

        if(nums.length<=0 || nums ==null){
            return 0;
        }

        if( nums.length ==1){
            return nums[0];
        }

        if(nums.length ==2){

            return Max(nums[0],nums[1]);
        }

        int[] dp = new int[nums.length];

        dp[0] = nums[0];
        dp[1] = Max(nums[0],nums[1]);

        for(int i = 2; i<nums.length; i++){

            dp[i] = Max((dp[i-2]+nums[i]),dp[i-1]);
        }

        return dp[nums.length-1];
    }

    public int Max(int a, int b){

        return a>b? a: b;
    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11287866.html

时间: 2024-11-13 08:07:14

(Easy) House Robber LeetCode的相关文章

House Robber——LeetCode

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 autom

House Robber -- leetcode

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 autom

(Easy) Set Mismatch -LeetCode

Description: The set S originally contains numbers from 1 to n. But unfortunately, due to the data error, one of the numbers in the set got duplicated to another number in the set, which results in repetition of one number and loss of another number.

(Easy) Valid Palindrome -LeetCode

Description: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. Note: For the purpose of this problem, we define empty string as valid palindrome. Example 1: Input: "A man, a plan, a canal: P

(Easy) Valid Boomerang - LeetCode

Description: A boomerang is a set of 3 points that are all distinct and not in a straight line. Given a list of three points in the plane, return whether these points are a boomerang. Example 1: Input: [[1,1],[2,3],[3,2]] Output: true Example 2: Inpu

(Easy) Arranging Coins - LeetCode

Description: You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins. Given n, find the total number of full staircase rows that can be formed. n is a non-negative integer and fits within

(Easy) Ransom Note - LeetCode

Description: Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function that will return true if the ransom note can be constructed from the magazines ; otherwise, it will return false. Each l

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

No.006:ZigZag Conversion

题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R Write the code that will take a string a