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的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [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 ...

  4. 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 ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

  10. Leetcode 笔记 101 - Symmetric Tree

    题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...

随机推荐

  1. 分享基于 websocket 网页端聊天室

    博客地址:https://ainyi.com/67 有一个月没有写博客了,也是因为年前需求多.回家过春节的原因,现在返回北京的第二天,想想,应该也要分享技术专题的博客了!! 主题 基于 websock ...

  2. java实现 批量转换文件编码格式

    一.场景说明 不知道大家有没有遇到过之前项目是GBK,现在需要全部换成UTF-8的情况.反正我是遇到了. eclipse可以改变项目的编码格式,但是文件如果直接转换的话里面的中文就会全部乱码,需要先复 ...

  3. 关于时间的那些事--PHP、JavaScript、MySQL操作时间

    PHP篇 PHP中时间操作单位是秒 一.将时间戳转为普通日期格式 //当前时间戳 time(); //当前时间格式 date("Y-m-d H:i:s",time()); //昨天 ...

  4. SAP 没有激活HUM功能照常可以使用Handling Unit

    SAP 没有激活HUM功能照常可以使用Handling Unit 笔者所在的项目上的公司间STO的流程里,发货公司在做PGI之后系统自动触发收货公司的inbound delivery单据,发货公司发出 ...

  5. 两种方法实现asp.net方案的前后端数据交互(aspx文件、html+ashx+ajax)

    一个HTML页面只能显示HTML代码信息,不能与数据库进行数据的交互.asp.net方案提供了网页与数据库交互的方法,这里举出两种:①aspx文件 ②ashx文件+ajax技术 一.创建数据库 这里以 ...

  6. Jmeter使用JDBC请求简介

    1.现在oracle或mysql的jdbc然后放到jmeter的lib路径下 2.添加jdbc默认请求控件. 3.添加jdbc请求 4.发送 5.出现ORA-00911错误是由于sql语句错误,注意别 ...

  7. ASP.Net Core开发(踩坑)指南

    ASP.NET与ASP.NET Core很类似,但它们之间存在一些细微区别以及ASP.NET Core中新增特性的使用方法,在此之前也写过一篇简单的对比文章ASP.NET MVC应用迁移到ASP.NE ...

  8. java获取机器IP地址常用方法

    private String getHostIP(){ Enumeration<NetworkInterface> allNetInterfaces = null; String resu ...

  9. 我的第一个python web开发框架(41)——总结

    我的第一个python web开发框架系列博文从17年6.7月份开始写(存了近十章稿留到9月份才开始发布),到今天结束,一年多时间,想想真不容易啊. 整个过程断断续续,中间有段时间由于工作繁忙停了好长 ...

  10. js深浅拷贝

    作为一枚前段,我们知道对象类型在赋值的过程中其实是复制了地址,从而会导致改变了一方其他也都被改变的情况.通常在开发中我们不希望出现这样的问题,我们可以使用浅拷贝来解决这个情况. 浅拷贝 首先可以通过O ...