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 ...
随机推荐
- Spark Mllib里决策树回归分析使用.rootMeanSquaredError方法计算出以RMSE来评估模型的准确率(图文详解)
不多说,直接上干货! Spark Mllib里决策树二元分类使用.areaUnderROC方法计算出以AUC来评估模型的准确率和决策树多元分类使用.precision方法以precision来评估模型 ...
- spring assert 用法
spring在提供一个强大的应用开发框架的同时也提供了很多优秀的开发工具类,合理的运用这些工具,将有助于提高开发效率.增强代码质量.下面就最常用的Assert工具类,简要介绍一下它的用法.Assert ...
- Linux环境 Java内存快速查看
最近生产环境遇到内存老是占用很大的情况,16G的内存Free的内存只剩100多M,仿佛一颗定时炸弹一般,说不定就服务Down了.于是开始网上不断的找查看内存使用的方法.现学现卖,以下通过一个例子来演示 ...
- HTTP1.1中CHUNKED编码解析(转载)
HTTP1.1中CHUNKED编码解析 一般HTTP通信时,会使用Content-Length头信息性来通知用户代理(通常意义上是浏览器)服务器发送的文档内容长度,该头信息定义于HTTP1.0协议RF ...
- MariaDB 实现主从复制
實驗目的: MariaDB為MySQL的一個分支,其完全開源.無版權之虞且操作上與 MySQL 一脈相承,實際應用中非常廣泛,軟件本身很小,安裝容易,使用簡單. 但其也有缺點,指令行方式操作,無原生G ...
- swift 第三方库迁移错误解决“Use Legacy Swift Language Version” (SWIFT_VERSION) is required to be configured correctly for targets which use Swift. Use the [Edit > Convert > To Current Swift Syntax…] menu to choo
先看看错误提示 这里Alamofire库报错,原因打开工程会Xcode会提示你覆盖到最新的3.0版本.但是仍然有些框架会出现一些问题 解决办法: 选择Pods - ReactiveCocoa - Sw ...
- js浮点数乘除法
JS在处理浮点数计算时经常会遇到精度的问题,上一篇博客封装了JS浮点数加减法的方法,这一次来封装一下js浮点数乘除法运算. 其实浮点除法的封装跟加减法的封装原理是一样,只是在第一次计算完后会再复位小数 ...
- 31全志r58平台Android4.4.2下打开USB摄像头
31全志r58平台Android4.4.2下打开USB摄像头 2018/10/26 16:00 版本:V1.0 开发板:SC5806 1.系统编译:(略) 2.需要修改的文件: W:\r58_andr ...
- PHP判断两个矩形是否相交
<?php $s = is_rect_intersect(1,2,1,2,4,5,0,3); var_dump($s); /* 如果两个矩形相交,那么矩形A B的中心点和矩形的边长是有一定关系的 ...
- netbackup如何手动获取主机ID证书。
如何手动获取主机ID证书. 文章:100039650 最后发布:2017-09-21 评分: 20 11 产品:NetBackup 问题 从NetBackup V8.1开始,管理员需要在证书颁发 ...