zoukankan      html  css  js  c++  java
  • 不同单词个数统计

    import java.util.*;
    public class Main 
    {
    	 public int theNum(String str) 
    	 {
    		 //sum统计有多少个不同的数//arr保存单词//bol某位置判断是不是有重复的单词1为重复单词
    	    int sum = 0;
    		String []arr = str.split(" |, |\. |\.");
    		int []bol = new int [arr.length];
    		//从第一个数开始遍历
    		for(int i = 0;i < arr.length;i++)
    		{
    			//用temp1保存 第i个单词
    			String temp1 = arr[i];
    			for(int x = i+1 ; x<arr.length;x++ ) 
    			{
    				//用temp2保存第x个单词
    				String temp2 = arr[x];
    				//如果被访问了的单词为重复单词 跳出判断循环
    				if(bol[i] == 1) 
    				{
    					break;
    				}
    				else 
    				{
    					//如果两个单词长度不同 证明是不同的单词 继续访问下一个单词
    					if(temp1.length()!=temp2.length()) 
    					{
    					}
    					else 
    					{
                            //标记两个单词是否一样 一样的话 将在bol进行标记
    						int flag = 0;
    						for(int y = 0; y<temp1.length();y++) 
    						{	
    							if(temp1.charAt(y)!=temp2.charAt(y)) 
    							{	
    								flag = 1;
    								break;
    							}//==
    						}
    						if(flag==0)
    						bol[x]=1;
    					}
    				}
    			}
    		}
    		for(int i=0;i<arr.length;i++) 
    		{
    		if(bol[i] ==0)
    			{
    				sum ++;	
    			}
    		}
    		 return sum;
    	 }
    	//测试代码
    	public static void main(String[] args) 
    	{
    		Scanner sc = new Scanner(System.in);
    		Main m = new Main();
    		String str = sc.nextLine();
    		System.out.println(m.theNum(str));
    	}
    }
    
  • 相关阅读:
    leetcode Remove Linked List Elements
    leetcode Word Pattern
    leetcode Isomorphic Strings
    leetcode Valid Parentheses
    leetcode Remove Nth Node From End of List
    leetcode Contains Duplicate II
    leetcode Rectangle Area
    leetcode Length of Last Word
    leetcode Valid Sudoku
    leetcode Reverse Bits
  • 原文地址:https://www.cnblogs.com/cznczai/p/11150630.html
Copyright © 2011-2022 走看看