leetcode之兩個數組元素相同還是不同

//思路:先掃描鍵盤輸入數據,第一行爲str1,第二行爲str2,
//當輸入數據時,調用isContain(str1,str2)方法
//for循環比較str.charAt(i)是否相等
import java.util.*;
public class Solution {
public static final void main(String[] args){
//新建一個掃描器對象
Scanner scan=new Scanner(System.in);
//掃描到鍵盤有數據輸入後,第一行爲str1,第二行爲str2
while(scan.hasNext()){
String str1=scan.nextLine();
String str2=scan.nextLine();
//調用返回值爲boolean類型的isContain(str1,str2)方法
boolean result=isContain(str1,str2);
//result爲真,輸出yes,不爲真,輸出no
if(result){
System.out.println("Yes");
}else{
System.out.println("No");
}
}
//關閉Scanner掃描器
scan.close();
}
//for循環比較str.charAt(i)值是否相等
public static boolean isContain(String str1,String str2){
for(int i=0,index=0;i<str1.length();i++){
if(str1.charAt(i)==str2.charAt(index)){
index++;
if(index==str2.length()){
return true;
}
}
}
return false;
}
}

輸入第一行:12345

輸入第二行:0

輸出:no

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章