江西省网站建设,腾讯企点网页版,自己建一个网站需要什么,电子商务网站建设与管理的背景题目#xff1a;383. 赎金信 由于此题只含有小写字母,并且magazine里面的字母不可重复使用. 故首先用一个长度为26的整形数组记录magazine里字母出现的次数。 再用这个整形数组跟ransomeNote进行遍历比较#xff0c;当数组中出现-1时#xff0c;说明false,否则true. 代码383. 赎金信 由于此题只含有小写字母,并且magazine里面的字母不可重复使用. 故首先用一个长度为26的整形数组记录magazine里字母出现的次数。 再用这个整形数组跟ransomeNote进行遍历比较当数组中出现-1时说明false,否则true. 代码
class Solution {public boolean canConstruct(String ransomNote, String magazine) {int[] tmp new int[26];//遍历magazine中的字符for(int i0;imagazine.length();i){char ch magazine.charAt(i);tmp[ch-a];}//遍历ransomNote中的字符for(int i0;iransomNote.length();i){char ch ransomNote.charAt(i);tmp[ch-a]--;if(tmp[ch-a]0){return false;}}return true;}
}
运行结果