(LeetCode 72)Edit Distance】的更多相关文章

Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a characterb) Delete a characterc) Replace…
题目 Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Insert a character b) Delete a character c) Re…
[题目] Given two words word1 and word2, find the minimum number of operations required to convert word1 to word2. You have the following 3 operations permitted on a word: Insert a character Delete a character Replace a character Example 1: Input: word1…
https://leetcode.com/problems/edit-distance/ Given two words word1 and word2, find the minimum number of steps required to convert word1 to word2. (each operation is counted as 1 step.) You have the following 3 operations permitted on a word: a) Inse…
Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called a reduced proper fraction. If we list the set of reduced proper fractions for d  8 in ascending order of size, we get: 1/8, 1/7, 1/6, 1/5, 1/4, 2/7, 1…
Given two strings S and T, determine if they are both one edit distance apart. 给定两个字符串,判断他们是否是一步变换得到的. 在这里需要注意几点: 1.不等于1的变换都要返回false(包括变换次数等于0). 2.还有很多细节需要注意. 方法如下: 1.直接判断:1)如果差值大于1,直接返回false.  2)如果长度相同,那么依次判断,是否只有一个字母不一样.  3)如果不一样,那么看是否是只是多出了一个字母. p…
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 possiblities to satisify one edit distance apart: Insert a character into s to get t Delete a character from s to get t Replace a character of s to get t…
Given two strings s and t, determine if they are both one edit distance apart. Note: There are 3 possiblities to satisify one edit distance apart: Insert a character into s to get t Delete a character from s to get t Replace a character of s to get t…
Problem: Given two strings S and T, determine if they are both one edit distance apart. General Analysis: This problem is not hard. However, to write out more efficient and elegant solution, we need to dive more deep to understand the logic behind it…
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. 题目要求 : 求整数数组的所有子集 注意: 1.子集元素按非降序排列 2.不包含重复的子集 解题思路: 求解这类诸如子集的题目,都可以采用回溯…
================================== LeetCode的一些算法题,都是自己做的,欢迎提出改进~~ LeetCode:http://oj.leetcode.com ================================== <Reverse Words in a String>-20140328 Given an input string, reverse the string word by word. For example, Given s =…
326. Power of ThreeGiven an integer, write a function to determine if it is a power of three. Follow up: Could you do it without using any loop / recursion? 看到这种题目,第一想法就是用递归或者循环来做,但是题目要求了不能用这种方法来做,所以只能另想他法. 假设输入一个数 n,如果 n 是3的幂,那么 3^x = n, 即 x = log10…
1 问题,给定一个字符串,求字符串中包含的最大回文子串,要求O复杂度小于n的平方. 首先需要解决奇数偶数的问题,办法是:插入’#‘,aba变成#a#b#a#,变成奇数个,aa变成#a#a#,变成奇数个. 其次要解决指导思想问题,这个方法的切入点是奇数的回文字符串具有对称性,就像圆形一样,所以我们就可以迭代圆心,把具有对称性的点到圆心的距离想象成半径.所以需要两个迭代,一个迭代字符串中的点,另一个从半径为1开始,到超出字符串范围为止迭代字符串的半径. #!/usr/binp/python #!co…
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] return 3,and [3,4,-1,1] return 2. Your algorithm should run in O(n) time and uses constant space. 题目: 给一无序数组,找到数组中从1开始第一个不出现的正整数. 要求O(n)的时间复杂度和常数空间复杂度…
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2). Find the minimum element. You may assume no duplicate exists in the array. 题目: 给定一旋转有序数组,求该数组的最小值. 思路: 二分查找Binary Search 比较简单…
Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 begin to intersect at node c1. Notes: If the two linked lists have…
给定一个int数组(每个数字无前导0),要求用这些数字拼接出一个最大的数字. 解决思路: 对整个数组进行排序,把排序后的结果拼接起来. 那么如何进行排序呢?只需要定义一个比较函数,如果str(x)+str(y)<str(y)+str(x),则说明y放在x前面比较合适. 在Python中,sorted方法现在只能提供key作为比较函数,这个函数只能处理单个元素,没法进行两两比较,这是有局限性的. class Solution: # @param {integer[]} nums # @return…
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be in lower-case. 题目: 给一组字符串,返回所有满足Anagrams(回文构词法)的字符串: Anagrams是指由颠倒字母顺序组成的单词,比如“dormitory”颠倒字母顺序会变成“dirty room”,“tea”会变成“eat”. 回文构词法有一个特点:单词里的字母的种类和数目没…
# json(为字符串形式)用于不同语言之间的数据交互,只适用于简单的数据交互,字典之类可以,函数就不行了,如下例 ''' import json def say(name):print('Hi!',name) info={ 'name':'Xue Jingjie', 'age':22, 'func':say } f=open('第73.text','w') f.write(json.dumps(info)) f.close() ''' #pickle(为二进制形式)则可以用于复杂的,pickl…
题目描述如下所示: 给定一个二叉树和一个目标和,找到所有从根节点到叶子节点路径总和等于给定目标和的路径.(https://leetcode-cn.com/problems/path-sum-ii/) 说明: 叶子节点是指没有子节点的节点. 示例:给定如下二叉树,以及目标和 sum = 22, 思路:从根节点开始,分别遍历左右子树,如果到达叶子节点且满足路径之和等于sum,则加入到结果中 /** * Definition for a binary tree node. * public class…
题目: You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? Note: Given n will be a positive integer. Approach #1 Brute Force [Time Limit Ex…
转自 http://www.turen.me/archives/509 Sublime Text 2 是现在很受大家欢迎的编辑器了,不仅是在web前端,在书定简单的php.Js等代码时,也是相当的好用,再配合多种的插件和新颖的界面,更是让人欲罢不能.在使用时,我们通过喜欢打开一个文件的时候,右击再选择打开程序,比如会用记事本.Notedpad++之类的,这时如果把Sublime Text也加入到其中毕竟会方便不少,在找了一番后,实验成功,效果如下: 实现起来也很简单:1.打开注册表,开始→运行→…
这题一年前就做过,当时刚开始刷leetcode,提交了几十次过不去,就放那没管了.今天剑指offer又遇到这题,终于做出来了,用的dp. class Solution { public: bool isMatch(string s, string p) { int s_len=s.size(),p_len=p.size(); vector<vector<,vector<,false)); //dp[i][j]表示s[0,i-1]和p[0,j-1]能否匹配 dp[][]=true;//空串…
Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 题目: 给一数组,n个元素,将数组向右移动循环移动k个元素. 思路: 注意题目的要求是循环移动,所以k可以是任意非负整数,但数组元素个数为n,因此k=k%n. 一种通用的思路就是: 1.翻转整个数组 :A[…
Given a sorted linked list, delete all duplicates such that each element appear only once. For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3. 题目要求: 给一有序链表,删除重复的结点,使得每个元素只出现一次. 解题思路: 1.从头到尾遍历链表,如果前后两个结点相同…
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x. You should preserve the original relative order of the nodes in each of the two partitions. For example,Given 1->4->3->2…
Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6,  val = 6Return: 1 --> 2 --> 3 --> 4 –> 5 题目要求: 删除链表中包含val的元素结点 解题思路: 重点在于找到第一个非val的头结点,然后遍历链表,依次删除值为…
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目要求: 合并两个有序链表 注意: 不能开辟新的结点空间 解题思路: 1.归并排序,创建一个新的头结点,从头到尾分别遍历两个链表,并依次比较其大小关系,每次将头指针指向小的那个. 2.递归思想(对于为改变链表结构的…
Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. 题目: 最大子数组和 思路: 1.暴力枚举 起点:i=0,...…
sam格式很精炼,几乎包含了比对的所有信息,我们平常用到的信息很少,但特殊情况下,我们会用到一些较为生僻的信息,关于这些信息sam官方文档的介绍比较精简,直接看估计很难看懂. 今天要介绍的是如何通过bam文件统计比对的indel和mismatch信息 首先要介绍一个非常重要的概念--编辑距离 定义:从字符串a变到字符串b,所需要的最少的操作步骤(插入,删除,更改)为两个字符串之间的编辑距离. (2016年11月17日:增加,有点误导,如果一个插入有两个字符,那编辑距离变了几呢?1还是2?我又验证…