zoukankan      html  css  js  c++  java
  • Java程序猿的书面采访String3

    public class SameString {
        //思想二:每个字符都相应着自己的ASC码,第一个思想的算法复杂度为O(nlogn)。一般能够利用空间来减少时间复杂度
    	//能够开辟一个大小为256的数组空间,而且将256个数组元素都置为0,然后遍历第一个字符串把字符的ASC作为数组下标。数组元素都加1,
    	//然后遍历第二个字符串将数组元素的各个值都减1,假设最后数组元素的值为0的话说明就是同样的字符串。此算法的时间复杂度为O(n)
    	public static void compare(String s1,String s2){
    		byte[] b1=s1.getBytes();
    		byte[] b2=s2.getBytes();
    		int[] bCount=new int[256];
    		
    		for(int i=0;i<256;i++){
    			bCount[i]=0;
    		}
    		for(int i=0;i<b1.length;i++){
    			bCount[b1[i]-'0']++;
    		}
    		for(int i=0;i<b2.length;i++){
    			bCount[b2[i]-'0']--;
    		}
    		for(int i=0;i<256;i++)
    			if(bCount[i]!=0){
    				System.out.println("not equal!");
    				return;
    			}
    			   System.out.println("equal!");
    			
    		
    	}
    	public static void main (String args[]){
    		String s1="aaabbbccc";
    		String s2="ababacbcc";
    		compare(s1, s2);
    		String s3="aabbccdd";
    		String s4="abc";
    		compare(s3, s4);
    		
    	}
    	
    }
    结果:equal
    not equal


    版权声明:本文博客原创文章。博客,未经同意,不得转载。

  • 相关阅读:
    Notes of the scrum meeting(12.7)
    Notes of the scrum meeting(12.5)
    事后分析报告(M1阶段)
    锁屏软件发布说明
    Android 锁屏软件MemoryDebris测试报告
    锁屏软件功能介绍
    Notes of the scrum meeting(11/4)
    Notes of the scrum meeting(11/3)
    Notes of the scrum meeting(11/2)
    SCRUM 12.17
  • 原文地址:https://www.cnblogs.com/gcczhongduan/p/4617654.html
Copyright © 2011-2022 走看看