Leetcode Valid Anagram高效解法
来源:互联网 发布:老布的淘宝店 编辑:程序博客网 时间:2024/05/17 23:25
// Author : yqtao// Date : 2016-6.19// Email :yqtao@whu.edu.cn/************************************************************************************ Given two strings s and t, write a function to determine if t is an anagram of s.* 即判断t是不是s字母交换顺序组成的字母* For example,* s = "anagram", t = "nagaram", return true.* s = "rat", t = "car", return false.** Note:* You may assume the string contains only lowercase alphabets.***********************************************************************************/#include "stdafx.h"#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;//很容易理解的方法,即排序//但是时间复杂度很高bool isAnagranm(string &s, string &t){ sort(s.begin(), s.end()); sort(t.begin(), t.end()); if (s == t) return true; else return false;}//第二种方法,使用哈希表bool isAnagranm1(string &s, string &t){ int map[26] = { 0 }; for (int i = 0; i < s.size(); i++) map[s[i] - 'a']++;//将字母存放在map的矩阵中 for (int i = 0; i < t.size(); i++) map[t[i] - 'a']--; for (int i = 0; i < sizeof(map) / sizeof(map[0]); i++) if (map[i] != 0) return false; return true;//整个map矩阵全为0,则为true}int main(){ string s = "nagaram"; string p = "nagaram"; cout << boolalpha << isAnagranm(s, p) << endl;//80ms cout << boolalpha << isAnagranm1(s, p) << endl;//12ms}
0 0
- Leetcode Valid Anagram高效解法
- 【LeetCode】 242.Valid Anagram 解法及注释
- leetcode之242. Valid Anagram(C++解法)
- leetcode之242. Valid Anagram(C++解法)
- Leetcode #242 Valid Anagram
- leetcode-242-Valid Anagram
- Leetcode: Valid Anagram
- 【leetCode】Valid Anagram
- [leetcode] 242.Valid Anagram
- leetcode 242: Valid Anagram
- 【leetcode】Valid Anagram
- [leetcode] Valid Anagram
- LeetCode Valid Anagram
- 【LeetCode】242 Valid Anagram
- [LeetCode] Valid Anagram
- leetcode: Valid Anagram
- leetCode(53):Valid Anagram
- leetcode Valid Anagram 异位体
- 自动车牌识别(ANPR)练习项目学习笔记3(基于opencv)
- 带虚框的拖拽
- 《C编程专家》:程序员面试(附2)
- javascript数据相关内容
- iOS面试题整理(下) 仔细研读受益匪浅
- Leetcode Valid Anagram高效解法
- Leetcode题解 342. Power of Four
- SQLite数据库(一) 数据库概念 语法
- ssh证书登录(实例详解)
- VS2008+ Win10 之 WinCE SDK 编译错误(1) c1xx : fatal error C1023 & (2)fatal error C1001
- 进程间通信方式
- SDUT 2125 数据结构实验之串二:字符串匹配
- 第5.2节 函数getint接受自由格式的输入,并执行转换,将输入的字符分解成整数,且每次调用得到一个整数
- 《Mongodb》