1. 程式人生 > >【博客搬家舊文】leetcode 771. Jewels and Stones

【博客搬家舊文】leetcode 771. Jewels and Stones

inf nbsp 嘻嘻 用戶輸入 pan spa 重新 ring expec

  今天開通了博客園 ,之前的博客就不用了。之後再陸陸續續把之前的博文重新放到這裏來。有標題這個tag的都是搬運的舊博客的文章。希望在這裏是個新的開始,嘻嘻。

  技術分享圖片

import java.util.Scanner;
 
class Solution {
    public static int numJewelsInStones(String J, String S) {
       int count=0;
         for(int i=0; i<J.length(); i++){
             for(int j=0; j<S.length(); j++){
                 
if(J.charAt(i) == S.charAt(j)) count++; } } return count; } } public static void main(String[] args){ Scanner in =new Scanner(System.in); String J=in.nextLine(); String S=in.nextLine();
int count=numJewelsInStones(J, S); System.out.println(count); } }

  以上是看到題目第一反應的做法,在eclipse上運行沒毛病,放到leetcode上之後報了一個編譯錯誤:

  Line 16: error: class, interface, or enum expected

  後來把程序改成不讓用戶輸入之後就過了,第一次刷leetcode還不太清楚為啥,先放在這裏以後再來看看。

class Solution {
    public static int numJewelsInStones(String J, String S) {
      
int count=0; for(int i=0; i<J.length(); i++){ for(int j=0; j<S.length(); j++){ if(J.charAt(i) == S.charAt(j)) count++; } } return count; } public static void main(String[] args){ String J="aA"; String S="aaAbbb"; int count=numJewelsInStones(J, S); System.out.println(count); } }

【博客搬家舊文】leetcode 771. Jewels and Stones