解题思路:

这道题需要注意的是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的更多相关文章

  1. 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, ...

  2. leetcode@ [49] Group Anagrams (Hashtable)

    https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For exam ...

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

  4. LeetCode:Word Ladder I II

    其他LeetCode题目欢迎访问:LeetCode结题报告索引 LeetCode:Word Ladder Given two words (start and end), and a dictiona ...

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

  6. LeetCode 刷题顺序表

    Id Question Difficulty Frequency Data Structures Algorithms 1 Two Sum 2 5 array + set sort + two poi ...

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

  8. 求和问题总结(leetcode 2Sum, 3Sum, 4Sum, K Sum)

    转自  http://tech-wonderland.net/blog/summary-of-ksum-problems.html 前言: 做过leetcode的人都知道, 里面有2sum, 3sum ...

  9. leetcode面试准备: CountPrimes

    1 题目 Description: Count the number of prime numbers less than a non-negative number, n. 接口:public in ...

  10. leetcode面试准备:Contains Duplicate I && II

    1 题目 Contains Duplicate I Given an array of integers, find if the array contains any duplicates. You ...

随机推荐

  1. 自定义view(14)使用Path绘制复杂图形

    灵活使用 Path ,可以画出复杂图形,就像美术生在画板上画复杂图形一样.程序员也可以用代码实现. 1.样板图片 这个是个温度计,它是静态的,温度值是动态变化的,所以要自定义个view.动态显示值,温 ...

  2. android模拟器创建时的PANIC: Could not open:错误的解决

    创建AVD之后,在启动时报如下错误,解决方法如下: 在环境变量中创建ANDROID_SDK_HOME=D:\Program Files (x86)\Android\android-sdk,后面的当然是 ...

  3. Jquery使用ajax参数详解

    记录一下  Jquery使用ajax(post.get及参数详解) 1.get: $.ajax({ type: "GET", url: baseUrl + "Showco ...

  4. 字符串和byte数组的相互转化

    关于byte[]数组转十六进制字符串: public static String getHexString(byte[] b) throws Exception { String result = & ...

  5. 一、单例模式(Singleton)

    单例模式最初的定义出现于<设计模式>(艾迪生维斯理, 1994):“保证一个类仅有一个实例,并提供一个访问它的全局访问点.” 特点:一是某个类只能有一个实例: 二是它必须自行创建这个实例: ...

  6. PHP 获取JSON json_decode返回NULL解决办法

    在用json_decode对JSON格式的字符串进行解码时竟然为空,页面空白啊,整半天检查这里检查那里,问同事都没用. 今天必应搜索了下,问题解决了,原来是有BOM头输出,大虾的解决办法如下: 1). ...

  7. base-command

    命令分类 自带命令 工具型 文件系统 第三方命令 CLI GUI 操作文件系统 pwd(print working directory) ls(list files) ls 列出当前目录文件 ls 目 ...

  8. SharpSvn操作 -- 获取Commit节点列表

    /// <summary> /// 获取工作目录的所有节点,包括子目录 /// </summary> /// <param name="workingCopyD ...

  9. 使用com.sun.imageio.plugins.png.PNGMetadata读取图片的元数据

    所谓图片元数据,就是除了我们肉眼看到的图片内容外,隐藏在这些内容背后的一些技术数据. 本文介绍如何使用Java代码将一张图片的隐藏信息读取出来. 首先不需要下载任何额外的Java库,用JDK自带的库就 ...

  10. 多目标检测分类 RCNN到Mask R-CNN

    最近做目标检测需要用到Mask R-CNN,之前研究过CNN,R-CNN:通过论文的阅读以及下边三篇博客大概弄懂了Mask R-CNN神经网络.想要改进还得努力啊... 目标检测的经典网络结构,顺序大 ...