[日常刷题]leetcode第十八天

版权声明:希望各位多提意见多多互相交流哦~ https://blog.csdn.net/wait_for_taht_day5/article/details/82875425

198. House Robber

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.

Example 1:

Input: [1,2,3,1]
Output: 4
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
             Total amount you can rob = 1 + 3 = 4.

Example 2:

Input: [2,7,9,3,1]
Output: 12
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
             Total amount you can rob = 2 + 9 + 1 = 12.

Solution in C++:

关键点:

  • 递归问题思维

思路:

  • 先说下我自己的思路哈,是错的。开始看了例子,感觉好简单,不就是隔一个偷一个嘛。后来提交就傻眼了,发现不对。就又重新想,好像可以不选隔一个的,可以选隔几个的,那么是为什么呢,肯定是这几个加起来都没有那一个大或者大的这个和他的下下个的和。所以就进步,多探索了几步,诶,很棒例子的都过了,开始不过的例子也过了,但是发现提交还是错了,我的脑细胞不够用了。看了下面这个解析真的是很赞!!!
  • 优秀的思路:From good to great.How to approach most of DP problems.
int rob(vector<int>& nums) {
        
        if(nums.size() == 0)
            return 0;
        
        int pre1 = 0;
        int pre2 = 0;
        for(auto num : nums)
        {
            int temp = pre1;
            pre1 = max( pre2 + num, pre1);
            pre2 = temp;
        }
        return pre1;
    }

202. Happy Number

Write an algorithm to determine if a number is “happy”.

A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the process until the number equals 1 (where it will stay), or it loops endlessly in a cycle which does not include 1. Those numbers for which this process ends in 1 are happy numbers.

Example:

Input: 19
Output: true
Explanation: 
12 + 92 = 82
82 + 22 = 68
62 + 82 = 100
12 + 02 + 02 = 1

Solution in C++:

关键点:

  • 认真读题 -判断环

思路:

  • 开始没认真看题,一直在想要么死循环了,要么最后是1怎么办呀,这是不是需要数学知识来做啊。完全没想到这个死循环就是环的判断!!!前面几天反复不断出到的检测环的方法又可以用到了。最后后悔死循环所以基本都算是环,只要对比最后的值是否为1即可。
int Happy(int n)
    {
        int result = 0;
        while(n)
        {
            result += (n % 10) * (n % 10);
            n /= 10;
        }
        return result;
    }
    
    bool isHappy(int n) {
        int slow = n;
        int fast = n;
        do{
            slow = Happy(slow);
            fast = Happy(fast);
            fast = Happy(fast);
        } while(slow != fast);
            
        if(slow == 1)
            return true;
        else 
            return false;
    }

203. Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.
Example:

Input:  1->2->6->3->4->5->6, val = 6
Output: 1->2->3->4->5

Solution in C++:

关键点:

  • 内存泄露 链表删除操作

思路:

  • 就是简单的链表删除操作,开始就是这样想的,但是最后完全写出来竟然还是花了至少20min,一个点是我不知道起始循环点怎么设置了,知道需要一个pre,cur指针。后来发现只要把头指针部分确定不是需要删除的数就OK。然后就是一些情况考虑不全,也算是代码不太规范的问题,在对指针操作之前没有判断非空。
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* removeElements(ListNode* head, int val) {
        
        ListNode *cur = head;
        // 将头部是需要删除的结点均删除
        while(cur && cur->val == val)
        {
            cur = cur->next;
        }
        
        head = cur;
        
        ListNode *pre = cur;
        if (cur != nullptr)
            cur = cur->next;
        
        while(cur)
        {
            if(cur->val == val)
            {
                pre->next = cur->next;
                free(cur);
                cur = pre->next;
            } else{
                pre = cur;
                cur = cur->next;
            }       
        }
        
        return head;
    }
};

小结

今天几点收获,一个是自己的基础算法思想基本忘的差不多了,必须要么采取边学边code方法,要么就是专题训练的方式去提高了,不然思想还是没掌握。然后就是抽象能力还很弱,也可能跟自己没有总结自己学到的方法有关,从现在开始要把学到的方法开始总结起来。

知识点

  • 递归及其优化思想
  • 环的检测
  • 链表删除

猜你喜欢

转载自blog.csdn.net/wait_for_taht_day5/article/details/82875425