【leetcode】Valid Anagram
来源:互联网 发布:centos7修改ssh端口号 编辑:程序博客网 时间:2024/05/17 21:40
Question
Given two strings s and t, write a function to determine if t is an anagram of 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.
Code
#include<iostream>#include<string.h>#include<algorithm>using namespace std;class Solution {public: bool isAnagram(string s, string t) { sort(s.begin(), s.end()); sort(t.begin(), t.end()); if (s == t) return true; else return false; }};int main() { Solution so; string s = "rat"; string t = "car"; cout << so.isAnagram(s, t) << endl; system("pause"); return 0;}
点评
在leetcode上看到的非常简单机智的做法,透过现象看本质,本质就是判断字符串中字符是否一致,通过sort将两个字符顺序变为一致即可。
0 0
- 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 异位体
- [leetcode 242]Valid Anagram
- leetcode Valid Anagram
- leetCode #242 Valid Anagram
- LeetCode题解:Valid Anagram
- 记一个复制黏贴的功能想法(黏贴剪切板中的数字自增,复制黏贴自增)
- PullrefreshLibrary(PullToRefreshGridView)上拉刷新,下拉加载+异步AsyncTask(二)
- autoware自动注入失败
- 你真的会用Retrofit2吗?Retrofit2完全教程
- 基于pinctrl 和 devcice tree的SOC GPIO 使用
- 【leetcode】Valid Anagram
- 查找单链表的中间节点,要求只能遍历一次链表
- LaTeX模板:实验报告封面样式
- linux 常用命令笔记
- http accept-encoding详解 HTTP协议-压缩(gzip,deflate)
- 一句话(非javascript)在页面中加入自动跳转
- Android开发之自定义圆形的ImageView的实现
- 在VS中如何把exe关联到DLL工程中
- HTML5的这些api你知道吗?