Re: [閒聊] 每日LeetCode已回收

看板Marginalman作者 (みけねこ的鼻屎)時間2年前 (2023/12/16 19:28), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串578/719 (看更多)
https://leetcode.com/problems/valid-anagram/description 242. Valid Anagram 給你兩個字串,判斷這兩個字串是不是可以透過交換字元位置相等。 思路: 1.字串長度不同不可能相同返回false,統計兩個字元的字元數量,不相等返回 false。 Java Code: ---------------------------------------------- class Solution { public boolean isAnagram(String s, String t) { if (s.length() != t.length()) { return false; } int[] count = new int[26]; for (int i = 0; i < s.length(); i++) { count[s.charAt(i) - 'a']++; count[t.charAt(i) - 'a']--; } for (int cnt : count) { if (cnt != 0) { return false; } } return true; } } ---------------------------------------------- -- https://i.imgur.com/Df746ya.jpg
-- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 122.100.73.13 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Marginalman/M.1702726130.A.50A.html
文章代碼(AID): #1bVOdoKA (Marginalman)
討論串 (同標題文章)
文章代碼(AID): #1bVOdoKA (Marginalman)