CC150 Arrays and Strings 1.4 ~ 1.6 Replace Spaces, String Compression, Rotate Image

来源:互联网 发布:js将数字转换成日期 编辑:程序博客网 时间:2024/06/08 06:06

1.4

题目如下:

Write a method to replace all spaces in a string with ‘%20’.

解答如下:

/** * @author feliciafay * @note   replace space with Percentage Symbol * @tips   1. manipulate a string from the end, extra buffer will save  *         us trouble from worrying about overwriting *         2. strings are IMMUTABLE, char arrays are not. *        */public class ReplaceSpaceWithPercentageSymbol {/* * @str: the input string, including the white spaces and the extra space to hold %20.  * @trueLength: the length of the string, including the white spaces. */String replaceSpacewithPercentageSymbol (char[] str, int trueLength) {if (trueLength < 1) {return "";}int countZero = 0;for (int i = 0; i < trueLength; ++i) {if (str[i] == ' ') {++countZero;}}int newLength = trueLength + countZero * 2;str[newLength] = '\0'; //设置拷贝新字符串后的间隔符号。newLength--;for (int i = trueLength - 1; i >=0; --i) {if (str[i] == ' ') {str[newLength] = '0';str[newLength-1] = '2';str[newLength-2] = '%';newLength -=3;} else {str[newLength--] = str[i];//--newLength;}}String result = new String(str);return result;}public static void main(String[] args) {// TODO Auto-generated method stubReplaceSpaceWithPercentageSymbol replacesymbol = new ReplaceSpaceWithPercentageSymbol();char[] chararray = new char[100]; char[] chararray2 = new char[] {'a',' ',' ','b',' ',' ',' '};System.arraycopy(chararray2, 0, chararray, 0, chararray2.length);System.out.println(replacesymbol.replaceSpacewithPercentageSymbol(chararray, 7));}}


1.5

题目如下:

implement a method to perform basic string compression using the counts of repeated characters. For example, the string "aabccccaaa" would become a2b1c5a3. 


解答如下:

public class BasicStringCompression {String basicStringCompression(String input) {String result = new String();if (input.length() < 1) return result;char current = input.charAt(0);int count = 1;StringBuilder sb = new StringBuilder();for (int i = 1; i < input.length(); ++i) {if (input.charAt(i) == current) {++count;} else  {sb.append(current);sb.append(count);current = input.charAt(i);count = 1;}}sb.append(current);sb.append(count);if (sb.length() < input.length())return sb.toString();else return input;}public static void main(String[] args) {// TODO Auto-generated method stubString input = "aaaabbcdddd";BasicStringCompression bsc = new BasicStringCompression();System.out.println(bsc.basicStringCompression(input));}}



1.6

题目如下:

Given N*N matrix, rotate it by 90 degree clockwise without extra memory.

解答如下:

//implement the swap index by index.public class RotateImage90Degree {public void Rotate90Degree(int[][] matrix, int degree) {for (int layer = 0; layer < degree/2; ++layer) {int first = layer;int last = degree - layer - 1;int tmp = 0;int offset = 0;for (int i = first; i <last; ++i) { // should be very clear: "<" instead of <=offset = i - first;//toptmp = matrix[first][i];//top <- leftmatrix[first][i] = matrix[last - offset][first];//left <- bottommatrix[last - offset][first] = matrix[last][last - offset];//bottom <- rightmatrix[last][last - offset] = matrix[i][last];// right <- top;matrix[i][last] = tmp;}}return;}public void print_matrix(int[][] matrix, int degrees){for (int i = 0; i < degrees; ++i) {for (int j = 0; j < degrees; ++j) {System.out.print(matrix[i][j] + " ");}System.out.println("");}}public static void main(String[] args) {// TODO Auto-generated method stubint [][] my_matrix = {{1,2,3},{4,5,6},{7,8,9}};int degree = 3;RotateImage90Degree ri9d = new RotateImage90Degree();ri9d.print_matrix(my_matrix, degree);ri9d.Rotate90Degree(my_matrix, 3);ri9d.print_matrix(my_matrix, degree);}}




0 0
原创粉丝点击