LeetCode OJ:Valid Anagram(有效字谜问题)
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.
题目的意思就是是否两个字符串之间可以通过颠倒次序来实现:
class Solution {
public:
bool isAnagram(string s, string t) {
int szS = s.size();
int szT = t.size();
if (szS != szT)return false;
sort(s.begin(), s.end());
sort(t.begin(), t.end());
for(int i = ; i < szS; ++i){
if (s[i] != t[i])
return false;
}
return true;
}
};
再看一遍题目感觉上面写的有点蠢了, 其实排序完成之后直接比较两个字符串是否相等就可以了,不过这种排序之后再比较的时间复杂度为O(NlogN),可以通过计数的方尝试将复杂度降低到O(N),两种方式的java代码如下所示:
第一种:排序
public class Solution {
public boolean isAnagram(String s, String t) {
char[] sArr = s.toCharArray();
char[] tArr = t.toCharArray();
Arrays.sort(sArr);
Arrays.sort(tArr);
return String.valueOf(sArr).equals(String.valueOf(tArr));
}
}
第二种: 计数法
public class Solution {
public boolean isAnagram(String s, String t) {
char [] count = new char [26];
for(int i = 0; i < s.length(); ++i){
count[s.charAt(i)-'a']++;
}
for(int i = 0; i < t.length(); ++i){
count[t.charAt(i)-'a']--;
}
for(int i = 0; i < 26; ++i){
if(count[i] != 0)
return false;
}
return true;
}
}
计数法的runtime实际上比前面的排序方法号上很多,但是有一点吐槽一下,现在leetCode上面的runtime为什么都是java比c++要快上很多啊,java现在有这么快吗,有点不能理解啊。
LeetCode OJ:Valid Anagram(有效字谜问题)的更多相关文章
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- [leetcode]242. Valid Anagram验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- LN : leetcode 242 Valid Anagram
lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...
- LeetCode 242. Valid Anagram (验证变位词)
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- [LeetCode] 242. Valid Anagram 验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- (easy)LeetCode 242.Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- Java [Leetcode 242]Valid Anagram
题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...
- Leetcode 242. Valid Anagram(有效的变位词)
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- Leetcode 242 Valid Anagram pytyhon
题目: Given two strings s and t, write a function to determine if t is an anagram of s. For example,s ...
随机推荐
- 在Web上运行Linux—js/linux模拟器
一个叫Fabrice Bellard 的程序员写了一段Javascript在Web浏览器中启动Linux(原网页,我把这个网页iframe在了下面),目前,你只能使用Firefox 4和Chrome ...
- git 遇到的问题
1.error: RPC failed; curl transfer closed with outstanding read data remaining 解决办法: git clone https ...
- use html5 video tag with MSE for h264 live streaming
本编博客记录桌面虚拟化移动端预研. 完整demo: https://github.com/MarkRepo/wfs.js 常见的直播方案有RTMP RTSP HLS 等等, 由于这些流都需要先传输到服 ...
- java执行cmd命令和linux命令
文章出处http://blog.csdn.net/xh16319/article/details/17302947 一:window下执行cmd指定 一:window下执行cmd指定 程序例子: [j ...
- 自己写的一个简单PHP采集器
自己写的一个简单PHP采集器 <?php //**************************************************************** $url = &q ...
- 再来一道测半平面交模板题 Poj1279 Art Gallery
地址:http://poj.org/problem?id=1279 题目: Art Gallery Time Limit: 1000MS Memory Limit: 10000K Total Su ...
- hadoop21---使用代理修改List,代理流程
package cn.itcast_05_proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Met ...
- CAS单点登录的配置
先说单点登录是个啥? 单点登录主要用于多系统集成,即在多个系统中,用户只需要到一个中央服务器登录一次即可访问这些系统中的任何一个,无须多次登录. 配置的步骤如下: 1.生成安全证书 Cas serve ...
- discuz对PHP7不支持mysql的兼容性处理
PHP7 废除了 ”mysql.dll” ,推荐使用 mysqli 或者 pdo_mysql,discuz对原生mysql函数做了如下处理,通过mysqli代替原mysql函数 http://blog ...
- Oracle数据库使用总结
--1.使用月份作为条件筛选(to_char函数与extract函数使用) select * from test_date where to_char(dqsj,'mm') like '%07%'; ...