My Twenty-Fifth Page - 剑指offer 05.替换空格 - By Nicolas

这篇page主要是针对leetcode上的剑指offer 95.替换空格所写的。小尼先简单的给各位介绍一下这道题的题意,就是实现一个函数,把字符串s中的每一个空格替换成"%20"。

其实这道题就是考研对字符串的调用的熟练程度,小尼给出的思路就是我们创建一个全新的char类型的数组(定义的长度可以就是我们给出的字符串的长度的三倍),然后我们遍历我们给出的字符串,然后我们如果遍历到的就是不同的字母,那么我们就直接将遍历的元素放入我们创建的数组中,如果我们遍历到的是空格,那么我们就在数组中加入三个元素,就是题目给我们规定的那三个符号。接下来,小尼拉一下代码:

int num = s.length();
char[] result = new char[3*num];
char ch;
int k = 0;
for(int i = 0;i<num;i++){
    ch = s.charAt(i);
    if(c == ' '){
        a[k++] = '%';
        a[k++] = '2';
        a[k++] = '0';
    }
    else{
        a[k++] = ch;
    }
}
String str = new String(result,0,k);
return str;

这个写法还是比较简洁简单的,小伙伴们可以参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive 中可以使用 UDF(User-Defined Functions)来实现将数字转化为英文格式的功能。以下是一个示例 UDF 的代码实现: ```java import org.apache.hadoop.hive.ql.exec.UDF; import java.text.DecimalFormat; public class NumberToEnglish extends UDF { private static final String[] tensNames = {"", " ten", " twenty", " thirty", " forty", " fifty", " sixty", " seventy", " eighty", " ninety"}; private static final String[] numNames = {"", " one", " two", " three", " four", " five", " six", " seven", " eight", " nine", " ten", " eleven", " twelve", " thirteen", " fourteen", " fifteen", " sixteen", " seventeen", " eighteen", " nineteen"}; private static final String[] bigNames = {"", " thousand", " million", " billion"}; public String evaluate(Double num) { if (num == 0) { return "zero"; } DecimalFormat decimalFormat = new DecimalFormat("#.00"); String formatted = decimalFormat.format(num); String[] parts = formatted.split("\\."); long dollars = Long.parseLong(parts[0]); int cents = Integer.parseInt(parts[1]); String dollarsInEnglish = convert(dollars); String centsInEnglish = convert(cents); String result = dollarsInEnglish + " dollars"; if (cents != 0) { result += " and " + centsInEnglish + " cents"; } return result.trim(); } private static String convert(long num) { if (num == 0) { return "zero"; } String prefix = ""; if (num < 0) { num = -num; prefix = "negative"; } String current = ""; int place = 0; do { long n = num % 1000; if (n != 0) { String s = convertLessThanOneThousand((int) n); current = s + bigNames[place] + current; } place++; num /= 1000; } while (num > 0); return (prefix + current).trim(); } private static String convertLessThanOneThousand(int num) { String current; if (num % 100 < 20) { current = numNames[num % 100]; num /= 100; } else { current = numNames[num % 10]; num /= 10; current = tensNames[num % 10] + current; num /= 10; } if (num == 0) { return current; } return numNames[num] + " hundred" + current; } } ``` 这个 UDF 接受一个 Double 类型的参数,然后将其转化为英文格式的字符串。在这个 UDF 中,我们使用了一个叫做 `convert()` 的函数来将数值转化为英文形式。这个函数将数值按照千位进行分组,然后对每一组的数值进行转化,最后将所有组的结果拼接起来。 例如,要将 USD24,217.45 转化为英文格式,可以使用如下的 HiveQL 语句: ```sql SELECT NumberToEnglish(24217.45); ``` 上面的语句将返回字符串 "twenty-four thousand two hundred seventeen dollars and forty-five cents"。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值