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 ...
随机推荐
- python代码编辑器PyCharm快捷键补充
个人觉得特别有用的: 替换:Ctrl+R 删除当前行 CTRY Y: 复制当前行:Ctrl+D ALT F7: 查找哪些地方使用了选中的方法. ALT UP: 移到上一个方法 ALT DOWN: 移到 ...
- 2 TensorFlow入门笔记之建造神经网络并将结果可视化
------------------------------------ 写在开头:此文参照莫烦python教程(墙裂推荐!!!) ---------------------------------- ...
- 用Maven构建Mahout项目实现协同过滤userCF--单机版
本文来自:http://blog.fens.me/hadoop-mahout-maven-eclipse/ 前言 基于Hadoop的项目,不管是MapReduce开发,还是Mahout的开发都是在一个 ...
- 聊天软件项目TCP升级版
//聊天软件项目TCP升级版 import java.io.*; import java.net.*; class TcpClient2 { public static void main(Strin ...
- Hadoop集群的配置的主机和IP
集群配置如下: hadoop 192.168.80.100 hadoop1 192.168.80.101 hadoop2 192.168.80.102 (注:ha ...
- c++ 模板 不能 分离编译
C++Template头文件和定义分开编译的问题 (1) // Foo.htemplate<typename T>class Foo{public:void f();}; // Foo.c ...
- python数据可视化、数据挖掘、机器学习、深度学习 常用库、IDE等
一.可视化方法 条形图 饼图 箱线图(箱型图) 气泡图 直方图 核密度估计(KDE)图 线面图 网络图 散点图 树状图 小提琴图 方形图 三维图 二.交互式工具 Ipython.Ipython not ...
- python之路 前段之html,css
一.HTML 超级文本标记语言是标准通用标记语言下的一个应用,也是一种规范,一种标准, 它通过标记符号来标记要显示的网页中的各个部分.网页文件本身是一种文本文件,通过在文本文件中添加标记符,可以告诉浏 ...
- 杭电1024Max Sum Plus Plus
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1024 题目: Problem Description Now I think you have got a ...
- JS相关方法总计
1. 锚点的使用: 简单使用: <a href="#001">跳到001</a> ...文字省略 <a name="001" id ...