leetcode-7-hashTable

解题思路:
这道题需要注意的是s和t长度相等,但都为空的情况。只需要扫描一遍s建立字典(char, count),然后扫描t,如果有
未出现的字母,或者键值小于0,就可以返回false了。
bool isAnagram(string s, string t) {
if (s.length() != t.length())
return false;
if (s.length() == 0 && t.length() == 0)
return true;
map<char,int> dict;
for (int i = 0; i < s.length(); i++) {
if (dict.find(s[i]) == dict.end()) {
dict.insert(dict.end(), make_pair(s[i],1));
} else
dict.find(s[i])->second++;
}
for (int i = 0; i < t.length(); i++) {
if (dict.find(t[i]) == dict.end())
return false;
else {
dict.find(t[i])->second--;
if (dict.find(t[i])->second < 0)
return false;
}
}
return true;
}
但是,因为题目中说是小写字母,所以可以用数组来做。思路类似,相比使用map插入更简单。
bool isAnagram(string s, string t) {
if (s.length() != t.length()) return false;
int n = s.length();
int counts[26] = {0};
for (int i = 0; i < n; i++) {
counts[s[i] - 'a']++;
counts[t[i] - 'a']--;
}
for (int i = 0; i < 26; i++)
if (counts[i]) return false;
return true;
}
与这道题类似的是这个:

解题思路:
使用数组计数。
bool canConstruct(string ransomNote, string magazine) {
int i;
int arr[26] = {0};
for (i = 0; i < magazine.length(); i++) {
arr[magazine[i]-'a']++;
}
for (i = 0; i < ransomNote.length(); i++) {
arr[ransomNote[i]-'a']--;
if (arr[ransomNote[i]-'a'] < 0)
return false;
}
return true;
}
相同思路的还有这道题:

解题思路:
因为数字是1~n的,所以考虑直接利用下标。将nums[i]-1取绝对值后作为新的索引,将此处的数组值添加负号。
如果再次索引到此处,则可以将此索引值+1加入到result中。
vector<int> findDuplicates(vector<int>& nums) {
vector<int> result;
int i;
for (i = 0; i < nums.size(); i++) {
if (nums[abs(nums[i])-1] < 0)
result.push_back(abs(nums[i]));
else
nums[abs(nums[i])-1] *= -1;
}
return result;
}
相同的还有这道题:
438. Find All Anagrams in a String

解题思路:
同样用数组存p内字母的情况,然后扫s检查就好。需要注意的是memcpy函数的使用,分配空间时,不要用数字。。。
之前我写memcpy(temp, pArr, 26);实际上:

简直呵呵哒。
vector<int> findAnagrams(string s, string p) {
vector<int> result;
if (p.length() > s.length())
return result;
int pArr[26] = {0};
for (int i = 0; i < p.length(); i++) {
pArr[p[i] - 'a'] ++;
}
int i,j,k;
bool flag;
for (i = 0; i <= s.length() - p.length() ; i++) {
int temp[26];
memcpy(temp, pArr, sizeof(pArr));
flag = true;
for (j = i; j < i + p.length(); j++) {
if (temp[s[j] - 'a'] == 0) {
flag = false;
break;
}
else
temp[s[j] - 'a'] --;
}
if (flag == true) {
for (k = 0; k < 26; k++) {
if (temp[k] != 0)
break;
}
if (k == 26) {
result.push_back(i);
}
}
}
return result;
}

解题思路:
本来是用遍历两次数组来算的,外层0~n-1,内层i+1~n-1,找到即跳出。但是如果数组很大,这样无疑很耗时。
由于数组是升序,所以考虑从左右两端开始查找。如果numbers[left]+numbers[right]==target,就找到了;
大于的话right左移,小于的话left右移。当left>=right时,终止。
vector<int> twoSum(vector<int>& numbers, int target) {
vector<int> result;
int left = 0;
int right = numbers.size() - 1;
while(left < right) {
if (numbers[left] + numbers[right] == target) {
result.push_back(left + 1);
result.push_back(right + 1);
break;
}
else if (numbers[left] + numbers[right] > target) {
right --;
}
else {
left ++;
}
}
return result;
}
leetcode-7-hashTable的更多相关文章
- leetcode@ [30/76] Substring with Concatenation of All Words & Minimum Window Substring (Hashtable, Two Pointers)
https://leetcode.com/problems/substring-with-concatenation-of-all-words/ You are given a string, s, ...
- leetcode@ [49] Group Anagrams (Hashtable)
https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...
- LeetCode HashTable 30 Substring with Concatenation of All Words
You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...
- LeetCode:Word Ladder I II
其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...
- Leetcode: Word Squares && Summary: Another Important Implementation of Trie(Retrieve all the words with a given Prefix)
Given a set of words (without duplicates), find all word squares you can build from them. A sequence ...
- LeetCode 刷题顺序表
Id Question Difficulty Frequency Data Structures Algorithms 1 Two Sum 2 5 array + set sort + two poi ...
- python leetcode 日记 --Contains Duplicate II --219
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)
转自 http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...
- leetcode面试准备: CountPrimes
1 题目 Description: Count the number of prime numbers less than a non-negative number, n. 接口:public in ...
- leetcode面试准备:Contains Duplicate I && II
1 题目 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. You ...
随机推荐
- 引入clipboard.js
引入clipboard.js var clipboardJS = new ClipboardJS('#accept-data'); // 括号内的是选择器
- Jenkins+Gitlab+Ansible自动化部署(四)
接Jenkins+Gitlab+Ansible自动化部署(三)https://www.cnblogs.com/zd520pyx1314/p/10235394.html Jenkins应用 Jenkin ...
- 爱上MVC~Web.Config的Debug和Release版本介绍
回到目录 对于web.config来说,我们不会陌生,主要对站点进行相关参数的配置,当它被修改后,IIS里对应的应用程序池会被重启,而对于config里的一些配置我们一般使用比较多的是数据连接串con ...
- Kaggle八门神器(一):竞赛神器之XGBoost介绍
Xgboost为一个十分有效的机器学习模型,在各种竞赛中均可以看到它的身影,同时Xgboost在工业届也有着广泛的应用,本文以Titanic数据集为研究对象,简单地探究Xgboost模型建模过程,同时 ...
- Nginx中的Rewrite的重定向配置与实践
阅读目录 一:理解地址重写 与 地址转发的含义. 二:理解 Rewrite指令 使用 三:理解if指令 四:理解防盗链及nginx配置 简介:Rewrite是Nginx服务器提供的一个重要的功能, ...
- Java 8特性尝鲜:新新IO
Java 8特性尝鲜:新新IO 在这个专题前面的文章中,我们已经看到,使用Java8的lambda表达式对现有的JDK1.2 I/O库的提升,主要是可以使用lambda表达式来构造java.io.Fi ...
- VS2010/VS2013项目创建及通过ADO.NET连接mysql/sql server步骤(VS2013连接成功步骤见上一篇随笔)
本随笔主要是对初学者通过ADO.NET连接数据库的步骤(刚开始我也诸多不顺,所以总结下,让初学者熟悉步骤) 1.打开VS新建一个项目(这里的VS版本不限,建项目都是一样的步骤) VS2010版本如图: ...
- css3相关样式
1.渐变 1.1 线性渐变(Linear Gradients)- 向下/向上/向左/向右/对角方向 background: linear-gradient(direction, color-stop1 ...
- B/S架构 C/S架构 SOA架构
一.什么是C/S和B/S 第一.什么是C/S结构.C/S (Client/Server)结构,即大家熟知的客户机和服务器结构.它是软件系统体系结构,通过它可以充分利用两端硬件环境的优势,将任务合理分配 ...
- vue2.0:(十)、外卖App商品组件部分和better-scroll
本篇中继续来给大家介绍外卖App制作中的商品组件的部分. 好,第一步,我们把商品的大致框架在goods.vue中搭建出来: menu-wrapper是左边菜单栏,foods-wrapper是右边商品栏 ...