leetcode:HappyNumber,House Robber

1.Happy Number

这个题中收获2点:

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

2.数字的每一位时,n%10,n/10。(3/7的余数为3,7/3的余数为4)

  题目:

  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:

 bool isHappy(int n) {
int num=;
while(n!=&&n!=) //1为初始值,如何平方都为1。4则进入死循环
{
while(n)
{
num += (n%) * (n%); //循环将每位平方
n/=;
}
n=num;
num=;
}
return ==n;
}

代码2:

 class Solution {
public:
bool isHappy(int n) {
while(n>)
{
int next = ;
while(n)
{
next+=(n%)*(n%);
n/=;
}
n = next;
}
return n==;
}
};

代码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 = , cur = ; //cur为i-1时的总和,pre为i-2的总和
for (int i = ; 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代码,但是思路非常清楚

 int max(int a, int b) {
return a > b ? a : b;
} int rob(int* nums, int numsSize) {
int* d; d = (int*) malloc (numsSize * sizeof(nums[])); d[] = nums[];
d[] = max (nums[], nums[]); // 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] for (int i = ; i < numsSize; i++) {
d[i] = max(d[i-]+nums[i], d[i-]);
} return d[numsSize-];
}

  代码3:错误思路

 #define max(a, b) ((a)>(b)?(a):(b))
int rob(int num[], int n) {
int a = ;
int b = ; for (int i=; i<n; i++)
{
if (i%==) //以奇偶来求最大,奇数和偶数必然不相邻,但却不一定是最大
{
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 = ;
int pre = ;
int temp = ; for (size_t i = ; 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 = { , , , , , , , }; //测试的,本来想写成不固定的数组,数组由输入来的,暂时还不会
int m;
Solution solution; //貌似必须这么写,不能直接引用
m = solution.rob(a);
cout << "the money is " << m << endl;
cin.get(); //防止编辑器闪退
return ;
}

程序运行中出现一个错误:expected initializer before '<' token 

  错误原因是:第16行中的 for循环内分号“;”误写成了“,”。

2016.5.14——leetcode-HappyNumber,House Robber的更多相关文章

  1. 2016.09.14,英语,《Using English at Work》全书笔记

    半个月时间,听完了ESLPod出品的<Using English at Work>,笔记和自己听的时候的备注列在下面.准备把每个语音里的快速阅读部分截取出来,放在手机里反复听. 下一阶段把 ...

  2. Leetcode 337. House Robber III

    337. House Robber III Total Accepted: 18475 Total Submissions: 47725 Difficulty: Medium The thief ha ...

  3. 【算法题 14 LeetCode 147 链表的插入排序】

    算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...

  4. Murano Weekly Meeting 2016.06.14

    Meeting time: 2016.June.14 1:00~2:00 Chairperson:  Kirill Zaitsev, from Mirantis Meeting summary: 1. ...

  5. [LeetCode] 213. House Robber II 打家劫舍 II

    Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...

  6. [LeetCode] 337. House Robber III 打家劫舍 III

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

  7. leetcode:House Robber(动态规划dp1)

    You are a professional robber planning to rob houses along a street. Each house has a certain amount ...

  8. 2016.5.15——leetcode:Number of 1 Bits ,

    leetcode:Number of 1 Bits 代码均测试通过! 1.Number of 1 Bits 本题收获: 1.Hamming weight:即二进制中1的个数 2.n &= (n ...

  9. [LeetCode] 337. House Robber III 打家劫舍之三

    The thief has found himself a new place for his thievery again. There is only one entrance to this a ...

随机推荐

  1. 【CSS】规范大纲

    文件规范: 文件分类 : 通用类 :业务类. 文件引入:行内样式(不推荐):外联引入:内联引入.(避免使用Import引入) 文件本身:文件名. 编码:UTF-8. 注释规范: 块状注释:统一缩进,在 ...

  2. 执行SCP命令拷贝文件失败

    scp是我最常用的远程文件拷贝工具,今天发现突然不能用了: svan-mac:hive-tools xiean$ scp  -Pxx target/urs-hive-tools-jar-with-de ...

  3. 5W2H方法

    5W2H分析方法也叫七问分析法,是二战中美国陆军兵器修理部首创.简单.方便.易于理解.使用,富有启发意义,被广泛应用于企业管理和技术活动,对于决策和执行性的措施也非常有帮助,有助于弥补考虑问题的疏漏 ...

  4. ldap禁止匿名用户登录

    此处默认ldap已经安装完成,安装文档传送门:https://www.cnblogs.com/crysmile/p/9470508.html openldap默认安装完成,是允许匿名用户登录的,因此需 ...

  5. 利用Eric+Qt Designer编写倒计时时钟

    [前言]前几日通过编写命令行通讯录,掌握了Python的基本语法结构,于是开始向更高水平冲击,利用Eric与Qt Designer 编写一个带界面的小程序.本次实操中也确实遇到了不少问题,通过学习也都 ...

  6. Linux服务器中安装python包管理工具pip

    pip是python的包管理工具,python的强大之处除了在于语法的简练,还有就是对众多的库支持了. 1.下载pip包管理工具 链接地址:https://pypi.python.org/pypi/p ...

  7. 【HDU5469】Antonidas(点分治,字符串哈希)

    [HDU5469]Antonidas(点分治,字符串哈希) 题面 HDU Vjudge 题解 啊哈?什么垃圾一眼点分治+Hash判断,哈哈哈哈哈,让我来码码码. 诶,怎么WA了.改改改改改. 诶,怎么 ...

  8. NOIWC前的交流题目汇总

    RT 2018.12.27 i207M:BZOJ 4695 最假女选手 以维护最大值为例,记录最大值和严格次大值和最大值的出现次数,然后取min的时候递归到小于最大值但大于次大值修改,这个就是最重要的 ...

  9. Qt 查找功能

    版权声明 该文章原创于Qter开源社区(www.qter.org),作者yafeilinux,转载请注明出处!     导语 这一篇我们来加上查找菜单的功能.因为要涉及Qt Creator的很多实用功 ...

  10. config配置中心之自动刷新

    自动刷新(自动刷新是基于springcloudbus来实现的,springcloud bus是基于rabbitMQ或者Kafka来实现的) Spring Cloud Bus 将分布式的节点用轻量的消息 ...