您的位置:首页 > 产品设计 > UI/UE

CC150 Arrays and Strings 1.1 ~ 1.3 Unique Characters, Reverse String, IsPermutation

2015-02-23 15:15 357 查看
1.1

题目如下:

Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures?

解答如下:

public class stringUniqueChar {
	// ask your interviewer if the input string is ASCII or Unicode, suppose it is ASCII.
	public boolean hasUniqueChar(String s) {
		if (s.length() > 256) return false;
		boolean [] charArray = new boolean [256]; //Boolean array, default false;
		//Arrays.fill(charArray, Boolean.FALSE); //set the value to false, not necessary here
		for (int i = 0; i < s.length(); ++i) {
			if (charArray[s.charAt(i)]) { //don't forget bracket even if one line, 
				return false;
			}
			charArray[s.charAt(i)] = true;
		}
		return true;
	}
	public static void main(String[] args) {
		stringUniqueChar suc = new stringUniqueChar();
		//String s1 = "测试一下测试"; //中文超出了ASCII string的256的范围了。
		//System.out.println(suc.hasUniqueChar(s1));
		String s1 = "a"; 
		System.out.println(suc.hasUniqueChar(s1));
		String s2 = "aab";
		System.out.println(suc.hasUniqueChar(s2));
		String s3 = "abcd";
		System.out.println(suc.hasUniqueChar(s3));
		String s4 = "{}";
		System.out.println(suc.hasUniqueChar(s4));
		String s5 = "*";
		System.out.println(suc.hasUniqueChar(s5));
		String s6 = "**";
		System.out.println(suc.hasUniqueChar(s6));
		String s7 = "b";
		System.out.println(suc.hasUniqueChar(s7));
		String s8 = "bbdb";
		System.out.println(suc.hasUniqueChar(s8));
		String s9 = " ";
		System.out.println(suc.hasUniqueChar(s9));
		String s10 = "  ";
		System.out.println(suc.hasUniqueChar(s10));
		System.out.println(3&6);
		/* bit operation
		 *  3 = 0011
		 *  6 = 0110
		 *  8 = 1000
		 * 14 = 1110 
		 *   
		 *  6 & 8 = 0
		 * 14 & 8 = 8
		 * 14 & 6 = 6
		 *  3 & 6 = 2
		*/
		
	}

}


1.2

题目如下:

Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)

解答如下:

/**
 * @note     option + mac command + J ==> FILE command
 * @author   feliciafay
 * @category reverse a sting
 */

public class ReverseString {
	String ReverseAString (String str_in) {
		StringBuilder str_out = new StringBuilder(str_in) ;
		if (str_in == null) {
			return str_out.toString();
		}
		int i = 0;
		int j = str_in.length() -1;
		char tmp = 0;
		while (i < j) {
			tmp = str_out.charAt(i);
			str_out.setCharAt(i, str_out.charAt(j));
			str_out.setCharAt(j, tmp);
			++i;
			--j;
		}
		return str_out.toString();
		}
	
		
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		String first = "jfkdjfkdw";
		ReverseString rs = new ReverseString();
		System.out.println("result = " + rs.ReverseAString(first));
	}

}

/* 区别StringBuilder与String.
 * Inside loop, StringBuilder will save time and memory, better than String
 * 
 * http://stackoverflow.com/questions/1532461/stringbuilder-vs-string-concatenation-in-tostring-in-java 
But if you are building a string e.g. inside a loop, use StringBuilder.

To clarify, assuming that hugeArray contains thousands of strings, code like this:

...
String result = "";
for (String s : hugeArray) {
    result = result + s;
}
is very time- and memory-wasteful compared with:

...
StringBuilder sb = new StringBuilder();
for (String s : hugeArray) {
    sb.append(s);
}
String result = sb.toString();

 * */


1.3

题目如下:

Given twos strings, write a method to decide if one is a permutation of the other.

解答如下:

/**
 * @author feliciafay
 * @NOTE Design an algorithm and write code to remove the duplicate characters 
 * 		 in a string without using any additional buffer. NOTE: One or two additional 
 * 		 variables are fine. An extra copy of the array is not.
 * 		 FOLLOW UP Write the test cases for this method.
 * @NOTE 
 */
import java.util.Arrays;

public class PermutationEachOther {
	
	boolean isPermutationEachOther1(String s1, String s2) {
		if (s1.length() != s2.length()) return false;
		char[] array1 = s1.toCharArray();
		char[] array2 = s2.toCharArray();
		Arrays.sort(array1);
		Arrays.sort(array2);
		System.out.println(array1);
		System.out.println(array2);
		String ss1 = new String(array1);
		String ss2 = new String(array2);
		return ss1.equals(ss2);
		//NOTE return ss1 == ss2;// is not suitable . 
		//NOTE: "==" will return false, since "==" compares the two object, not the value. 
 	}
	
	boolean isPermutationEachOther2 (String s1, String s2) {
		// sweep down the string for three times.
		if (s1.length() != s2.length()) return false;
		int[] count1 = new int[256];
		int[] count2 = new int[256];
		for (char c: s1.toCharArray()) {
			count1[c]++;
		}
		for (char c: s2.toCharArray()) {
			count2[c]++;
		}
		for (int i = 0; i < 256; ++i) {
			if (count1[i] != count2[i]) return false;
		}
		return true;
	}
	
	
	boolean isPermutationEachOther3 (String s1, String s2) {
		// improvement of the previous version : sweep down the string for only twice.
		if (s1.length() != s2.length()) return false;
		int[] count1 = new int[256];
		for (char c: s1.toCharArray()) {
			count1[c]++;
		}
		for (char c: s2.toCharArray()) {
			if(--count1[c] < 0) { //简洁的表达方式
				return false;
			}
		}
		return true;
	}
	
	
	public static void main(String[] args) {
		String s1 = "fjkdf";
		String s2 = "ffjkd";
		PermutationEachOther pe = new PermutationEachOther();
		System.out.println(pe.isPermutationEachOther1(s1, s2));
		System.out.println(pe.isPermutationEachOther2(s1, s2));
		System.out.println(pe.isPermutationEachOther3(s1, s2));
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: