当前位置:   article > 正文

Golang | Leetcode Golang题解之第198题打家劫舍

Golang | Leetcode Golang题解之第198题打家劫舍

题目:

题解:

  1. func rob(nums []int) int {
  2. if len(nums) == 0 {
  3. return 0
  4. }
  5. if len(nums) == 1 {
  6. return nums[0]
  7. }
  8. first := nums[0]
  9. second := max(nums[0], nums[1])
  10. for i := 2; i < len(nums); i++ {
  11. first, second = second, max(first + nums[i], second)
  12. }
  13. return second
  14. }
  15. func max(x, y int) int {
  16. if x > y {
  17. return x
  18. }
  19. return y
  20. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/850362
推荐阅读
相关标签
  

闽ICP备14008679号