2016.5.15——leetcode-HappyNumber,House Robber

leetcode:HappyNumber,House Robbe

1.Happy Number

这个题中收获2点:

1.拿到题以后考虑特殊情况,代码中考虑1和4,或者说<6的情况,动手算下。(可能要在代码一步步测试中发现,目前还不知道怎么知道这些特殊情况)

2.数字的每一位时,n%10,n/10。

  题目:

  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: 19 is a happy number

  • 12 + 92 = 82
  • 82 + 22 = 68
  • 62 + 82 = 100
  • 12 + 02 + 02 = 1

  思路:

  我的思路:(作为你个程序末末,希望大家不要嘲笑)

    将数字用pow(x,2)来计算,但是不知道怎么写,好吧,跟没有思路一样.

  Leetcode/discuss中的思路:

    (n%10)*(n%10),n/10.

    Tip:在leetcode上每道题都有个discuss,从里面可以看到大家的思路及讨论,个人觉得很不错

  代码:

    有两个我认为写的比较简洁且易懂的代码。

  代码1:

 1 bool isHappy(int n) {
 2             int num=0;
 3             while(n!=1&&n!=4)         //1为初始值,如何平方都为1。4则进入死循环
 4             {
 5                 while(n)
 6                 {
 7                     num += (n%10) * (n%10);    //循环将每位平方
 8                     n/=10;
 9                 }
10                 n=num;
11                 num=0;
12             }
13             return 1==n;
14         }            

代码2:

 1 class Solution {
 2 public:
 3     bool isHappy(int n) {
 4         while(n>6)
 5     {
 6         int next = 0;
 7         while(n)
 8             {
 9                  next+=(n%10)*(n%10);
10                  n/=10;
11              }
12         n = next;
13     }
14     return n==1;
15     }
16 };        

代码1是将数字“1”和“4”单独拿出来看,代码2则是直接从n大于6看。其实两者是一样的,可以自己用算下,“2”和”4“是一样的,”3“,”5“,”6“最终也会进入”2“,”4“循环。

22 =4

42=8

82=16

12+62=37

32+72=58

52+82=89

82+92=145

12+42+52=42

42+22=20

22=4

32=9

92=81

82+12=65

62+52=61

62+12=37(回到22中的循环)

52=25

22+52=29

22+92=85

82+52=89(回到22中的循环)

62=36

32+62=45

42+52=41

42+12=17

12+72=50

52=25(回到52中的循环)

2.House Robber

题目收获:

  1. 如何用迭代
  2. 关于C++代码编辑器闪退的:

      1).在上述代码位置处加入语句 cin.get();

      2).同上述代码添加位置,加入语句 system("pause");

  题目:

  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.

  思路:

  我的思路:

    给定数组,求数组中不相邻的数字的最大和。但是不知道代码怎么写。

  Leetcode/discuss中的思路:

    跟我的相同,求数组中不相邻的数字的最大和。

  代码:

  贴出3段代码,前两个是写的简洁易懂的,后面一个是有问题的代码。

  代码1:C++代码

class Solution {
public:
    int rob(vector<int>& nums) {
        int n = nums.size(), pre = 0, cur = 0;    //cur为i-1时的总和,pre为i-2的总和
        for (int i = 0; i < n; i++) {
            int temp = max(pre + nums[i], cur);    //如果pre+nums[i](即i-2时的总值)大于cur(即i-1时的总值),此时的总和值为pre+nums[i],否则为cur.
            pre = cur;
            cur = temp;
        }
        return cur;
    }
};

  代码2:c代码,但是思路非常清楚

 1 int max(int a, int b) {
 2      return a > b ? a : b;
 3  }
 4
 5 int rob(int* nums, int numsSize) {
 6     int* d;
 7
 8     d = (int*) malloc (numsSize * sizeof(nums[0]));
 9
10     d[0] = nums[0];
11     d[1] = max (nums[0], nums[1]);
12
13 // d[i] means the most value that can be robbed before the ith store. For each store,  // we have two choice: rob or not rob: (1)if robbing, d[i] = d[i-2] + nums[i], for stores robbed cannot be connected. (2)if not robbing, d[i] = d[i-1]
14
15     for (int i = 2; i < numsSize; i++) {
16         d[i] = max(d[i-2]+nums[i], d[i-1]);
17     }
18
19     return d[numsSize-1];
20 }

  代码3:错误思路

#define max(a, b) ((a)>(b)?(a):(b))
int rob(int num[], int n) {
    int a = 0;
    int b = 0;

    for (int i=0; i<n; i++)
    {
        if (i%2==0)     //以奇偶来求最大,奇数和偶数必然不相邻,但却不一定是最大
        {
            a = max(a+num[i], b);
        }
        else
        {
            b = max(a, b+num[i]);
        }
    }

    return max(a, b);
}    

  代码3的错误原因奇偶不一定就是最大,比如a = { 1, 4, 2, 2, 4, 5, 9, 5 }这数组的最大为17(只选9,4,4),奇数为16,偶数16。可知看奇偶思路是错的。

  带主函数的:不会写主函数,不过还是自己吭哧吭哧出来一个主函数的代码,还不知道测试的边界条件是什么。

#include "stdafx.h"    //控制台头文件
#include "iostream"    //cin,cout输入输出的头文件
#include "vector"        //vector的头文件
#include "algorithm"    //代码中的max()函数的头文件,min()也是这个
using namespace std;    //C++必须写,还不知道为什么

class Solution
{
public:
    int rob(vector<int>& nums)
    {
        int cur = 0;
        int pre = 0;
        int temp = 0;

        for (size_t i = 0; i < nums.size(); i++)   //在visul studio中必须写错size_t不然会报错,但是leetcode上的测试不用,直接写成int就可以
        {
            temp = max(pre + nums[i], cur);
            pre = cur;
            cur = temp;
        }
        return cur;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    vector<int> a = { 1, 4, 2, 2, 4, 5, 9, 5 };    //测试的,本来想写成不固定的数组,数组由输入来的,暂时还不会
    int m;
    Solution solution;    //貌似必须这么写,不能直接引用
    m = solution.rob(a);
    cout << "the money is " << m << endl;
    cin.get();    //防止编辑器闪退
    return 0;
}

  

时间: 2024-10-20 15:22:51

2016.5.15——leetcode-HappyNumber,House Robber的相关文章

2016/02/15 codes

return e.addTest = function(a,b){ if(typeof a == "object") for(var d in a )y(a,d)&& e.addTest(d,a[d]); else{a = a.toLowerCase(); if(e[a]!== c)return e; b = typeof b = "function"?b():b, typeof f != "undefined" &&am

2016.4.15 -关于分离和总结

2016.4.15 科比退役了,祝老大走好,作为铁杆科密的我没有选择在社交媒体上去煽情和感伤,我只知道,我喜欢科比,是因为他的精神,现在的我需要传承老大的“凌晨四点的洛杉矶”,在内心和行动上默默坚持,默默努力,前行.用自己的实际行动证明一切,而不是说说. 研究生阶段唯一的好友,张同学今天突然告诉我他要选择转博了,没有一点惊喜,反而有点悲伤,剩下的路又要一个人走了,或许是上天对我的考察,,,终于想通 了,其实我们每个人都有自己的路要走,每个人都只能陪你走一段路程而已,剩下的路,你一个人走,要坚持,

2016.2.15 四旋翼相关资料

--------2016.2.15--------最近在玩儿四旋翼,所以相关的资料我会贴上来供需要的朋友参考,如果遇到了我们趟过的地雷就能够方便一些. 机架:F450我们之后由于要将NVIDIA的开发板和飞行器连在一起,所以从机架的选择上我们要慎重考虑,之前为了方便选的是F330,但是DJI正版的F330停产了,所以taobao的F330进行测试,经过测试发现F330相当脆弱,机架非常容易在后写撞击中折断,所以如果上面搭载NVIDIA的开发板的话,那么这样开发的成本太高,一旦炸机可能机毁板子亡,

[LeetCode][JavaScript]House Robber

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 

LeetCode中 House Robber问题随笔

先附上题目链接,有两问: https://leetcode.com/problems/house-robber/ https://leetcode.com/problems/house-robber-ii/ 第一问题目如下: You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constrain

macbook pro 2016 2017 15寸 雷电3 外接显卡 epu 简单教程(不修改UEFI)

雷电3外接显卡效果还不错,但是除了akitio node 其他厂家并不会维护自己的固件来适配新机型,我自己买的mbp 2016 15''就出现了和AORUS Gaming Box 1070不兼容的问题,在引导的时候会在win徽标处转圈卡死,我联系了厂家,客服,给他们打了电话发了邮件,都快变成骚扰了,不过并没有解决问题,得到的回复是问题在解决中,不知道什么时候能解决. 后来在egpu.io上找到了解决方案并尝试成功,效果还不错 https://egpu.io/forums/implementati

LeetCode 213. House Robber II

Note: This is an extension of House Robber. After robbing those houses on that street, the thief has found himself a new place for his thievery so that he will not get too much attention. This time, all houses at this place are arranged in a circle. 

LeetCode 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 autom

2016.5.16——leetcode:Rotate Array,Factorial Trailing Zeroe

Rotate Array 本题目收获: 题目: Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 思路: 我的思路:新建一个数组存放旋转后的内容,但是怎么把原数组的内容存放在数组中,不清楚. leetcode/discuss思路: 思路一:新建数组,复制原