题目描述:

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.

解题思路:

将字符串进行排序,然后逐个比较是否相同。

注意将数组转为String的时候需要使用String.valueOf()方法。不能用Array自带的toString()方法,详细原因见:http://www.cnblogs.com/ningvsban/p/3955483.html

代码如下:

public class Solution {
public boolean isAnagram(String s, String t) {
char[] s1 = s.toCharArray();
char[] t1 = t.toCharArray();
Arrays.sort(s1);
Arrays.sort(t1);
return String.valueOf(s1).equals(String.valueOf(t1));
}
}

  

Java [Leetcode 242]Valid Anagram的更多相关文章

  1. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  2. 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 ...

  3. [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: ...

  4. 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 ...

  5. [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: ...

  6. 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 ...

  7. (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 ...

  8. 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 = & ...

  9. 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  ...

随机推荐

  1. 由底层和逻辑说开去--c++之引用的深入剖析

    在学c++的时候 我遇到的第一个问题就是这个引用,引用是什么东西,我的c++启蒙教科书是c++ primer plus,这本书上说的是:引用是已定义变量的别名,可以使用这个引用来表示这个变量:每当看到 ...

  2. 再次学习C++类之构造函数

    学习C++类,首先要说C中的结构体,虽然C++类扩展了C中的结构体,可以添加成员函数,但他们是有区别的.在结构体中,成员变量.成员函数都是公有的,而类中,一般是成员变量是私有的,成员函数是公有的,私有 ...

  3. 电网SVG简介

    目 录1.        范围        12.        规范性引用文件        13.        缩略语        14.        本标准涉及的图形交换特征      ...

  4. shell自定义函数

    Linux中提供了很多内置的函数,但有时我们需要根据自己的需求来创建自定义函数.下面介绍一下关于shell编程中的自定义函数. 1.函数定义 function hello(){    echo &qu ...

  5. Python socket模拟HTTP请求

    0x00 起 最近在做一个对时间要求比较高的扫描器,需要封装一下SOCKET模拟HTTP发包的一些常用函数.简单的说,就是重写一下requests中的get.post方法. 今天在写的时候,遇到一枚很 ...

  6. 类的本质、description方法、SEL、NSLog输出增强

    一.类的本质 1.类也是个对象 其实类也是一个对象,是Class类型的对象,简称“类对象” Class类型的定义 typedef struct objc_class *Class; 类名就代表着类对象 ...

  7. 分布式日志收集系统--Chukwa

    1. 安装部署 1.1 环境要求 1.使用的JDK的版本必须是1.6或者更高版本,本实例中使用的是JDK1.6 2.使用的hadoop的版本必须是Hadoop0.20.205.1及以上版本,本实例中使 ...

  8. 总有你需要的之 ios 小技巧 (下)

    图片上绘制文字 写一个UIImage的category NSMutableParagraphStyle* paragraphStyle = [[NSParagraphStyle defaultPara ...

  9. 【noi2013】【bz3244】树的计数

    题目概括:给出树的dfs.bfs序 求树的期望高度 题解:由于我比较懒 先copy一段百度文库的题解~void copy(){我们可以发现,所求的树之所以会有很多种,是因为出现了这种情况:对于A.B, ...

  10. jquery类选择器无法取得对象问题原因

    <html> <script type="text/javascript" src="jquery-1.9.1.js"></scr ...