华为机试-HJ4 字符串分隔

157 阅读1分钟

题目

image.png

www.nowcoder.com/practice/d9…

我的题解

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int loop = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < str.length(); i++) {
            if (loop == 8) {
                System.out.println(sb);
                sb = new StringBuilder();
                loop = 0;
            }

            if (str.charAt(i) != ' ') {
                sb.append(str.charAt(i));
                loop++;
            }
        }
        if (sb.length() < 8) {
            for (int i = sb.length(); i < 8; i++) {
                sb.append(0);
            }
        }
        System.out.println(sb);
    }
}

image.png

思路:

1、 获取处理的字符串

Scanner sc = new Scanner(System.in);
String str = sc.nextLine();

2、处理每个元素所以先把整体循环写出来

for (int i = 0; i < str.length(); i++) {
}

3、拼接子子字符串功能先加上、注意空格不拼接

StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
    if (str.charAt(i) != ' ') {
        sb.append(str.charAt(i));
    }
}

4、把8个字符作为一个子串的需求加上

int loop = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
    if (loop == 8) {
        System.out.println(sb);
        sb = new StringBuilder();
        loop = 0;
    }
    if (str.charAt(i) != ' ') {
        sb.append(str.charAt(i));
        loop++;
    }
}

5、不够8个字符后面补0的需求加上

int loop = 0;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
    if (loop == 8) {
        System.out.println(sb);
        sb = new StringBuilder();
        loop = 0;
    }
    if (str.charAt(i) != ' ') {
        sb.append(str.charAt(i));
        loop++;
    }
}
if (sb.length() < 8) {
    for (int i = sb.length(); i < 8; i++) {
        sb.append(0);
    }
}
System.out.println(sb);

总结:

1、算法都是基础思路的拼接

2、感受一下ScannerBufferedReader 性能差距; 39ms-> 13ms

    BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
    String str = bf.readLine();
    
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();

除了获取String方式不同、其余代码保持一致 image.png

image.png

高效题解

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            int len = str.length();
            int start = 0;
            while (len >= 8) {
                System.out.println(str.substring(start, start + 8));
                start += 8;
                len -= 8;
            }
            if (len > 0) {
                char[] tmp = new char[8];
                for (int i = 0; i < 8; i++) {
                    tmp[i] = '0';
                }
                for (int i = 0; start < str.length(); i++) {
                    tmp[i] = str.charAt(start++);
                }
                System.out.println(String.valueOf(tmp));
            }
        }
    }
}

image.png

总结;

1、while ((str = br.readLine()) != null) 判断!=null 可以理解、虽然控制台也无法输入null; 只需要读一次、却用while循环有点不理解

2、他这里用的是截取字符串的思路

一个记录起始坐标、一个记录剩余长度、这是长度超过8的处理方式

int len = str.length();
int start = 0;
while (len >= 8) {
    System.out.println(str.substring(start, start + 8));
    start += 8;
    len -= 8;
}

长度不到8需要补0的特殊处理

if (len > 0) {
    char[] tmp = new char[8];
    for (int i = 0; i < 8; i++) {
        tmp[i] = '0';
    }
    for (int i = 0; start < str.length(); i++) {
        tmp[i] = str.charAt(start++);
    }
    System.out.println(String.valueOf(tmp));
}

3、最后长度不够8的补0可以用如下两种方式

3.1、先用0占位、然后用存在的数据把0替换掉、

if (len > 0) {
    char[] tmp = new char[8];
    for (int i = 0; i < 8; i++) {
        tmp[i] = '0';
    }
    for (int i = 0; start < str.length(); i++) {
        tmp[i] = str.charAt(start++);
    }
    System.out.println(String.valueOf(tmp));
}

3.2、在存在的数据后面直接补0

if (len > 0) {
    StringBuilder sb = new StringBuilder(str.substring(str.length() - len));
    for (int i = len; i < 8; i++) {
        sb.append(0);
    }
    System.out.println(sb);
}

4、虽然知道截取后会存在不够8个情况、这里并不在乎他们的联系、而是直接分为2种情况来处理、这个思路要熟悉

while (len >= 8) {
}
if (len > 0) {
}

高效题解-改进版

import java.util.*;
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str;
        while ((str = br.readLine()) != null) {
            int len = str.length();
            int start = 0;
            while (len >= 8) {
                System.out.println(str.substring(start, start + 8));
                start += 8;
                len -= 8;
            }
            if (len > 0) {
                StringBuilder sb = new StringBuilder(str.substring(start));
                for (int i = sb.length(); i < 8; i++) {
                    sb.append(0);
                }
                System.out.println(sb);
            }
        }
    }
}

image.png