Anagrams
Given an array of strings, return all groups of strings that are anagrams.
Note: All inputs will be in lower-case.
善用STL,好处有两个:简单,可靠!
介绍两个API
string::compare
<string>
int compare ( const string& str ) const; int compare ( const char* s ) const; int compare ( size_t pos1, size_t n1, const string& str ) const; int compare ( size_t pos1, size_t n1, const char* s) const; int compare ( size_t pos1, size_t n1, const string& str, size_t pos2, size_t n2 ) const; int compare ( size_t pos1, size_t n1, const char* s, size_t n2) const;
Compare strings
Compares the content of this object (or a substring of it, known as
compared (sub)string) to the content of a
comparing string, which is formed according to the arguments passed.
The member function returns 0 if all the characters in the compared contents compare equal, a negative value if the first character that does not match compares to less in the object than in the comparing string, and a positive value in the opposite case.
Notice that for string objects, the result of a character comparison depends only on its character code (i.e., its ASCII code), so the result has some limited alphabetical or numerical ordering meaning.
For other basic_string class instantiations, the comparison depends on the specific traits::compare function, where traits is one of the class template parameters.
Parameters
-
str
- string object with the content to be used as comparing string. s
-
Array with a sequence of characters to be used as
comparing string.
Except for the last member version, this is a null-terminated character sequence whose length is determined by the first occurrence of a null-character.
In the last member version, the length is not determined by any occurrence of null-characters but by parameter n2.
pos1
- Position of the beginning of the compared substring, i.e. the first character in the object (in *this) to be compared against the comparing string. n1
- Length of the compared substring. pos2
- Position of a character in object str which is the beginning of the comparing string. n2
- Length in characters of the comparing string.
Return Value
0 if the compared characters sequences are equal, otherwise a number different from 0 is returned, with its sign indicating whether the object is considered greater than the comparing string passed as parameter (positive sign), or smaller (negative sign).If either pos1 or pos2 is specified with a position greater than the size of the corresponding string object, an exception of type out_of_range is thrown.
sort
<algorithm>
template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last ); template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
Sort elements in range
Sorts the elements in the range
[first,last) into ascending order.
The elements are compared using operator< for the first version, and comp for the second.
Elements that would compare equal to each other are not guaranteed to keep their original relative order.
Parameters
-
first, last
- Random-Access iterators to the initial and final positions of the sequence to be sorted. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last. comp
- Comparison function object that, taking two values of the same type than those contained in the range, returns true if the first argument goes before the second argument in the specific strict weak ordering it defines, and false otherwise.
Return value
nonestruct Node
{
string key;
int val;
Node(string key_in, int val_in):key(key_in), val(val_in)
{
}
};
bool comp(const Node& n1, const Node& n2)
{
return n1.key.compare(n2.key) < 0;
}
class Solution {
public:
vector<string> anagrams(vector<string> &strs) {
vector<string> ret;
if(strs.size() == 0)
return ret;
vector<Node> nodes;
for(int i = 0; i < strs.size(); ++i)
{
string temp = strs[i];
sort(temp.begin(), temp.end());
nodes.push_back(Node(temp, i));
}
sort(nodes.begin(), nodes.end(), comp);
int old = 0;
for(int i = 0; i < (int)nodes.size(); ++i)
{
if(i - 1 >= 0 && 0 == nodes[i - 1].key.compare(nodes[i].key))
ret.push_back(strs[nodes[i].val]);
else if(i + 1 < (int)nodes.size() && 0 == nodes[i + 1].key.compare(nodes[i].key))
ret.push_back(strs[nodes[i].val]);
}
return ret;
}
};