LeetCode每日刷题Day1--- 771 宝石与石头
2019-04-01 15:56
363 查看
771宝石与石头
思路与结果
代码
思路1
package Day1_3_31.L771; import java.util.Scanner; public class Main1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str1 = sc.next(); String str2 = sc.next(); int n = num(str1,str2); System.out.println(n); } public static int num(String J, String S){ char[] chsJ = J.toCharArray(); char[] chsS = S.toCharArray(); int count = 0; for (int i = 0; i < chsJ.length; i++) { for (int i1 = 0; i1 < chsS.length; i1++) { if (chsJ[i] == chsS[i1]){ count++; } } } return count; } }
思路2
package Day1_3_31.L771; import java.util.HashMap; import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str1 = sc.next(); String str2 = sc.next(); int n = num(str1,str2); System.out.println(n); } public static int num(String J, String S){ char[] chsJ = J.toCharArray(); char[] chsS = S.toCharArray(); HashMap<Character, Integer> map = new HashMap<>(); for (int i = 0; i < chsS.length; i++) { if (map.containsKey(chsS[i])){ map.put( chsS[i], map.get(chsS[i]) + 1); }else{ map.put(chsS[i], 1); } } int count = 0; for (int i = 0; i < chsJ.length; i++) { if (map.containsKey(chsJ[i])){ count += map.get(chsJ[i]); } } return count; } }
相关文章推荐
- Leetcode771宝石与石头
- Leetcode刷题1-771.宝石与石头(C++)
- Leetcode 771.宝石与石头
- Leetcode771.宝石与石头(python)
- LeetCode编程771:宝石与石头
- 【LeetCode】宝石与石头
- LeetCode 刷题(python)之 宝石与石头
- LeetCode 第771题 宝石与石头/Jawels and Stones
- Leetcode 771. 宝石与石头
- Leetcode:宝石与石头
- leetcode刷题思路总结_宝石与石头
- LeetCode宝石与石头(C语言)
- 771.宝石与石头
- 【leetcode每日一题】NO257.Binary Tree Paths
- leetcode:226 Invert Binary Tree-每日编程第七题
- leetcode 每日一题 172. Factorial Trailing Zeroes
- leetcode:160 Intersection of Two Linked Lists-每日编程第二十九题
- LeetCode1000.Minimum Cost to Merge Stones(合并石头的最低成本)
- leetcode 771 Jewels and Stones
- LeetCode每日一题——231. Power of Two