350. Intersection of Two Arrays II
Given two arrays, write a function to compute their intersection.
Example:
Given nums1 = [1, 2, 2, 1]
, nums2 = [2, 2]
, return [2, 2]
.
Note:
- Each element in the result should appear as many times as it shows in both arrays.
- The result can be in any order.
Follow up:
- What if the given array is already sorted? How would you optimize your algorithm?
- What if nums1's size is small compared to nums2's size? Which algorithm is better?
- What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?
class Solution {
public:
vector<int> intersect(vector<int>& nums1, vector<int>& nums2) {
map<int,int> flag;
vector<int> res;
for(auto s:nums1)
{
if(flag[s]==0) flag[s]=2;
else flag[s]++;
}
for(auto s:nums2)
{
if(flag[s]>=2)
{
flag[s]--;
res.push_back(s);
}
}
return res;
}
};
290. Word Pattern
Given a pattern
and a string str
, find if str
follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern
and a non-empty word in str
.
Examples:
- pattern =
"abba"
, str ="dog cat cat dog"
should return true. - pattern =
"abba"
, str ="dog cat cat fish"
should return false. - pattern =
"aaaa"
, str ="dog cat cat dog"
should return false. - pattern =
"abba"
, str ="dog dog dog dog"
should return false.
Notes:
You may assume pattern
contains only lowercase letters, and str
contains lowercase letters separated by a single space.
别人的代码
class Solution { public: bool wordPattern(string pattern, string str) { //使用string和map初始值的一些输入输出特性 map<char,int> pa;//存储pattern中当前字符的最新位置 map<string,int> st;//存储str中当前单词的最新位置 istringstream in(str);//使用字符串输入流,下面使用>>每次得到一个单词(不包含空字符) int n=pattern.size(); string ss;//每次输入一个单词到string中 int i=0; for(;in>>ss;i++) { if(i==n||pa[pattern[i]]!=st[ss])//初始时两个都是零 return false; pa[pattern[i]]=st[ss]=i+1;//配对改成当前匹配的最新位置,这里使用i+1,是因为默认初始值是0 } return i==n;//当前面正好循环完时,说明str全部读完,如果读取的单词个数和n一样,就是true } };
389. Find the Difference
Given two strings s and t which consist of only lowercase letters.
String t is generated by random shuffling string s and then add one more letter at a random position.
Find the letter that was added in t.
Example:
Input: s = "abcd" t = "abcde" Output: e Explanation: 'e' is the letter that was added.别人的方法:
class Solution {
public:
char findTheDifference(string s, string t) {
char r=0;
for(auto c:s) r^=c;
for(auto c:t) r^=c;
return r;
}
};
2 把所有字符相加,然后两者相减
for (int i = 0; i < 26; i++) alpha[i] = 0;
for (char c : s.toCharArray())
alpha[ c - 'a' ]++;
for (char c : t.toCharArray()) {
//could do decrement first, then check but yeah
if (--alpha[c - 'a'] < 0)
return c;
}
return 0;
409. Longest Palindrome
Given a string which consists of lowercase or uppercase letters, find the length of the longest palindromes that can be built with those letters.
This is case sensitive, for example "Aa"
is not considered a palindrome here.
Note:
Assume the length of given string will not exceed 1,010.
Example:
Input: "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7.
class Solution {
public:
int longestPalindrome(string s) {
map<char,int> flag;
bool oven=false;
int sum=0;
for(auto c:s)
{
flag[c]++;
}
for(auto c:flag)
{
if(c.second>1&&c.second%2==0) sum+=c.second;
else if(c.second>1)
{
oven=true;
sum+=c.second-1;
}
else
oven=true;
}
return oven?sum+1:sum;
}
};
第二种方法:
public int longestPalindrome(String s) {
if(s==null || s.length()==0) return 0;
HashSet<Character> hs = new HashSet<Character>();
int count = 0;
for(int i=0; i<s.length(); i++){
if(hs.contains(s.charAt(i))){
hs.remove(s.charAt(i));
count++;
}else{
hs.add(s.charAt(i));
}
}
if(!hs.isEmpty()) return count*2+1;
return count*2;
}
447. Number of Boomerangs
Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of points (i, j, k)
such that the distance between i
and j
equals the distance between i
and k
(the order of the tuple matters).
Find the number of boomerangs. You may assume that n will be at most 500 and coordinates of points are all in the range [-10000, 10000] (inclusive).
Example:
Input: [[0,0],[1,0],[2,0]] Output: 2 Explanation: The two boomerangs are [[1,0],[0,0],[2,0]] and [[1,0],[2,0],[0,0]]