[codewars][Java] 翻译摩斯码

本Kata要求实现一个简单的摩斯电码解码器。摩斯电码尽管已被现代通信方式取代,但仍应用于某些场景。电码将每个字符表示为点和划的序列,如 'A' 编码为 '.-', 'B' 编码为 '-...', '1' 编码为 '.----'。电码不区分大小写,通常使用大写字母。字符间用单个空格分隔,单词间用3个空格分隔。你的任务是编写一个函数,输入摩斯电码,返回可读字符串,如 '.- ...- .-- - .. .-' 解码为 'SOS'。" 118371265,10641549,Java面试实战:时间戳转换与核心技术梳理,"['Java', '面试经验', '数据库', 'JVM内存', '网络协议', '算法']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

In this kata you have to write a simple Morse code decoder. While the Morse code is now mostly superceded by voice and digital data communication channels, it still has its use in some applications around the world.

The Morse code encodes every character as a sequence of "dots" and "dashes". For example, the letter A is coded as ·−, letter Q is coded as −−·−, and digit 1 is coded as ·−−−−. The Morse code is case-insensitive, traditionally capital letters are used. When the message is written in Morse code, a single space is used to separate the character codes and 3 spaces are used to separate words. For example, the message HEY JUDE in Morse code is ···· · −·−−   ·−−− ··− −·· ·.

NOTE: Extra spaces before or after the code have no meaning and should be ignored.

In addition to letters, digits and some punctuation, there are some special service codes, the most notorious of those is the international distress signal SOS (that was first issued by Titanic), that is coded as ···−−−···. These special codes are treated as single special characters, and usually are transmitted as separate words.

Your task is to implement a function that would take the morse code as input and return a decoded human-readable string.

For example:

MorseCodeDecoder.decode(".... . -.--   .--- ..- -.. .")
//should return "HEY JUDE"

我的code:

import java.util.ArrayList;

public class MorseCodeDecoder {
    public static String decode(String morseCode) {
        // your brilliant code here, remember that you can access the preloaded Morse code table through MorseCode.get(code)
    	 String[] list = morseCode.split(" ");   //用空格分隔,并存入一个数组中
		 ArrayList<String> arr = new ArrayList<>();
		 int space = 0;
         int count =0;
		 String s = "";

		for(int i =0 ; i<list.length ; i++)
		 {
			 arr.add(MorseCode.get(list[i]));
		 }
		for(int k=0; k<arr.size(); k++)
		{
			
			if(arr.get(k) == null)
			{
				space ++;
				
			}
			else    //第k个元素不等于null
			{
				if(space ==0)
				{
					s += arr.get(k);
					count++;
				}
				else
				{
					if(count ==0)    //前面的元素为空
					{
						s += arr.get(k);
						space = 0;
						count ++;
					}
					else
					{
						s += " "+arr.get(k);
						space = 0;	
					}
				}
						
			}
			
		}
		
		 return s;
	 }

}


分几种情况进行讨论:
1. 链表中元素 = null
   1> 将 space ++;, 并且不做任何输出
2. 链表中元素非空
   1> space = 0, 说明当前元素之前没有null元素,可以直接输出;如 [E,null,null,E]
      s = s+ arr.get(k);
   2> space >0, 说明当前元素前为null, 此时需要判断null元素是出现在链表开头,如: 
      [null,null,E,null,null,E]; 还是中间位置,如: [E,null,null,E]
      ~ 如果null出现在开头,如[null,null,E,null,null,E],则标志变量count=0;
      ~ 如果null出现在中间,如[E,null,null,E], 则标志变量count =1;

Clever code:

public class MorseCodeDecoder {
    public static String decode(String morseCode) {
      String result = "";
      for(String word : morseCode.trim().split("   ")) {
        for(String letter : word.split("\\s+")) {
          result += MorseCode.get(letter);
        }
        result += ' ';
      }
      return result.trim();
    }
}

// .trim() 方法用于删除字符串两端的字符
// "\\s+" 为正则表达式,"\"为转义字符,"\s+" 代表多个空白字符,可以是空格,回车,换行,换页,制表符等。这个正则表达式经常用到

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值