题目描述:

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. Linux安装oracle 10g常见问题之——ORA-01078,LRM-00109,ORA-01102

    [oracle@toughhou database]$ sqlplus /nolog SQL> conn / as sysdba SQL> startup ORA-01078: failu ...

  2. 如何应用CLR线程池来管理多线程

        class Program     {         static void Main(string[] args)         {             int intWorkerT ...

  3. 监控SQL Server的job执行情况

    在服务器没有设置发邮件并且不允许发邮件的情况下, 可以通过下列语句来检查SQL Server 的job的执行情况 select top 150 a.run_date,a.run_time, b.nam ...

  4. Mvc设计模型与三层架构

    Mvc(Model-View-Controller):是软件架构的一中设计模式,对软件进行分割成3个层次:视图.模型.控制. 实现对软件的一种动态的设计,并且容易对软件进行扩展.后期的修改,使某些程序 ...

  5. $.post()返回数据正常,但不执行success回调函数

    今天遇到一特郁闷的问题,如题:$.post()返回数据正常,但不执行success回调函数.说它郁闷是因为没毕业之前就遇到过解决了,却没有记录下来,导致卡了一下午. 像这样,post返回数据正常,但却 ...

  6. @Entity设置实体lazy = false

    问题描述 在通过Hibernate查询Bean信息时报以下异常信息: org.hibernate.LazyInitializationException: could not initialize p ...

  7. DX 的.x 文件

    template Header { <3D82AB43-62DA-11cf-AB39-0020AF71E433> WORD major; WORD minor; DWORD flags;} ...

  8. 《Publish or Perish》——从某种角度来说,我们也算和世界同步了呢。

  9. hdu 1087

    动规  d[i]记录以第 i 个数结尾的最大值 #include <cstdio> #include <algorithm> #include <cstring> ...

  10. hdu 1851 A Simple Game 博弈论

    简单博弈问题(巴什博弈-Bash Game) 巴什博弈:只有一堆n个物品,两个人轮流从这对物品中取物,规定每次至少取一个,最多取m个,最后取光着得胜. 很容易想到当n%(m+1)!=0时,先取者必胜, ...