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 ...
随机推荐
- 表单数据验证方法(一)—— 使用validate.js实现表单数据验证
摘要:使用validate.js在前端实现表单数据提交前的验证 好久没写博客了,真的是罪过,以后不能这样了,只学习不思考,学的都是白搭,希望在博客园能记录下自己学习的点滴,虽然记录的都是些浅显的技术, ...
- tomcat部署项目后,war包是否可刪?war包存在必要性!
在tomcat中webapps目錄上傳war包后, 对war解压时候. war不能在tomcat运行时删除,否则会删除自动解压的工程. 你可以停止tomcat后删除war. 当你重新部署的时候,如果 ...
- 倒计时5S秒自动关闭弹窗
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 第十课html5 新增标签及属性 html5学习5
一.常用新增标签 1.header:定义页面的页眉头部 2.nav:定义导航栏 3.footer:定义页面底部,页脚 4.article:定义文章 5.section:定义区域 6.aside:定义侧 ...
- Android Studio教程11-RecycleView的使用
目录 1. RecyclerView 1.1. Add support library 1.2. 将RecyclerView添加到布局 1.3. 主actiivty中如何调用recycleview对象 ...
- Java:配置环境(Mac)——Tomcat
1.官网下载 2.把下载的文档解压,放到合适的路径下. 3.打开eclipse 4.在Apache文件夹下选择Tomcat的对应版本 5.选择刚才下载的文件 6.可以右键Start了
- 好代码是管出来的——.Net Core中的单元测试与代码覆盖率
测试对于软件来说,是保证其质量的一个重要过程,而测试又分为很多种,单元测试.集成测试.系统测试.压力测试等等,不同的测试的测试粒度和测试目标也不同,如单元测试关注每一行代码,集成测试关注的是多个模块是 ...
- MongoDB自学(4)
超过存储上限或记录条数删除最早的记录:db.createCollection("集合名",{capped:true,size:1024,max:100})注解:指定集合名的最大记录 ...
- zoomeye搜索+用selenium实现对佳能打印机的爬虫
本文仅用于学习参考.要遵纪守法哦! 目的:找出一台佳能打印机,得到它的日志文件,并利用其远程打印. 1.先用zoomeye找一个打印机出来,搜索语句:printer +country:"CN ...
- JS的正则表达式及回文
function palindrome(str) { str = str.replace(/\s/g,"").replace(/[^a-zA-Z0-9]/g,"" ...