Codewars-Java编程刷题学习4-Jaden Casing Strings

点此欢迎光临我的个人网站【一几文星球】

最近有点迷上这个刷题平台了,每天都来打卡一题,希望能一直坚持下去。

题目:

Jaden Smith, the son of Will Smith, is the star of films such as The Karate Kid (2010) and After Earth (2013). Jaden is also known for some of his philosophy that he delivers via Twitter. When writing on Twitter, he is known for almost always capitalizing every word. For simplicity, you'll have to capitalize each word, check out how contractions are expected to be in the example below.

Your task is to convert strings to how they would be written by Jaden Smith. The strings are actual quotes from Jaden Smith, but they are not capitalized in the same way he originally typed them.

Example:

Not Jaden-Cased: "How can mirrors be real if our eyes aren't real"
Jaden-Cased:     "How Can Mirrors Be Real If Our Eyes Aren't Real"

Note that the Java version expects a return value of null for an empty string or null.

[谷歌翻译]

威尔史密斯的儿子贾登史密斯是《空手道小子》(2010 年)和地球之后(2013 年)等电影的明星。 Jaden 也因其通过 Twitter 传达的一些哲学而闻名。 在 Twitter 上写作时,他以几乎总是大写每个单词而闻名。 为简单起见,您必须将每个单词大写,请查看以下示例中的收缩方式。

您的任务是将字符串转换为 Jaden Smith 编写的字符串。 这些字符串是来自 Jaden Smith 的实际引号,但它们的大写方式与他最初键入的方式不同。

请注意,Java 版本期望为空字符串或 null 返回 null 值。

(这是硬加情景的题吧,感觉有点安利和打广告的嫌疑,特别是题目后面还有Twitter地址链接。)

解:

测试提交的时候有点小插曲,因为是手打的,且没有自动补全和警告提示,toUpperCase方法后面忘记写括号,然后就报错了,看来过度依赖编辑器也不太好啊。

然后来看看大神们的方案:

高赞第一的方案-

import java.lang.Character;

public class JadenCase {

  public String toJadenCase(String phrase) {
    if(phrase == null || phrase.equals("")) return null;
    
    char[] array = phrase.toCharArray();
    
    for(int x = 0; x < array.length; x++) {
      if(x == 0 || array[x-1] == ' ') {
        array[x] = Character.toUpperCase(array[x]);
      }
    }
    
    return new String(array);
  }

}

直接拆分成字符数组,第一个字符和前一个字符为‘ ’的字符转大写,很简洁明了。

高赞第二的方案-

import java.util.Arrays;
import java.util.stream.Collectors;

public class JadenCase {

  public String toJadenCase(String phrase) {
      if (null == phrase || phrase.length() == 0) {
          return null;
      }

      return Arrays.stream(phrase.split(" "))
                   .map(i -> i.substring(0, 1).toUpperCase() + i.substring(1, i.length()))
                   .collect(Collectors.joining(" "));
  }

}

使用了stream流,代码量很少,阅读性也很好。

高赞第三的方案-

import java.util.stream.Collectors;
import java.util.stream.Collector;
import java.util.Arrays;

public class JadenCase {
  public static final Collector<CharSequence,?,String> JOIN_WITH_SPACE = Collectors.joining(" ");

  public String toJadenCase(String phrase) {
    if (phraseIsEmpty(phrase)) { return null; }
    
    return Arrays.stream(splitIntoWords(phrase)).map(this::capitalize).collect(JOIN_WITH_SPACE);
  }

  private String[] splitIntoWords(String phrase) {
    return phrase.split(" ");
  }
  
  private String capitalize(String word) {
    return word.substring(0, 1).toUpperCase() + word.substring(1);
  }
  
  private boolean phraseIsEmpty(String phrase) {
    return phrase == "" || phrase == null;
  }
}

这是秀儿啊,反正我没看完,个人觉得阅读体验不太好,而且该方案下也有评论在说这点,

ho...took me sometime to get your code but why make it so complex?(ho...花了我一些时间来理解您的代码,但为什么要让它如此复杂?)

 

上一篇:Codewars-Java编程刷题学习3-Bit Counting

点此欢迎光临我的个人网站【一几文星球】

 微信公众号,欢迎关注,一起学习。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值