Created
November 29, 2018 01:03
-
-
Save qiaoxu123/1d0e54ac50a122ff0e304d8ad6d9e7ac to your computer and use it in GitHub Desktop.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//比较巧妙的思路 | |
class Solution { | |
public: | |
int rob(vector<int>& nums) { | |
int a = 0,b = 0; | |
for(int i = 0;i < nums.size();i++){ | |
if(i % 2 == 0) | |
a = (a + nums[i]) > b ? (a + nums[i]) : b; | |
else | |
b = (a) > (b + nums[i]) ? a : (b + nums[i]); | |
} | |
return a > b ? a : b; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment