|
| 1 | +import java.util.Arrays; |
| 2 | + |
| 3 | +public class PatternPrinting { |
| 4 | + public static void main(String[] args) { |
| 5 | + mergeTwoGivenSortedArray(); |
| 6 | + convertNumToChar(); |
| 7 | + printCharacterAndItsLength(); |
| 8 | + } |
| 9 | + |
| 10 | + private static void printCharacterAndItsLength() { |
| 11 | + String str = |
| 12 | + "abbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd"; |
| 13 | + int count = 0; |
| 14 | + for (int i = 0; i < str.length(); i++) { |
| 15 | + char Char = str.charAt(i); |
| 16 | + count++; |
| 17 | + char next = i + 1 < str.length() ? str.charAt(i + 1) : (char) -1; |
| 18 | + if (Char != next) { |
| 19 | + System.out.print(Char + "" + count); |
| 20 | + count = 0; |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + static void convertNumToChar() { |
| 26 | + String str = "a1b50c10d10e1h100"; |
| 27 | + for (int i = 0, k; i < str.length(); i = k + 1) { |
| 28 | + char ch = str.charAt(i); |
| 29 | + int count = str.charAt(++i) - 48, next = i + 1 < str.length() ? str.charAt(i + 1) - 48 : -1; |
| 30 | + k = i; |
| 31 | + while (next > -1 && next < 10 && k + 1 < str.length()) { |
| 32 | + count *= 10; |
| 33 | + count += (str.charAt(++k) - 48); |
| 34 | + if (k + 1 < str.length()) next = str.charAt(k + 1) - 48; |
| 35 | + } |
| 36 | + printCharBaseCount(ch, count); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + static void printCharBaseCount(char ch, int count) { |
| 41 | + for (int j = 0; j < count; j++) { |
| 42 | + System.out.print(ch); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void mergeTwoGivenSortedArray() { |
| 47 | + int[] array1 = {-1, 1, 10, 12, 20}, array2 = {1, 2, 10, 11}; |
| 48 | + // out: -1,1,1,2,10,10,11,12,20 |
| 49 | + int[] newArray = new int[array1.length + array2.length]; |
| 50 | + int newArrIndex = 0; |
| 51 | + for (int i = 0; i < array1.length; i++) { |
| 52 | + newArray[newArrIndex++] = array1[i]; |
| 53 | + if (i < array2.length) newArray[newArrIndex++] = array2[i]; |
| 54 | + } |
| 55 | + System.out.println(Arrays.toString(insertionSor(newArray))); |
| 56 | + } |
| 57 | + |
| 58 | + private static int[] insertionSor(int[] intArray) { |
| 59 | + for (int i = 1; i < intArray.length; i++) { |
| 60 | + int previousIndex = i - 1; |
| 61 | + int currentValue = intArray[i]; |
| 62 | + while ((previousIndex > 0) && intArray[previousIndex] > currentValue) |
| 63 | + intArray[previousIndex + 1] = intArray[previousIndex--]; |
| 64 | + intArray[previousIndex + 1] = currentValue; |
| 65 | + } |
| 66 | + return intArray; |
| 67 | + } |
| 68 | +} |
0 commit comments