题目描述:

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. 针对PIL中ImageDraw.py报错的解决方案

    linux mint 13开始就发现这个问题了,一直不知道怎么解决,今天突然发现了解决方案,来分享给大家 下面是修改对比,自己根据修改,这个是系统文件,需要root权限,路径/usr/lib/pyth ...

  2. 九度OJ做题记录 更新.....

    2015年1月7日 20:34:23  题目1007:奥运排序问题 有点意思,以后想另外方法快速做出来 2015年1月7日 21:03:56 有一个技巧就是,写了三个比较函数cmp1,cmp2,cmp ...

  3. chown

    chown 命令 用途:更改与文件关联的所有者或组 chown [ -f ] [ -h ] [ -R ] Owner [ :Group ] { File ... | Directory ... } c ...

  4. Java-使用js进行编码,后台解码。

    1:使用js编码 var value=window.encodeURI(window.encodeURI(strValue)); 2:Java类中解码. String str=URLDecoder.d ...

  5. COUNT(*)与COUNT(列名)的区别(转)

    COUNT(*)与COUNT(列名)的区别       以前一直没有留意到COUNT(*)与COUNT(列名)的区别,昨天晚上无意中看到数据库系统工程师教程里面的一句话."如果null参与聚 ...

  6. 为Dapper编写一个类似于EF的Map配置类

    引言 最近在用Dapper处理Sqlite.映射模型的时候不喜欢用Attribute配置,希望用类似EF的Map来配置,所以粗略的实现了一个. 实现 首先是主体的配置辅助类型: using Syste ...

  7. [Jquery] js验证手机号

    function checkIdPhone(id,idErr){ var reg0=/^(13[0-9]|15[012356789]|18[01235,idErr6789]|14[57]|17[0]) ...

  8. 现代浏览器原生js获取id号方法

    <div id="tests" class="a b c" style="color:#f00">123</div> ...

  9. js根据id、pid把数据转为树结构

    //格式化树数据 function toTreeData(data) { var pos = {}; var tree = []; var i = 0; while (data.length != 0 ...

  10. [Firefly引擎][学习笔记一][已完结]带用户验证的聊天室

    原地址:http://bbs.9miao.com/thread-44571-1-1.html 前言:早在群里看到大鸡蛋分享他们团队的Firefly引擎,但一直没有时间去仔细看看,恰好最近需要开发一个棋 ...