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

  今天开通了博客园 ,之前的博客就不用了。之后再陆陆续续把之前的博文重新放到这里来。有标题这个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);
    }
}

猜你喜欢

转载自www.cnblogs.com/cy708/p/10004175.html