804. Unique Morse Code Words

本文介绍了一种统计不同Morse码表示的方法。通过将单词列表转换为其对应的Morse码表示形式,并利用HashSet来去除重复项,最终返回不同Morse码表示的数量。

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

International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: "a"maps to ".-""b" maps to "-...""c" maps to "-.-.", and so on.

For convenience, the full table for the 26 letters of the English alphabet is given below:

[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]

Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, "cab" can be written as "-.-.-....-", (which is the concatenation "-.-." + "-..." + ".-"). We'll call such a concatenation, the transformation of a word.

Return the number of different transformations among all words we have.

Example:
Input: words = ["gin", "zen", "gig", "msg"]
Output: 2
Explanation: 
The transformation of each word is:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."

There are 2 different transformations, "--...-." and "--...--.".

 

Note:

  • The length of words will be at most 100.
  • Each words[i] will have length in range [1, 12].
  • words[i] will only consist of lowercase letters.

My Solution:
class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] alpha = new String[] {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
        ArrayList<String> list = new ArrayList<String>();
        ArrayList<String> newlist = new ArrayList<String>();
        if(words.length == 0)
            return 0;
        for(int i = 0; i < words.length; i++){
            String temp = "";
            for(int len = 0; len < words[i].length(); len++){
                temp += alpha[words[i].charAt(len) - 'a'];
            }
            list.add(temp);
        }
        newlist.add(list.get(0));
        for(int i = 1; i < list.size(); i++){
            if(newlist.contains(list.get(i)))
                continue;
            else{
                newlist.add(list.get(i));
            }
        }
        return newlist.size();
    }
}


Approach #1: Hash Set [Accepted]

Intuition and Algorithm

We can transform each word into it's Morse Code representation.

After, we put all transformations into a set seen, and return the size of the set.

class Solution {
    public int uniqueMorseRepresentations(String[] words) {
        String[] MORSE = new String[]{".-","-...","-.-.","-..",".","..-.","--.",
                         "....","..",".---","-.-",".-..","--","-.",
                         "---",".--.","--.-",".-.","...","-","..-",
                         "...-",".--","-..-","-.--","--.."};

        Set<String> seen = new HashSet();
        for (String word: words) {
            StringBuilder code = new StringBuilder();
            for (char c: word.toCharArray())
                code.append(MORSE[c - 'a']);
            seen.add(code.toString());
        }

        return seen.size();
    }
}

Complexity Analysis

  • Time Complexity: O(S), where S is the sum of the lengths of words in words. We iterate through each character of each word in words.

  • Space Complexity: O(S)


Summary:

编程萌新。

我的思路是先把每个词换成morse码存进一个数组,然后另建一个数组,把不重复的morse码存入,最后输出新数组的大小,即为题中所求不同morse码的数量。这里还遇到一个坑,如果没有输入时要输出0。

看了Approach #1觉得其实大致思路差不多,但使用了HashSet,重复的元素就不会存入集合中,不需要另建一个新数组。还应学习的是StringBuilder的用法,还有for each的循环结构。


<think>嗯,用户想在GNU Radio中添加一个Morse Code Vector Source。首先,我得回忆一下GNU Radio的组件结构。GNU Radio通常使用块(blocks)来处理信号,所以用户可能需要创建一个自定义的块来生成莫尔斯码的向量源。 首先,用户需要了解如何创建自定义的GNU Radio块。这可能涉及到使用gr_modtool,这是GNU Radio自带的模块创建工具。我应该先检查gr_modtool的使用方法,确保步骤正确。比如,使用`gr_modtool newmod`创建新模块,然后添加新的块。 接下来,莫尔斯码的生成逻辑。莫尔斯码由点和划组成,每个字符对应不同的时间序列。用户需要将输入的文本转换为对应的莫尔斯码符号,再将这些符号转换为时间序列的采样点。例如,点可能是一个短脉冲,划是长脉冲,字符之间和单之间有不同的间隔时间。 然后,关于信号参数,比如采样率、符号速率。假设每个点的时间长度为基本单位,比如1个点=1个单位时间,划就是3个单位,字符间隔是3个单位,单间隔是7个单位。用户需要根据这些参数生成相应的采样向量。 另外,输入输出类型的问题。作为Vector Source,可能输出的是浮点型或字节型的向量,每个元素代表一个采样点的幅度。可能需要使用Throttle块来控制数据流的速度,避免占用过多CPU资源。 测试和验证也是关键。用户可能需要使用QT GUI Time Sink来查看生成的波形是否正确,或者连接到音频输出块来听莫尔斯码的声音。调试过程中可能需要调整时间参数或符号映射。 还有可能遇到的常见问题,比如块同步问题、采样率不匹配,或者生成的向量长度不正确。需要确保生成的向量长度与GNU Radio的缓冲区大小兼容,避免出现流水线阻塞或数据丢失的情况。 最后,用户可能需要参考GNU Radio的官方文档和社区资源,比如邮件列表或论坛,寻找类似项目的例子或解决方案。如果现有模块库中有类似的模块,用户也可以直接使用或进行修改,而不必从头开始编写。</think>### 如何在GNU Radio中添加Morse Code Vector Source #### 步骤 1:理解需求 Morse Code Vector Source需要实现以下功能: 1. 将输入文本转换为国际莫尔斯码符号序列(如`A->".-"`) 2. 根据符号生成对应时间序列(点=短脉冲,划=长脉冲) 3. 添加字符间/单间间隔 4. 输出为离散采样值向量 #### 步骤 2:创建自定义模块 使用GNU Radio Companion的`gr_modtool`创建新模块: ```bash gr_modtool newmod morse cd gr-morse gr_modtool add -t sync -l python morse_vector_source ``` #### 步骤 3:实现核心逻辑 在生成的`morse_vector_source_impl.py`中添加代码: ```python MORSE_CODE = { 'A': '.-', 'B': '-...', 'C': '-.-.', 'D': '-..', 'E': '.', # ...完整映射表... } def generate_symbols(text, dot_duration=0.1, sample_rate=8000): samples = [] for char in text.upper(): code = MORSE_CODE.get(char, '') for symbol in code: duration = dot_duration if symbol == '.' else 3*dot_duration samples += [1.0]*int(duration*sample_rate) samples += [0.0]*int(dot_duration*sample_rate) # 符号内间隔 samples += [0.0]*int(3*dot_duration*sample_rate) # 字符间间隔 return samples ``` #### 步骤 4:配置块参数 在`morse_vector_source.block.yml`中定义参数: ```yaml parameters: - id: text label: Input Text dtype: string - id: dot_duration label: Dot Duration (s) dtype: float default: 0.1 ``` #### 步骤 5:测试流程 构建测试流程图: ``` Morse Vector Source -> Throttle -> QT GUI Time Sink -> Audio Sink ``` #### 验证参数示例: - 采样率:8000 Hz - 点持续时间:0.1秒 - 划持续时间:0.3秒 - 字符间隔:0.3秒 - 单间隔:0.7秒 #### 性能优化建议: 1. 使用预生成查找表加速符号生成 2. 添加流控制机制防止缓冲区溢出 3. 支持实时文本输入接口
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值