码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-11-22 22:36:06      阅读:263      评论:0      收藏:0      [点我收藏+]

标签:span   lin   enum   inf   搬运   out   开始   class   import   

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

标签:span   lin   enum   inf   搬运   out   开始   class   import   

原文地址:https://www.cnblogs.com/cy708/p/10004175.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!