码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 242. Valid Anagram

时间:2018-09-15 23:19:11      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:class   tail   字符   cto   net   leetcode   val   ||   span   

这个题只存储26个字母的,之前用的256个字符,所以可以直接用s[i]这种作为坐标,但现在只存储在26个中,坐标值是0到25,必须减去‘a‘才行,不减的话可能是100多的assic码

class Solution {
public:
    bool isAnagram(string s, string t) {
        int length1 = s.size();
        int length2 = t.size();
        if(length1 <= 0 || length2 <= 0)
            return true;
        vector<int> res(26,0);
        for(int i = 0;i < length1;i++)
            res[s[i] - a] += 1;
        for(int j = 0;j < length2;j++)
            res[t[j] - a] -= 1;
        for(int i =0;i < 26;i++){
            if(res[i] != 0)
                return false;
        }
        return true;
    }
};

https://blog.csdn.net/fly_yr/article/details/49886391

 

leetcode 242. Valid Anagram

标签:class   tail   字符   cto   net   leetcode   val   ||   span   

原文地址:https://www.cnblogs.com/ymjyqsx/p/9652683.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!