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 ...
随机推荐
- 关于企业邮箱无法提醒解决办法(未安装邮件客户端可添加至网易邮箱大师/qq邮箱等)
关于企业邮箱无法提醒解决办法: 一.使用现有的邮箱客户端,以下以网易的邮箱大师为例mail.exe 点击客户端左边的添加邮箱账号,在出现的对话框中输入账号和密码后,点击登陆按钮后,等待添加完成即可,邮 ...
- 053 Maximum Subarray 最大子序和
给定一个序列(至少含有 1 个数),从该序列中寻找一个连续的子序列,使得子序列的和最大.例如,给定序列 [-2,1,-3,4,-1,2,1,-5,4],连续子序列 [4,-1,2,1] 的和最大,为 ...
- Jenkins+Gitlab+Ansible自动化部署(六)
Pipeline Job实现Nginix+MySQL+PHP+Wordpress实现自动化部署交付(Jenkins+Gitlab+Ansible自动化部署(五)https://www.cnblogs. ...
- nodejs 实践:express 最佳实践(八) egg.js 框架的优缺点
nodejs 实践:express 最佳实践(八) egg.js 框架的优缺点 优点 所有的 web开发的点都考虑到了 agent 很有特色 文件夹规划到位 扩展能力优秀 缺点 最大的问题在于: 使用 ...
- SQL查询-约束-多表
一.SQL语句查询 1.聚合函数 COUNT()函数,统计表中记录的总数量 注:COUNT()返回的值为Long类型;可通过Long类的intValue()方法 ...
- 关于死循环while(true){}或for(;;){}的总结
关于死循环while(true){}或for(;;){}的总结 1.基本用法: while(true){ 语句体; } for(;;){ 语句体; } 以上情况,语句体会一直执行. 2 ...
- SQL 中的group by (转载)
概述 原始表 简单Group By Group By 和 Order By Group By中Select指定的字段限制 Group By All Group By与聚合函数 Having与Where ...
- Python3中requests库学习01(常见请求示例)
1.请求携带参数的方式1.带数据的post data=字典对象2.带header的post headers=字典对象3.带json的post json=json对象4.带参数的post params= ...
- 摘自 dd大牛的《背包九讲》
P01: 01背包问题 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大. 基本思路 这是最 ...
- Python正则表达式计算器流程图