Leetcode 5
HashTable Easy
1. 136. Single Number
0与0异或是0,1与1异或也是0,那么我们会得到0
class Solution {
public:
int singleNumber(vector<int>& nums) {
int res = ;
for( auto num : nums) res ^= num;
return res;
}
};
2. 202. Happy Number
用 HashSet 来记录所有出现过的数字,然后每出现一个新数字,在 HashSet 中查找看是否存在,若不存在则加入表中,若存在则跳出循环,并且判断此数是否为1,若为1返回true,不为1返回false
class Solution {
public:
bool isHappy(int n) {
unordered_set<int> st;
while(n != ){
int sum = ;
while(n){
sum += ( n % ) * ( n % );
n /= ;
}
n = sum;
if(st.count(n))
break;
st.insert(n);
}
return n == ;
}
};
3. 204. Count Primes
大概步骤为,第一次筛选2的倍数的数字,将其都筛选出去,第二轮筛选3的倍数的数字,筛选后,剩下的第一个数字就是5(因为4在第一次筛选的时候作为2的倍数已经筛出去)第三轮则筛选5倍数的数字,第四轮7倍数,第五轮11倍数……依次筛选下去,筛n轮。
class Solution {
public:
int countPrimes(int n) {
int res = ;
vector<bool> prime(n,true);
for(int i = ; i < n ; i++){
if(prime[i])
res++;
for(int j = ; i*j < n; j++){
prime[i*j] = false;
}
}
return res;
}
};
4. 290. Word Pattern
map.put(),返回的值是与当前key相同的值所对应的value,也就是之前出现过key值的编号。返回的是一个对象用Integer。
class Solution {
public boolean wordPattern(String pattern, String str) {
String[] words = str.split(" ");
if(words.length != pattern.length())
return false;
Map index = new HashMap();
for(Integer i=0; i<words.length; i++){
if( index.put(pattern.charAt(i), i) != index.put(words[i],i))
return false;
}
return true;
}
}
Leetcode 5的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串
Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...
- Leetcode 笔记 113 - Path Sum II
题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
随机推荐
- Virtual Box虚拟机Ubuntu系统安装及基本配置
Linux简介 什么是 Linux? Linux:世界上不仅只有一个 Windows 操作系统,还有 Linux.mac.Unix 等操作系统.桌面操作系统下 Windows 是霸主,而 Linux ...
- pwnable.tw dubblesort 分析
本系列为用于记录那些比较有意思的题目. 题目为一个排序算法,就如题目名称那样,dubblesort,32位程序. 利用思路为栈溢出,先是栈溢出泄露出栈上libc的相关数据从而获取libc地址,再是栈溢 ...
- GoLang simple-project-demo-02
GoLang 有很多种数据类型:字符型,整型,浮点型,布尔型.下面是基础例子: package main import "fmt" func main() { fmt.Printl ...
- CentOS 7.4 下 如何部署 AspNetCore 结合 consul
上篇我们讲到consul的概念,以及在WIN下如何使用:http://www.cnblogs.com/szlblog/p/9162557.html 步骤如下: 1.安装虚拟机VM 2.下载安装 Cen ...
- vscode下面开发vue.js项目
vscode下面开发vue.js项目 https://blog.csdn.net/linzhiqiang0316/article/details/79176651 vscode下面开发vue.js ...
- 2017年IT行业测试调查报告
在刚刚过去的2017年, 我们来一起看一下2017年IT行业测试调查报告 还是1到5名测试工程师最多 Test Architects 在北上广一线城市已经出现 https://www.lagou.co ...
- 简述ADO.NET命名空间
system.data命名空间的类型 system.data命名空间的核心成员 命名空间 作用 Constraint 表示某个DataColumn对象的约束 DataColumn 表示某个DataT ...
- js转换时间戳-转换成 yyyy-MM-dd HH:mm:ss
比如:转换成 yyyy-MM-dd HH:mm:ss //时间戳转换方法 date:时间戳数字 function formatDate(date) { var date = new Date(date ...
- DevExtreme App 开发记要
添加插件 除提供的标准插件外,可直接在config.xml中书写配置,然后编译模板,在后台能看到相关的插件了 无法显示百度地图 在IPHONE中正常加载地图,但在安卓中提示BM ...
- <自动化测试方案_8>第八章、手机端UI自动化测试
第八章.手机端UI自动化测试 (一)APP测试分类 1,原生APP:Native页面是使用原生系统内核的,相当于直接在系统上操作 2,H5APP:先调用系统的浏览器内核,相当于是在网页中进行操作,较原 ...