当前位置:网站首页>LeetCode 第十八天
LeetCode 第十八天
2022-07-13 17:55:00 【太阳在坠落】
今天回来晚了,就只刷了一个题。。。。。。
实现一个魔法字典
设计一个使用单词列表进行初始化的数据结构,单词列表中的单词 互不相同 。 如果给出一个单词,请判定能否只将这个单词中一个字母换成另一个字母,使得所形成的新单词存在于你构建的字典中。
实现 MagicDictionary 类:
- MagicDictionary() 初始化对象
- void buildDict(String[] dictionary) 使用字符串数组 dictionary 设定该数据结构,dictionary 中的字符串互不相同
- bool search(String searchWord) 给定一个字符串 searchWord ,判定能否只将字符串中 一个 字母换成另一个字母,使得所形成的新字符串能够与字典中的任一字符串匹配。如果可以,返回 true ;否则,返回 false 。
分析:
使用List来存储字典,当调用search时,遍历List中的每一个word与searchWord匹配,如果长度不同直接跳到下一个,长度相同时定义一个count来记录不同字符的个数,当count>1时直接跳出循环去遍历下一个word;如果遍历完当前word后count=1则直接返回true。
class MagicDictionary {
List<String> dic;
public MagicDictionary() {
dic = new ArrayList<>();
}
public void buildDict(String[] dictionary) {
for (int i = 0; i < dictionary.length; i++) {
dic.add(dictionary[i]);
}
}
public boolean search(String searchWord) {
for (int i = 0; i < dic.size(); i++) {
String word = dic.get(i);
if (word.length() != searchWord.length()) continue;
int count = 0;
for (int j = 0; j < word.length(); j++) {
if (word.charAt(j) != searchWord.charAt(j)){
count++;
if (count>1) break;
}
}
if (count == 1) return true;
}
return false;
}
}边栏推荐
- ROS communication mechanism
- 免费获取erlang安装包!!
- obsidian第三方插件无法加载
- [Go语言入门] 06 Go语言循环语句
- Various operations of binary tree (leaf node, parent node, search binary tree, delete node in binary tree, depth of binary tree)
- Leetcode-1 两数之和(STL, hashTable, unordered_map)
- Pyopencv basic operation guide
- General commands of MATLAB
- [Go语言入门] 10 Go语言Map详解
- Implementation of array flattening
猜你喜欢

01-kNN
![[go language introduction] 13 go language interface details](/img/38/7576257ecc706251e23b8a5ec2d98e.png)
[go language introduction] 13 go language interface details

Simple thread example - running Lantern - stack space allocation skills

在Colab上训练yolov3(一)

使用Pygame制作2048小游戏

Record: vscode connects to Alibaba cloud via SSH

How to use the virtual oscilloscope in keil5 for software simulation

Use MessageBox to realize window confession applet (with source code)

Rttread dynamic memory allocation

史上最快上手Redis(附加连接云服务器上的Redis)
随机推荐
[Go语言入门] 11 Go语言函数
Get Erlang installation package for free!!
Customize and modify the width and height of the van button in the van weap component library
分布式理论
Implementation of array flattening
免费获取erlang安装包!!
数据库黑马笔记DDL
Chapter 4 stm32+ld3320+syn6288+dht11 realize voice acquisition of temperature and humidity values (Part 1)
001 null pointer and wild pointer
Train yolov3 on colab (I)
STM32 uses a simple method to control many lights to achieve the effect of flowing water
Automation instrumentation and process control (final review)
Using idea IntelliJ to view bytecode files
[introduction to go language] 03 go language data types, variables, constants
[introduction to go language] 12 go language structure (struct) detailed explanation
Virtual memory location structure (reserved area, code area, stack area, heap area, literal constant area) and variable modifiers (const, auto, static, register, volatile, extern)
Record: vscode connects to Alibaba cloud via SSH
vim用法
How to use the virtual oscilloscope in keil5 for software simulation
SSM integration (classic self version)