#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.

去掉故事背景就是说,一个含有n个元素的数组(元素值当然大于0),从中取出m个位置的元素,要求相邻两个位置的元素最多仅仅能取一个。

求能获得元素值和的最大值

典型的DP问题。

自顶向下分析问题。n个元素的数组取和的最大值  f(n) ,能够转换为

max{  f(n-1) , f(n-2)+nums[n]  }

利用数组a[n]保存中间子问题结果,算法例如以下:

注:依据故事背景。一条街上的住户不超过1000家吧。。。

//0ms
int rob(int* nums, int numsSize) {
int i=0,t1,t2;
int a[1000]={0};
if(numsSize==1)
{
a[0] = nums[0];
return a[0];
}
if(numsSize==2)
{
a[1] = (nums[0]>=nums[1]) ? nums[0]:nums[1];
return a[1];
}
if(numsSize==3)
{
if(nums[0]+nums[2]>=nums[1])
a[3] = nums[0]+nums[2];
else
a[3] = nums[1];
return a[3];
}
if(numsSize>3)
{
a[0] = nums[0];
a[1] = (nums[0]>=nums[1]) ? nums[0]:nums[1];
if(nums[0]+nums[2]>=nums[1])
a[2] = nums[0]+nums[2];
else
a[2] = nums[1]; for(i=3;i<numsSize;i++)
{
//t1 = rob(nums,numsSize-1);
//t2 = rob(nums,numsSize-2)+nums[numsSize-1];
t1 = a[i-1];
t2 = a[i-2]+nums[i];
a[i] = (t1>=t2)? t1:t2;
}
}
return a[numsSize-1];
}

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

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

Happy Number  指的是数n的每一位的平方和能通过经过有限次循环后和为 1

仅仅要出现循环就不是Happy Number

对于int型(10位)每一位平方和 小于: 9^2  * 10  = 810 ;能够用一个数组保存和 ,当再次出现该值时说明出现了一个循环,返回false

//0ms
bool isHappy(int n) {
int hash[810]={0};
int i=1,new_n=0;
if(n==1)
return true;
while(n!=1)
{
new_n = 0;
while(n)
{
new_n += (n%10)*(n%10);
n = n/10;
}
n = new_n; if(n==1)
return true; if(hash[n]==1)
return false;
else
hash[n] = 1;
}
}

基于事实1 是 Happy Number 而 2,3,4,5,6均不是Happy Number  为了使空间复杂度变为为O(1),可採用例如以下算法

//0ms
bool isHappy(int n)
{
int next;
while(n > 6)
{
next = 0;
while(n)
{
next += (n%10) * (n%10);
n /= 10;
}
n = next;
}
return n == 1;
}

#203 Remove Linked List Elements

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

Example

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

Return: 1 --> 2 --> 3 --> 4 --> 5

<span style="font-size:10px;">//12ms
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* removeElements(struct ListNode* head, int val) {
struct ListNode *newhead,*p,*q;
if(!head)
return NULL;
newhead->next = head; //简化代码,加入头结点 q p
q = newhead;
p = head;
while(p)
{
if(p->val==val)
q->next = p->next;//q 不变 p后移
else
q = p;// q p 都后移
p = p->next;
}
return newhead->next;
}</span>

#204 Count Primes

Count
the number of prime numbers less than a non-negative number, n.

求1~n-1之间素数的个数,注意1不是素数

推断 n 是一个素数的方法是,不能被
2 ~ sqrt(n) 之间的数整除。即约数仅仅有1和其本身

在Leetcode Discuss中看到例如以下解法:时间和空间复杂度都接近O(n)

https://leetcode.com/discuss/34622/my-c-solutions-in-44ms-time-nearly-o-n-and-space-nearly-o-n

//44ms
/*1. trick1 is to use square root of n.
2. trick2 is not to use non-prime numbers as the step
3. trick3 is to use i*i as the start.
4. trick4 is to use count-- in every loop, avoiding another traversal. */
int countPrimes(int n) {
if(n <= 2) return 0;
if(n == 3) return 1;
bool *prime= (bool*)malloc(sizeof(bool)*n);
int i=0,j=0;
int count = n-2;
int rt = sqrt(n);//trick1
for(j = 0; j < n; j++)
{
prime[j] = 1;
}
for(i = 2; i <= rt; i++)
{
if (prime[i])//trick2
{
for(j=i*i ; j<n ; j+=i)//trick3
{
if (prime[j])
{
prime[j]=0;
count--;//trick4
}
}
}
}
free(prime);
return count;
}

Leetcode--easy系列9的更多相关文章

  1. hdu 2049 不easy系列之(4)——考新郎

    不easy系列之(4)--考新郎 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  2. LeetCode——single-number系列

    LeetCode--single-number系列 Question 1 Given an array of integers, every element appears twice except ...

  3. HDU 2045不easy系列之三LELE的RPG难题(趋向于DP的递推)

    不easy系列之(3)-- LELE的RPG难题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Ot ...

  4. hdu1465不easy系列之中的一个(错排)

    版权声明:本文为博主原创文章,未经博主同意不得转载. vasttian https://blog.csdn.net/u012860063/article/details/37512659 转载请注明出 ...

  5. Leetcode算法系列(链表)之删除链表倒数第N个节点

    Leetcode算法系列(链表)之删除链表倒数第N个节点 难度:中等给定一个链表,删除链表的倒数第 n 个节点,并且返回链表的头结点.示例:给定一个链表: 1->2->3->4-&g ...

  6. Leetcode算法系列(链表)之两数相加

    Leetcode算法系列(链表)之两数相加 难度:中等给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字.如果,我们将 ...

  7. leetcode easy problem set

     *勿以浮沙筑高台* 持续更新........     题目网址:https://leetcode.com/problemset/all/?difficulty=Easy 1. Two Sum [4m ...

  8. [Leetcode] Sum 系列

    Sum 系列题解 Two Sum题解 题目来源:https://leetcode.com/problems/two-sum/description/ Description Given an arra ...

  9. LeetCode 笔记系列16.3 Minimum Window Substring [从O(N*M), O(NlogM)到O(N),人生就是一场不停的战斗]

    题目:Given a string S and a string T, find the minimum window in S which will contain all the characte ...

  10. 决战Leetcode: easy part(51-96)

    本博客是个人原创的针对leetcode上的problem的解法,所有solution都基本通过了leetcode的官方Judging,个别未通过的例外情况会在相应部分作特别说明. 欢迎互相交流! em ...

随机推荐

  1. 小学生都能学会的python(闭包和迭代器)

    小学生都能学会的python(闭包和迭代器) 1. 函数名第一类对象 函数名其实就是变量名 1). 可以像变量一样互相赋值. 2). 可以作为函数的参数,进行传递 3). 可以作为返回值返回 4). ...

  2. 5kcrm增加权限管理中的模块(签到统计)

    1 首先在model表增加模块名称 2 在controll里增加方法 3 在授权的html增加表单

  3. 新建Eclipse工作空间,复制原有的配置(转)

    方法一: File->Switch workspace->Other...,按下图选择 只复制简单的配置,如cvs之类的信息是不会复制的. 方法二: 在方法一的基础上做如下操作 将新建的w ...

  4. stl里面stack的注意事项

    1. pop是不返回元素的.因为不能返回引用,只能返回实例.而这个实例是在函数里面初始化的,所以必须在外面再赋值和初始化.而如果实例复制失败,会产生丢失. 2. 而top是可以返回引用的.实际上,返回 ...

  5. Educational Codeforces Round 6 C. Pearls in a Row set

    C. Pearls in a Row There are n pearls in a row. Let's enumerate them with integers from 1 to n from ...

  6. bzoj1570: [JSOI2008]Blue Mary的旅行(二分+网络流)

    1570: [JSOI2008]Blue Mary的旅行 题目:传送门 题解: get到拆点新姿势,还是做题太少了...ORZ 因为每天就只能有一个航班,那就不能直接连了,所以要拆点(然后就被卡住了) ...

  7. xBIM 基础11 WeXplorer 常用事件

    系列目录    [已更新最新开发文章,点击查看详细]  本篇将介绍查看器在不同场合触发的事件.所有这些都记录在xViewer中. 如果您从Web服务器运行本教程,可以在此处查看完整的实例.并且确保您的 ...

  8. Python之Linux下的virtualenv

    在使用 Python 开发的过程中,工程一多,难免会碰到不同的工程依赖不同版本的库的问题: 亦或者是在开发过程中不想让物理环境里充斥各种各样的库,引发未来的依赖灾难. 此时,我们需要对于不同的工程使用 ...

  9. Android setImageResource与setImageBitmap的区别

    同样的布局文件,小分辨率手机: 1.使用setImageBitmap设置时,出现如下现象: 2.使用setImageResource时,图片显示正常 原因:setImageResource(id)会根 ...

  10. [转帖]关于Xilinx下Micro_Blaze中UartLite232外设的使用

    来源:https://blog.csdn.net/shen_you/article/details/78713746