自己写的程序(时间:20ms):
int numJewelsInStones(String J, String S) { byte[] byteJ = J.getBytes(); byte[] byteS = S.getBytes(); int result = 0; for(byte s:byteS){ for(byte j:byteJ){ if(s==j){ result++; } } } return result; }
别人11ms的程序(我用这个提交是25ms):
int numJewelsInStones(String J, String S) { int result = 0; HashSet<Character> setJ = new HashSet<>(); for(int i=0;i<J.length();i++){ setJ.add(J.charAt(i)); } for(int i=0;i<S.length();i++){ if(setJ.contains(S.charAt(i))){ result++; } } return result; }