摩斯密码Java/Python实现
摩斯密码Java/Python实现
前言
摘自《犯罪大师》谜题关卡
一、Java实现
伪代码如下:
package com.hjc.demo.logic.morsecode;
import com.alibaba.fastjson.JSONObject;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
/**
* @Classname MorseCode
* @Description TODO
* @Date 2021/7/24 9:25
* @Created by Mr.He
* TODO 摩斯密码
*/
public class MorseCode {
final int ZERO = 0;
final int ONE = 1;
public String transform(String[] value, int dense) {
AtomicReference<String> result = new AtomicReference<>("");
int j = 0;
for (String s :
value) {
if (Objects.equals(dense, ZERO)) {
if (!morseCodeTable().containsKey(s)) {
return null;
}
j = 0;
}
if (Objects.equals(dense, ONE)) {
if (!morseCodeTable().containsValue(s)) {
return null;
}
j = 1;
}
int finalJ = j;
morseCodeTable().forEach((k, v) -> {
if(Objects.equals(finalJ,ZERO)){
if (Objects.equals(k, s.toUpperCase())) {
result.updateAndGet(v1 -> v1 + v