198. House Robber

相邻不能打劫,取利益最大化。

思想:当前值和前一个和的总数   与  前一个和    做大小比较,取最大值,重复该步骤。

  1. class Solution {
  2. public:
  3. int rob(vector<int>& nums) {
  4. const int n = nums.size();
  5. if (n == ) return ;
  6. if (n == ) return nums[];
  7. if (n == ) return max(nums[], nums[]);
  8. vector<int> f(n, );
  9. f[] = nums[];
  10. f[] = max(nums[], nums[]);
  11. for (int i = ; i < n; ++i)
  12. f[i] = max(f[i-] + nums[i], f[i-]);
  13. return f[n-];
  14. }
  15. };

202. Happy Number

  1. class Solution {
  2. public:
  3. bool isHappy(int n) {
  4. unordered_map<int,int> tmp;
  5.  
  6. while(n != )
  7. {
  8. if(tmp[n] == ) //如果出现了循环,则直接返回false
  9. tmp[n]++;
  10. else
  11. return false;
  12.  
  13. int sum = ;
  14. while(n != )
  15. {
  16. sum += pow(n % ,);
  17. n = n / ;
  18. }
  19.  
  20. n = sum;
  21. }
  22.  
  23. return true;
  24. }
  25. };
  26.  
  27. ///////////////////////////
  28.  
  29. class Solution {
  30. public:
  31. int next(int n)
  32. {
  33. int sum = ;
  34.  
  35. while(n != )
  36. {
  37. sum += pow(n % ,);
  38. n = n / ;
  39. }
  40.  
  41. return sum;
  42. }
  43.  
  44. public:
  45. bool isHappy(int n) {
  46. int slow = next(n);
  47. int fast = next(next(n));
  48.  
  49. while(slow != fast)
  50. {
  51. slow = next(slow);
  52. fast = next(next(fast));
  53. }
  54.  
  55. return fast == ;
  56. }
  57. };

204. Count Primes

思路:在i × i 的基础上递进 i,这些都不是素数;

  1. class Solution {
  2. public:
  3. int countPrimes(int n) {
  4. //Sieve of Erantothoses
  5. vector<bool> check(n+,true);
  6.  
  7. //Because 0 and 1 are not primes
  8. check[]=false;
  9. check[]=false;
  10.  
  11. //OPtimization 2: Do only till rootn since all numbers after that are handled
  12. //The remaining values are already true
  13. for(int i=;i*i<=n;i++)
  14. {
  15. //If already visited
  16. if(check[i]==false) continue; //访问过的不重复
  17.  
  18. //Optimation 1 : 3*2 is already handled by 2*3. Toh directly start from 9
  19. int j=i*i;
  20. while(j<=n)
  21. {
  22. check[j]=false;
  23. j = j+i; ##以i*i基础上每次增加i,这些都可以化为i的倍数,所以不是素数
  24. }
  25.  
  26. }
  27.  
  28. int count=;
  29. //Checking all the numbers which are prime (less than n)
  30. for(int i=;i<n;i++)
  31. if(check[i]) count++;
  32.  
  33. return count;
  34. }
  35. };
 

219. Contains Duplicate II

Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k.

思路:固定窗口滑动,左窗口利用erase来滑动,右边利用i来滑动。

  1. class Solution {
  2. public:
  3. bool containsNearbyDuplicate(vector<int>& nums, int k)
  4. {
  5. unordered_set<int> s;
  6.  
  7. if (k <= ) return false;
  8. if (k >= nums.size()) k = nums.size() - ;
  9.  
  10. for (int i = ; i < nums.size(); i++)
  11. {
  12. if (i > k) s.erase(nums[i - k - ]);
  13. if (s.find(nums[i]) != s.end()) return true;
  14. s.insert(nums[i]);
  15. }
  16.  
  17. return false;
  18. }
  19. };

231. Power of Two

Power of 2 means only one bit of n is '1', so use the trick n&(n-1)==0 to judge whether that is the case

  1. class Solution {
  2. public:
  3. bool isPowerOfTwo(int n) {
  4. if(n<=) return false;
  5. return !(n&(n-));
  6. }
  7. };

234. Palindrome Linked List

思路:中间为界,翻转后半部分,对照前半部分是否相等;  定位中界使用快慢指针。

  1. class Solution {
  2. public:
  3. bool isPalindrome(ListNode* head) {
  4. if(head==NULL||head->next==NULL)
  5. return true;
  6. ListNode* slow=head;
  7. ListNode* fast=head;
  8. while(fast->next!=NULL&&fast->next->next!=NULL){
  9. slow=slow->next;
  10. fast=fast->next->next;
  11. }
  12. slow->next=reverseList(slow->next);
  13. slow=slow->next;
  14. while(slow!=NULL){
  15. if(head->val!=slow->val)
  16. return false;
  17. head=head->next;
  18. slow=slow->next;
  19. }
  20. return true;
  21. }
  22. ListNode* reverseList(ListNode* head) {
  23. ListNode* pre=NULL;
  24. ListNode* next=NULL;
  25. while(head!=NULL){
  26. next=head->next;
  27. head->next=pre;
  28. pre=head;
  29. head=next;
  30. }
  31. return pre;
  32. }
  33. };

leetcode 198-234 easy的更多相关文章

  1. [LeetCode] 198. 打家劫舍II ☆☆☆(动态规划)

    描述 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的 ...

  2. leetcode 198. House Robber (Easy)

    https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...

  3. (easy)LeetCode 198.House Robber

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

  4. Leetcode:234 回文链表

    leetcode:234 回文链表 关键点:请判断一个链表是否为回文链表.示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true. ...

  5. [LeetCode] 198. House Robber 打家劫舍

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

  6. LeetCode 198. 打家劫舍(House Robber) 5

    198. 打家劫舍 198. House Robber 题目描述 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两 ...

  7. leetcode 198

    198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...

  8. leetcode 198打家劫舍

    讲解视频见刘宇波leetcode动态规划第三个视频 记忆化搜索代码: #include <bits/stdc++.h> using namespace std; class Solutio ...

  9. 【leetcode】234. Palindrome Linked List

    234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...

  10. 【Leetcode】【Easy】String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

随机推荐

  1. mac下解压bin文件

    在mac下要解压Android-ndk-r10e-darwin-x86_64.bin文件. 1.进入文件所在目录,修改文件的读取权限 chmod a+x android-ndk-r10e-darwin ...

  2. js 倒计时毫秒级别显示

    <html> <head> <style> div{ width:100%; text-align:center; font-size: 14px; } </ ...

  3. js 实现横向轮播效果

    参考:https://www.cnblogs.com/LIUYANZUO/p/5679753.html html: <!DOCTYPE html> <html> <hea ...

  4. 廖雪峰Java10加密与安全-5签名算法-2DSA签名算法

    DSA DSA:Digital Signature Algorithm,使用EIGamal数字签名算法,和RSA数字签名相比,DSA更快. DSA只能配合SHA使用: SHA1withDSA SHA2 ...

  5. 自动化运维工具Ansible工具

    目录 一.初识Ansible 二.Ansible的架构 三.Ansible基础使用 安装 主机清单 管理主机 四.Ansible用脚本管理主机 五.Ansible模块Module 六.Ansible常 ...

  6. sudo apt-get update报错E: 部分索引文件下载失败。如果忽略它们,那将转而使用旧的索引文件。

    解决方案1: 将对应的PPA删除掉即可 cd /etc/apt/suorces.list.d mv **.list **.list.bak 解决方案2: 更改源 cp /etc/apt/source_ ...

  7. (转)AngularJS判断checkbox/复选框是否选中并实时显示

    最近做了一个选择标签的功能,把一些标签展示给用户,用户选择自己喜欢的标签,就类似我们在购物网站看到的那种过滤标签似的: 简单的效果如图所示: 首先看一下html代码: <!DOCTYPE htm ...

  8. sqlserver 创建用户 sp_addlogin

    创建新的 Microsoft® SQL Server™ 登录,使用户得以连接使用 SQL Server 身份验证的 SQL Server 实例.  语法: sp_addlogin [ @loginam ...

  9. 设置div背景图片填满div

    可以设置div的样式为 background:url('+UPLOAD_PATH+data.url+') no-repeat; background-size: 100%;width:100%;hei ...

  10. C# 制作ActiveX控件并添加到网页

    1.创建ActiveX控件——按钮 2.定义一个接口,并在控件中实现 3.部署安装 4.CAB打包 5.添加到网页中进行测试 一. 创建ActiveX控件——按钮 1.新建一个Window窗体控件库项 ...