leetcode 198-234 easy
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的更多相关文章
- [LeetCode] 198. 打家劫舍II ☆☆☆(动态规划)
描述 你是一个专业的小偷,计划偷窃沿街的房屋,每间房内都藏有一定的现金.这个地方所有的房屋都围成一圈,这意味着第一个房屋和最后一个房屋是紧挨着的.同时,相邻的房屋装有相互连通的防盗系统,如果两间相邻的 ...
- leetcode 198. House Robber (Easy)
https://leetcode.com/problems/house-robber/ 题意: 一维数组,相加不相邻的数组,返回最大的结果. 思路: 一开始思路就是DP,用一维数组保存dp[i]保存如 ...
- (easy)LeetCode 198.House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- Leetcode:234 回文链表
leetcode:234 回文链表 关键点:请判断一个链表是否为回文链表.示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true. ...
- [LeetCode] 198. House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- LeetCode 198. 打家劫舍(House Robber) 5
198. 打家劫舍 198. House Robber 题目描述 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两 ...
- leetcode 198
198. House Robber You are a professional robber planning to rob houses along a street. Each house ha ...
- leetcode 198打家劫舍
讲解视频见刘宇波leetcode动态规划第三个视频 记忆化搜索代码: #include <bits/stdc++.h> using namespace std; class Solutio ...
- 【leetcode】234. Palindrome Linked List
234. Palindrome Linked List 1. 使用快慢指针找中点的原理是fast和slow两个指针,每次快指针走两步,慢指针走一步,等快指针走完时,慢指针的位置就是中点.如果是偶数个数 ...
- 【Leetcode】【Easy】String to Integer (atoi)
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
随机推荐
- java基础之完数判断
完数: 完全数(Perfect number),又称完美数或完备数,是一些特殊的自然数.它所有的真因子(即除了自身以外的约数)的和(即因子函数),恰好等于它本身.如果一个数恰好等于它的因子之和,则称该 ...
- nginx使用手册--nginx.conf文件配置详解
#运行用户 user nobody; #启动进程,通常设置成和cpu的数量相等 worker_processes 1; #全局错误日志及PID文件 #error_log logs/error.log; ...
- Java超简明入门学习笔记(二)
Java编程思想第4版学习笔记(二) 第三章 操作符 & 第四章 控制执行流程(流程控制语句) 第三章和第四章的内容主要是讲操作符和流程控制语句,Java的大多数操作符和流程控 ...
- springboot新增jsp的支持
一.添加依赖 <!-- 添加对jsp的支持 --> <!-- web 依赖 --> <dependency> <groupId>org.springfr ...
- iotop实时监控磁盘io
介绍 Linux下的IO统计工具如iostat, nmon等大多数是只能统计到per设备的读写情况, 如果你想知道每个进程是如何使用IO的就比较麻烦. iotop 是一个用来监视磁盘 I/O 使用状况 ...
- 基于 Kubernetes 实践弹性的 CI/CD 系统
大家好,我是来自阿里云容器服务团队的华相.首先简单解释一下何为 Kubernetes 来帮助大家理解.Kuberentes 是一个生产可用的容器编排系统.Kuberentes 一方面在集群中把所有 N ...
- OpenCV读取RTSP视频流
用opencv的VideoCapture读取RTSP视频流,只有opencv3.1版本可以,之前的版本都无法读取视频流.可能的原因是云平台的RTSP视频流太差,经常错码.项目最后使用的是opencv2 ...
- Django项目:CRM(客户关系管理系统)--57--47PerfectCRM实现CRM客户报名流程02
图片另存为 16*16 名字修改为 bpm_logo.jpg /*! *bootstrap.js * * Bootstrap v3.3.7 (http://getbootstrap.co ...
- 线性基(模板) LUOGU 3812
题面 解题思路 线性基,是构造出一组数:ax,ax-1-.a1,ax的二进制最高位为x.这些数字能异或和可以表示原来所有数的异或和.其实相当于一个高斯消元的过程.所以我们按位枚举,如果这一位曾经没数, ...
- spring cloud深入学习(十二)-----Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式
Zuul的核心 Filter是Zuul的核心,用来实现对外服务的控制.Filter的生命周期有4个,分别是“PRE”.“ROUTING”.“POST”.“ERROR”,整个生命周期可以用下图来表示. ...