LeetCode Anagrams

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

none






struct 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;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值