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 ...
随机推荐
- WPF (VisualChildren)可视化子元素详解
VisualChildrenCount 的 FrameworkElement 实现始终返回 0 或 1. 如果类所要维护的可视化子元素集合的成员数可能超过 1,则这样的类必须重写此属性和 Ge ...
- Unity3d中3D Text对模型的穿透显示
昨晚,好友在电话里问我在Unity3d中使用3D Text,不想让其穿透模型显示,即想让场景中的3D Text与模型有正确的遮挡关系,怎么解? 今早谷歌上查了查,明白了原因,因为3D Text的默认材 ...
- CSS filter 属性
filter 将模糊或者颜色偏移等图像效果用于元素,通常用于调整图像,背景和边框的渲染 css 标准中已内置一些预定义效果的函数,也可通过url使用SVG滤镜 语法 /* URL to SVG fil ...
- MUI获取文本框的值
MUI事件绑定注意父节点.子节点(也可以是标签选择器) js部分 html部分
- linux命令之文本查看
vi掌握练习: 英文文档,相同的单词复制粘贴光标移动编辑等操作: cat:显示文件所有内容,小文件查看时使用. 缺点:文件大时不方便查看,文件很大时,会抢占系统资源,会出现命令崩溃. [zyj@loc ...
- AutoHotKey设置ide的光标功能键
CapsLock:: SetCapsLockState,off ;锁定为小写 CapsLock & d:: flag_C=1 ;小写d代替control键,实现跨词移动CapsLock &am ...
- 有一个无效 SelectedValue,因为它不在项目列表中
“Drp_XX”有一个无效 SelectedValue,因为它不在项目列表中 出现以上异常的原因肯定是将DrowDownList控件的SelectedValue属性赋值为一个列表中不存在的值.那么我们 ...
- MovieReview—Ghost in the Shell(攻壳机动队:笑脸男事件,个别的11人事件)
AI with Wisdom I have recently watched two films in the series of Ghost in the Shell, Smilin ...
- UVALive 3942 Remember The Word (Tire)
状态是DAG,因此方案用dp统计,dp[i] = sum(dp[i+len(x)]),x是以i开头的前缀且是单词,关键在于快速判断一个前缀是不是单词,可用Trie. 每一次转移的复杂度是O(maxle ...
- 关系代数演算So Easy
关系代数运算So Easy 关系代数是以关系为运算的一组高级运算的集合.由于定义为属性个数 相同的元组的集合,因此集合代数的操作就可以引入到关系代数中.关系代数也可以看做是一种抽象的查询语言,是对关系 ...