198. House Robber

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

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

class Solution {
public:
int rob(vector<int>& nums) {
const int n = nums.size();
if (n == ) return ;
if (n == ) return nums[];
if (n == ) return max(nums[], nums[]);
vector<int> f(n, );
f[] = nums[];
f[] = max(nums[], nums[]);
for (int i = ; i < n; ++i)
f[i] = max(f[i-] + nums[i], f[i-]);
return f[n-];
}
};

202. Happy Number

class Solution {
public:
bool isHappy(int n) {
unordered_map<int,int> tmp; while(n != )
{
if(tmp[n] == ) //如果出现了循环,则直接返回false
tmp[n]++;
else
return false; int sum = ;
while(n != )
{
sum += pow(n % ,);
n = n / ;
} n = sum;
} return true;
}
}; /////////////////////////// class Solution {
public:
int next(int n)
{
int sum = ; while(n != )
{
sum += pow(n % ,);
n = n / ;
} return sum;
} public:
bool isHappy(int n) {
int slow = next(n);
int fast = next(next(n)); while(slow != fast)
{
slow = next(slow);
fast = next(next(fast));
} return fast == ;
}
};

204. Count Primes

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

class Solution {
public:
int countPrimes(int n) {
//Sieve of Erantothoses
vector<bool> check(n+,true); //Because 0 and 1 are not primes
check[]=false;
check[]=false; //OPtimization 2: Do only till rootn since all numbers after that are handled
//The remaining values are already true
for(int i=;i*i<=n;i++)
{
//If already visited
if(check[i]==false) continue; //访问过的不重复 //Optimation 1 : 3*2 is already handled by 2*3. Toh directly start from 9
int j=i*i;
while(j<=n)
{
check[j]=false;
j = j+i; ##以i*i基础上每次增加i,这些都可以化为i的倍数,所以不是素数
} } int count=;
//Checking all the numbers which are prime (less than n)
for(int i=;i<n;i++)
if(check[i]) count++; return count;
}
};
 

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来滑动。

class Solution {
public:
bool containsNearbyDuplicate(vector<int>& nums, int k)
{
unordered_set<int> s; if (k <= ) return false;
if (k >= nums.size()) k = nums.size() - ; for (int i = ; i < nums.size(); i++)
{
if (i > k) s.erase(nums[i - k - ]);
if (s.find(nums[i]) != s.end()) return true;
s.insert(nums[i]);
} return false;
}
};

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

class Solution {
public:
bool isPowerOfTwo(int n) {
if(n<=) return false;
return !(n&(n-));
}
};

234. Palindrome Linked List

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

class Solution {
public:
bool isPalindrome(ListNode* head) {
if(head==NULL||head->next==NULL)
return true;
ListNode* slow=head;
ListNode* fast=head;
while(fast->next!=NULL&&fast->next->next!=NULL){
slow=slow->next;
fast=fast->next->next;
}
slow->next=reverseList(slow->next);
slow=slow->next;
while(slow!=NULL){
if(head->val!=slow->val)
return false;
head=head->next;
slow=slow->next;
}
return true;
}
ListNode* reverseList(ListNode* head) {
ListNode* pre=NULL;
ListNode* next=NULL;
while(head!=NULL){
next=head->next;
head->next=pre;
pre=head;
head=next;
}
return pre;
}
};

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. codeforces 1186E- Vus the Cossack and a Field

    传送门:QAQQAQ 题意:给一个01矩阵A,他的相反矩阵为B,每一次变换都会将原矩阵面积乘4成为: AB BA 矩阵的左上角固定,变换无限次,现有q个询问,即求一个矩阵内的1的个数. 思路:因为反转 ...

  2. java线程池的使用学习

    目录 1. 线程池的创建 2. 线程池的运行规则 3. 线程池的关闭 4. 线程池的使用场合 5. 线程池大小的设置 6 实现举例 1. 线程池的创建 线程池的创建使用ThreadPoolExecut ...

  3. Neo4j与springdata集成

    1.maven工程需导入的jar包 <!-- neo4j --> <dependency> <groupId>org.springframework.data< ...

  4. 常用的git操作命令

    整理来源于廖雪峰的git教程https://www.liaoxuefeng.com git: 分布式版本控制系统  本地有完整的代码库,还有远程代码库 svn: 集中式版本控制系统 必须联网时才可提交 ...

  5. python 打印的异常回溯和代码不对应

    正在运行的程序没有停止 又重新install了导致site-packages里的代码改变 正在运行的是老代码, 当出现异常时打印的行数是老代码,但显示的行的内容时新代码

  6. Navicat12激活,最新版本v12.1.18,原版激活[windows]

    1.navicat_premium原版安装包  :官网下载地址2.注册工具  :github地址 本次用到的软件我已经打包好  :    蓝奏云 我安装navicat的路径在:I:\Navicat P ...

  7. js前台中获取后台传的值

    后台controller String ifOffice = "yes";req.setAttribute("ifOffice", ifOffice); 前台j ...

  8. JasperReport编译报表设计5

    我们在前面的章节中产生的JasperReport模板(JRXML文件).这个文件不能直接用于生成报告.它必须被编译成JasperReport的“本地二进制"格式,称为Jasperfile.在 ...

  9. Luogu P2678 跳石头(二分)

    P2678 跳石头 题意 题目背景 一年一度的"跳石头"比赛又要开始了! 题目描述 这项比赛将在一条笔直的河道中进行,河道中分布着一些巨大岩石.组委会已经选择好了两块岩石作为比赛起 ...

  10. Go之路之go语言结构

    Go Hello World 实例 package main //定义了包名,必须在源文件中非注释的第一行指名这个文件属于哪个包,每个Go应用程序都包含一个名为main的包 import " ...