字节面试题-阿拉伯数字转中文

题目描述

✅ 字节面试题-阿拉伯数字转中文

image-20250613194325729

image-20250613194354371

image-20250613194413734

思路分析

解法一:四位分节处理(推荐)

核心思路

  • 将数字按 万、亿、兆 四位一节拆分。
  • 每节用 千/百/十/个 单位转换,并在需要时补“零”。
  • 组合各节并处理前导“一十”简化为“”。

复杂度分析

  • 时间复杂度:O(log10 n),其中 n 表示数值大小。
  • 空间复杂度:O(1)。
class Solution {
    private static final String[] DIGITS = {"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"};
    private static final String[] UNITS = {"", "十", "百", "千"};
    private static final String[] BIG_UNITS = {"", "万", "亿", "兆"};

    public String numberToChinese(long num) {
        if (num == 0) {
            return "零";
        }

        StringBuilder res = new StringBuilder();
        int unitIdx = 0;
        boolean needZero = false;

        while (num > 0) {
            int section = (int) (num % 10000);
            if (section == 0) {
                if (res.length() > 0) {
                    needZero = true;
                }
            } else {
                String part = sectionToChinese(section);
                if (needZero) {
                    res.insert(0, "零");
                    needZero = false;
                }
                if (section < 1000 && res.length() > 0) {
                    res.insert(0, "零");
                }
                res.insert(0, part + BIG_UNITS[unitIdx]);
            }

            num /= 10000;
            unitIdx++;
        }

        String result = res.toString();
        if (result.startsWith("一十")) {
            result = result.substring(1);
        }

        return result;
    }

    private String sectionToChinese(int num) {
        StringBuilder sb = new StringBuilder();
        int[] factors = {1000, 100, 10, 1};
        boolean zero = false;

        for (int i = 0; i < 4; i++) {
            int digit = num / factors[i];
            num %= factors[i];

            if (digit == 0) {
                if (sb.length() > 0) {
                    zero = true;
                }
                continue;
            }

            if (zero) {
                sb.append("零");
                zero = false;
            }

            sb.append(DIGITS[digit]).append(UNITS[3 - i]);
        }

        return sb.toString();
    }
}
import "strings"

func numberToChinese(num int64) string {
	if num == 0 {
		return "零"
	}

	digits := []string{"零", "一", "二", "三", "四", "五", "六", "七", "八", "九"}
	units := []string{"", "十", "百", "千"}
	bigUnits := []string{"", "万", "亿", "兆"}

	res := strings.Builder{}
	unitIdx := 0
	needZero := false

	for num > 0 {
		section := int(num % 10000)
		if section == 0 {
			if res.Len() > 0 {
				needZero = true
			}
		} else {
			part := sectionToChinese(section, digits, units)
			if needZero {
				res.WriteString("零")
				needZero = false
			}
			if section < 1000 && res.Len() > 0 {
				res.WriteString("零")
			}
			res.WriteString(part + bigUnits[unitIdx])
		}

		num /= 10000
		unitIdx++
	}

	result := res.String()
	if strings.HasPrefix(result, "一十") {
		result = strings.TrimPrefix(result, "一十")
		result = "十" + result
	}

	return result
}

func sectionToChinese(num int, digits []string, units []string) string {
	factors := []int{1000, 100, 10, 1}
	sb := strings.Builder{}
	zero := false

	for i := 0; i < 4; i++ {
		digit := num / factors[i]
		num %= factors[i]

		if digit == 0 {
			if sb.Len() > 0 {
				zero = true
			}
			continue
		}

		if zero {
			sb.WriteString("零")
			zero = false
		}

		sb.WriteString(digits[digit])
		sb.WriteString(units[3-i])
	}

	return sb.String()
}

相似题目

题目 难度 考察点
273. 整数转换英文表示 困难 字符串
8. 字符串转换整数 (atoi) 中等 字符串
12. 整数转罗马数字 中等 字符串
13. 罗马数字转整数 简单 字符串
415. 字符串相加 简单 字符串
本文作者:
本文链接: https://hgnulb.github.io/blog/2025/30749474
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!