# Python 实现 LeetCode 编辑距离 (Edit Distance)
在计算机科学中,编辑距离是衡量两个字符串之间相似度的重要指标,通常用于拼写检查、文本比较等应用。具体来说,编辑距离是通过插入、删除或替换字符所需的最小操作次数,将一个字符串转换为另一个字符串。这一算法问题在 LeetCode 上被广泛讨论。
## 编辑距离算法
在理解编辑距离前,我们先来看几个基本操作:
-
原创
2024-09-02 05:28:12
52阅读
题目内容Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one solution, and you may not use the sa
转载
2023-12-17 08:19:10
30阅读
岛屿问题一题目链接463. 岛屿的周长题目描述解题思路求岛屿的周长其实有很多种方法,如果用 DFS 遍历来求的话,有一种很简单的思路:岛屿的周长就是岛屿方格和非岛屿方格相邻的边的数量。注意,这里的非岛屿方格,既包括水域方格,也包括网格的边界。我们可以画一张图,看得更清晰:AC代码class Solution {
int ans = 0;
int dir[][] = {{0,1},{
转载
2024-06-08 16:20:57
51阅读
实现 strStr() 函数。给你两个字符串 haystack 和 needle ,请你在 haystack 字符串中找出 needle 字符串出现的第一个位置(下标从 0 开始)
原创
2022-06-13 17:06:21
4765阅读
当返回 0 。这与 C 语言的 strstr() 以及 Java 的 indexOf() 定义相符。示例 1:示例 2:提示:...
原创
2022-10-26 01:15:28
66阅读
urn -1; ...
原创
2022-02-28 13:51:47
38阅读
int strStr(char * haystack, char * needle){int len=strlen(needle);if(len==0){return 0;}int i,j;int tmp=0;for(i=0;i<strlen(haystack);++i){if(haystack[i]==needle[0]){ tmp=i+1; for(j=1;j<len;++j) { if(haystack[tmp]==needle[j])
原创
2022-01-10 14:47:49
44阅读
ImplementstrStr().Return the index of the first occurrence of needle in haystack, or-1if needle is not part of haystack.Example 1:Input: haystack = "hello", needle = "ll"Output: 2Example...
原创
2022-03-09 15:24:30
27阅读
LeetCode 题库的第 28 题——实现strStr() 题目如下:解题代码 该题就是两层循环,第一层循环主要是遍历字符串,第二层循环用来进行匹配。实现代码如下:int strStr(char* haystack, char* needle) { int pos = -1; int str1len = strlen(haystack); int str2len = str
原创
2022-04-07 09:32:32
67阅读
#include <limits.h>#include <ctype.h>#include <iostream>class Solution {public: int myAtoi(string str) { int pos; bool negative; if (false == get_dig...
原创
2022-12-01 16:50:33
90阅读
class Solution {public: int strStr(string haystack, string needle) { if (true == needle.empty()) { return 0; } if (true == haystack.empty()) { ret...
原创
2022-12-01 16:51:05
38阅读
https://leetcode-cn.com/problems/implement-strstr/description/我的解决方案:public static int strStr(String haystack, String needle) { if(needle.length()>haystack.length())return -1; ...
原创
2021-06-29 13:58:50
150阅读
Implement strStr().
Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
Example 1:
Input: haystack = "hello", needle = "ll"
Output: 2
Example 2:
原创
2021-07-12 13:47:43
69阅读
int strStr(char * haystack, char * needle){ int i,j; int hlen=strlen(haystack); int nlen=strlen(needle); if (!nlen) return 0; for (i=0; i<hlen && hlen
转载
2020-09-25 18:33:00
28阅读
2评论
给定两个大小为 m 和 n 的有序数组 nums1 和 nums2。请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n))。你可以假设 nums1 和 nums2 不会同时为空。示例 1:nums1 = [1, 3]nums2 = [2]则中位数是 2.0示例 2:nums1 = [1, 2]nu
转载
2023-05-31 12:55:07
64阅读
题目:原题链接(简单)与题目0225配套,互为相反。解法执行用时Ans 1 (Python)40ms (>63.47%)Ans 2 (Python)36ms (>84.25%)LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。解法一(使用queue的栈实现):from queue import LifoQueueclass MyQueue: def __init__(s
原创
2022-01-12 16:30:22
51阅读
题目:原题链接(简单)解法执行用时Ans 1 (Python)28ms (>98.79%)LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。解法一:class MyStack: def __init__(self): """ Initialize your data structure here. """ self.stack
原创
2022-01-12 16:30:24
128阅读
题目:原题链接(中等)标签:字典树、字符串解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(NS)O(NS)O(NS)O(NS)O(NS)O
原创
2022-10-22 01:23:14
114阅读
题目:原题链接(简单)解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)84ms (96.92%)Ans 2
原创
2022-02-18 11:08:47
28阅读
题目:原题链接(简单)解法时间复杂度空间复杂度执行用时Ans 1 (Python)O(N)O(N)O(N)O(N)O(N)O(N)84ms (96.92%)Ans 2 (Python)O(N)O(N)O(N)O(N)O(N)O(N)88ms (91.46%)Ans 3 (Python)LeetCode的Python执行用时随缘,只要时间复杂度没有明显差异,执行用时一般都在同一个量级,仅作参考意义。解法一(暴力解法):def floodFi
原创
2021-08-26 10:35:44
156阅读