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
- CC150 Arrays and Strings 1.4 ~ 1.6 Replace Spaces, String Compression, Rotate Image
- CC150 1.6 String Compression
- CC150 Reading Notes 1: Arrays and Strings
- [CC150] Chapter 1 Arrays and Strings
- CC150 chapter 1 Arrays and strings.
- CC150 Arrays and Strings 1.1 ~ 1.3 Unique Characters, Reverse String, IsPermutation
- CC150 Arrays and Strings 1.7 ~ 1.8 Set Matrix Zero, IsS1RotationOfS2
- Image Compression and Coding
- Chapter 1 Arrays and Strings - 1.6
- Chapter 1 Arrays and Strings - 1.4
- Arrays and Strings----1
- Arrays and Strings----3
- Arrays and Strings----4
- Arrays and Strings----7
- Image and Video Compression Techniques
- Chapter 1 Arrays and Strings
- 3-Strings, Vectors, and Arrays
- 【5】Replace all spaces in a string with ‘%20’
- 数学(hdu5175Misaki's Kiss again)
- 浅谈三层架构(2)
- 【主机规划与磁盘分区】 磁盘分区 MBR
- C# 委托 事件
- 浅谈三层架构(1)
- CC150 Arrays and Strings 1.4 ~ 1.6 Replace Spaces, String Compression, Rotate Image
- 数据库连接-ADO.NET
- VB.NET & Visual Basic
- zookeeper常用命令
- 设计模式总结-结构型模式
- 创建型模式-工厂三姐妹
- 设计模式总结-行为模式
- 02langfanghanfeng
- 菜鸟练习PAT(三)