题目:两个字符串是变位词 题目难度:简单 题目描述: 写出一个函数 anagram(s, t) 判断两个字符串是否可以通过改变字母的顺序变成一样的字符串. 解题思路: C++:引入哈希的思维,这道题就迎刃而解了. C++ Code: class Solution {public:    /**     * @param s: The first string     * @param b: The second string     * @return true or false     */ …
作者:CYC2018 文章链接:https://github.com/CyC2018/CS-Notes/blob/master/docs/notes/Leetcode+%E9%A2%98%E8%A7%A3.md 本文主要介绍的是LeetCode题库中与字符串相关的经典题目,提供了LeetCode原题题号,参考答案,以及题目的部分解析. 大家可以参考这个刷题指南来完成对字符串部分题目的练习,当然,这只是一部分,字符串的相关题目还有很多,譬如最长公共子序列和最长公共子串,这里列举的只是LeetCod…
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python> .但是仅仅看书对于知识的理解不够深入.于是选择了 lintcode 刷题,本篇博客即为最近做的算法题的一则小结. lintcode 的界面非常干净,还有中文版本的.博主的刷题的顺序为从入门开始,逐步深入,并不集中刷某块的题目.入门的8道题目都刷了,从简单开始优先做热点题(热点不分优先级). 一 矩阵…
虽然刷题一直饱受诟病,不过不可否认刷题确实能锻炼我们的编程能力,相信每个认真刷题的人都会有体会.现在提供在线编程评测的平台有很多,比较有名的有 hihocoder,LintCode,以及这里我们关注的 LeetCode. LeetCode 是一个非常棒的 OJ(Online Judge)平台,收集了许多公司的面试题目.相对其他 OJ 平台而言,有着下面的几个优点: 题目全部来自业内大公司的真实面试 不用处理输入输出,精力全放在解决具体问题上 题目有丰富的讨论,可以参考别人的思路 精确了解自己代码…
本文梳理对LeetCode上有关字符串习题的知识点,并给出对应的刷题建议.本文建议刷题的总数为32题.具体知识点如下图: 1.回文问题 题号:5. 最长回文子串,难度中等 题号:214. 最短回文串,难度困难 题号:564. 寻找最近的回文数,难度困难 2.子串问题(类似子集) 题号:76. 最小覆盖子串,难度困难 题号:115. 不同的子序列,难度困难 题号:522. 最长特殊序列 II,难度中等 题号:1163. 按字典序排在最后的子串,难度困难 3.表达式求值问题 题号:12. 整数转罗马…
转载自:http://blog.csdn.net/lnho2015/article/details/50962989 以下是我个人做题过程中的一些体会: 1. LeetCode的题库越来越大,截止到目前,已经有321个问题了.对于大多数人来说,没有时间也没有必要把所有题目都做一遍(时间充裕可以随意).刷个100题左右应该就差不多了(可以考虑序号为前100多的题目,相对更经典一点). 2. 从AC率高的开始做,难度从简单->中等,先不要做困难的. 3. 可以按照下文的面试出题频率顺序来做,从频率最…
本篇博客对最近做的链表的算法题做个简单的小结,主要描述题目和提供解题思路,具体代码见我的 github:https://github.com/MUSK1881/lintcode-by-python 36. 翻转链表 II(中等) 描述 翻转链表中第m个节点到第n个节点的部分 样例 给出链表1->2->3->4->5->null, m = 2 和n = 4,返回1->4->3->2->5->null 思路 新建链表头,在第 m 个节点前打断,在第…
基础(65) 巨水无比(4):1214.3816:2B题:1000A+B:2462:输出10个1 模拟/枚举/暴力(15):4063傻子模拟:1968小学生暴力:1218前缀和暴力:3856读英文:4106直接算:1800暴力判断:2208暴力判断(要会邻接表):1028枚举:1789&1830高能暴力:2241暴力:2120神奇的暴力:4145子集暴力:4029模拟处理:1086DFS树:1224暴力:3444暴力判 人类智慧题(17):2463输出0或1:1192找规律:1413奥数:143…
最近开始刷lintcode,记录下自己的答案,数字即为lintcode题目号,语言为python3,坚持日拱一卒吧... (一). 回文字符窜问题(Palindrome problem) 627. Longest Palindrome 给出一个包含大小写字母的字符串.求出由这些字母构成的最长的回文串的长度是多少. 数据是大小写敏感的,也就是说,"Aa" 并不会被认为是一个回文串 输入 : s = "abccccdd" 输出 : 7 说明 : 一种可以构建出来的最长回…
标签:动态规划 题目描述: Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. 解题思路: 这一题是非常经典的动态规划问题,在解题思路上可以按照经典的动态规划的解法,这是在系统学习动态规划之后第一个解决的LintCode上的问题: 1.子问题划分 给出两个字符串的A,B,两个字符串长度分别为lenA,lenB,求出两个字符串的LCS: 这划…
标签:动态规划 描述: 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: Insert a character Delete a character Re…
标签:动态规划 题目描述: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing…
对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 基本:两重for循环,时间复杂度O(n^2). class Solution { /** * Returns a index to the first occurrence of target in source, * or -1 if target is not part of source. * @param s…
单例模式,用C#实现过单例模式,python区别就是类里边的静态方法写法不一样,python叫类方法,函数之前加@classmethod class Solution: # @return: The same instance of this class every time __m_Solution=None @classmethod def getInstance(self): # write your code here if self.__m_Solution == None: self…
题目: 判断一个正整数是不是回文数. 回文数的定义是,将这个数反转之后,得到的数仍然是同一个数. 样例: 11, 121, 1, 12321 这些是回文数. 23, 32, 1232 这些不是回文数. 分析: 回文数就是反转后和自身一样,可利用java中StringBuffer中reverse()这一函数进行操作. 下面给出代码: public class Solution { /** * @param num a positive number * @return true if it's a…
题目: 给定n个小的字符串T和一个大的字符串S,先输出T总共再S中出现了多少次 然后q个询问···每次修改S上的一个字母,然后再次输出上述答案··· n小于1000,q<200000,T的总长度和S的长度都小于100000: 题解: 我们可以发现,每次修改位置P,那么S影响的范围只有在P-MAX到P+MAX的范围,其中MAX为T的最长长度····所以将这一范围的S跑一边AC自动机··然后更新答案即可·· 这道题也让我发现我做AC自动机以来一直有的一个不好的习惯··每次求出现次数都是暴力跳fail…
标签: 动态规划 题目描述: There are a row of n houses, each house can be painted with one of the k colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.…
标签: 动态规划 描述: Find the contiguous subarray within an array (containing at least one number) which has the largest product.   For example, given the array [2,3,-2,4], the contiguous subarray [2,3] has the largest product = 6. 解题思路: 前面几道题有点儿过分依赖答案了,后面还是…
标签:动态规划 题目描述: Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and return its area. Example For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4. 解题思路: 1.这一题明显使用动态规划来解题,…
标签: 动态规划 解题思路 1. 这道题最重要的是,存在三个字符串,但是并不需要两个二维矩阵来进行解,因为可以使用i+j-1来代表s3的下标,这样就可以通过i和j来遍历s3了.因为对于任何一个合法的交叉字符串都会有,s3(i+j-1)=s1(i-1) 或者s3(i+j-1) = s2(j-1) 2. 所以对于动态规划矩阵就可以在s1,s2这两个向量上进行分解,有dp[i][j], 其所代表的意义的是在s3(i+j-1)的位置上是否存在有s1(i)或者s2(j)与其相等, 3.如果结果返回true…
标签: 动态规划 描述: Given an integer array nums with all positive numbers and no duplicates, find the number of possible combinations that add up to a positive integer target. Example Given nums = [1, 2, 4], target = 4 The possible combination ways are: [1,…
标记: 动态规划 问题描述: Given n items with size Ai, an integer m denotes the size of a backpack. How full you can fill this backpack? Example If we have 4 items with size [2, 3, 5, 7], the backpack size is 11, we can select [2, 3, 5], so that the max size we…
标签: 位运算 描述: Given two 32-bit numbers, N and M, and two bit positions, i and j. Write a method to set all bits between i and j in N equal to M (e g , M becomes a substring of N located at i and starting at j)  解题思路: 1.这道题给出了四个int型的整数,其中m,n是两个给定的整数,而i与…
标签: 位运算 描述: Count how many 1 in binary representation of a 32-bit integer. 解题思路: 统计一个int型的数的二进制表现形式中1的个数1.与check power of 2中的解题形式非常相似,同样利用num&(num-1) 的结果来检查num中二进制形式上1的个数,区别在于Check Power of 2 是来检查是否存在1,而这一题主要是检查有几个1.2. 此题可以利用 num = num&(num-1) 每次进…
242.有效地字母异位词 由于本题的字符串只包含 26 个小写字符,因此可以使用长度为 26 的整型数组对字符串出现的字符进行统计,并对比字母出现的次数是否一致.不再使用 HashMap. toCharArray()的用法 java中的foreach的用法 class Solution { public boolean isAnagram(String s, String t) { int[] cnts = new int[26]; for (char c : s.toCharArray())…
题目 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 思路 字符串转数字:从字符串第一位开始取,每次取出的值转换为数字与之前的和的10倍相加即可 数字转字符串:str()函数 实现 class Solution: def multiply(self, num1: str, num2: str) -> str: def mul(string): product = 0 for i in string: product…
package leetcode.day_01_29; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * 请你来实现一个myAtoi(string s)函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). * <p> * 函数myAtoi(string s) 的算法如下: * <p> * 读入字符串并丢弃无用的前导空格 * 检查下一个字符(假设还未到字…
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAPLSII…
标签: 位运算 描述 Write a function that add two numbers A and B. You should not use + or any arithmetic operators. 解题思路: 利用位运算来解决A+B的问题,可以将此问题转化为解决不进位相加和进位(carry bit)的两部分问题: 1. 首先是不进位相加:_A = A^B 先对A和B进行异或运算(XOR manupitation) , A 和B 中两位不相同的变为1,相同的变为 0, 同为1是不…
标签: 动态规划 问题描述: There are n coins with different value in a line. Two players take turns to take one or two coins from left side until there are no more coins left. The player who take the coins with the most value wins. Could you please decide the fi…