作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetcode.com/problems/greatest-common-divisor-of-strings/ 题目描述 For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concatenate…
题目如下: For strings S and T, we say "T divides S" if and only if S = T + ... + T  (T concatenated with itself 1 or more times) Return the largest string X such that X divides str1 and X divides str2. Example 1: Input: str1 = "ABCABC", st…
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrings(string str1, string str2) { return (str1+str2==str2+str1) ? (str1.substr(, gcd(str1.size(), str2.size()))) : ""; } }; 参考 1. Leetcode_easy_1071…
lc1071 Greatest Common Divisor of Strings 找两个字符串的最长公共子串 假设:str1.length > str2.length 因为是公共子串,所以str2一定可以和str1前面一部分匹配上,否则不存在公共子串. 所以我们比较str2和str1的0~str2.length()-1部分, 若不同,则直接返回””,不存在公共子串. 若相同,继续比较str2和str1的剩下部分,这里就是递归了,调用原函数gcd(str2, str1.substring(str…
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连接 1 次或多次)时,我们才认定 "T 能除尽 S". 返回字符串 X,要求满足 X 能除尽 str1 且 X 能除尽 str2. 每日一算法2019/6/17Day 45LeetCode1071. Greatest Common Divisor of Strings 示例 1: 输入:s…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/description/ 题目描述 Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in…
这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T(T与自身连接1次或更多次)时,我们说"T除S". 返回最大的字符串X,使得X除以str1,X除以str2. 例如: 输入:str1 ="ABCABC",str2 ="ABC" 输出:"ABC" 输入:str1 ="AB…
题目如下: Given two strings text1 and text2, return the length of their longest common subsequence. A subsequence of a string is a new string generated from the original string with some characters(can be none) deleted without changing the relative order…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 [LeetCode] https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ Total Accepted: 67533 Total Submissions: 178900 Difficulty: Easy 题目描述 Given a binary…
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 个人公众号:负雪明烛 本文关键词:prefix, 公共前缀,题解,leetcode, 力扣,Python, C++, Java 目录 题目描述 题目大意 解题方法 遍历前缀子串 使用set 遍历最短字符串 排序 日期 题目地址:https://leetcode.com/problems/longest-common-prefix/description/ 题目描述 Write a fun…
题目: Write a function to find the longest common prefix string amongst an array of strings. 思路:求最长前缀子串,假设第一个字符串是最长前缀子串,采用增强for得到数组中每个字符串,分别与第一个字符串的同一位置进行比较,有一个字符串在该位置不一致,就返回. public class Solution { public String longestCommonPrefix(String[] strs) { i…
Write a function to find the longest common prefix string amongst an array of strings. Solution: class Solution { public: string longestCommonPrefix(vector<string>& strs) { //runtime:4ms string str=""; if(strs.empty())return str; ; whi…
题目: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that…
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as th…
Lowest Common Ancestor of a Binary Search Tree Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two…
Longest Common Prefix Write a function to find the longest common prefix string amongst an array of strings. 解法一: 思路:设置一个位数记录器num,遍历所有字符串的第num位.如果都相同,则num++. 直到某字符串结束或者所有字符串的第num位不都相同,则返回[0~num-1]位,即最长公共前缀. class Solution { public: string longestComm…
Write a function to find the longest common prefix string amongst an array of strings. 题解: 简单的暴力遍历解决 class Solution { public: string longestCommonPrefix(vector<string>& strs) { int n = strs.size(); || strs[].empty()) return ""; string…
题目如下: Given a rooted binary tree, return the lowest common ancestor of its deepest leaves. Recall that: The node of a binary tree is a leaf if and only if it has no children The depth of the root of the tree is 0, and if the depth of a node is d, the…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcode.com/problems/find-common-characters/ 题目描述 Given an array A of strings made only from lowercase letters, return a list of all characters that show up…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 正则+统计 日期 题目地址:https://leetcode.com/problems/most-common-word/description/ 题目描述 Given a paragraph and a list of banned words, return the most frequent word that is not in the li…
LCIS /* 1423 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MAXN 505 int a[MAXN]; int b[MAXN]; int c[MAXN]; int n, m; int ans; int max(int a, int b) { return a>b ? a:b; } void solve() { int i, j, k; memset(c, , siz…
题目如下: Given an array nums of integers, we need to find the maximum possible sum of elements of the array such that it is divisible by three. Example 1: Input: nums = [3,6,5,1,8] Output: 18 Explanation: Pick numbers 3, 6, 1 and 8 their sum is 18 (maxi…
题目如下: You are given some lists of regions where the first region of each list includes all other regions in that list. Naturally, if a region X contains another region Y then X is bigger than Y. Given two regions region1, region2, find out the smalle…
题目如下: Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times, you ne…
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)➤GitHub地址:https://github.com/strengthen/LeetCode➤原文地址:https://www.cnblogs.com/strengthen/p/10961892.html ➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章…
583. Delete Operation for Two Strings Given two words word1 and word2, find the minimum number of steps required to make word1 and word2 the same, where in each step you can delete one character in either string. Example 1: Input: "sea", "e…
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 本文关键词:LeetCode,力扣,算法,算法题,交替合并字符串,Merge Strings Alternately,刷题群 目录 题目描述 解题思路 合并 欢迎加入组织 日期 题目地址:https://leetcode-cn.com/problems/merge-strings-alternately/ 题目描述 给你两个字符串 word1 和 word2 .请你从 word1 开始,通过交替添加字母来合并字符串.如果一个…
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.com/problems/delete-operation-for-two-strings/description/ 题目描述 Given two words word1 and word2, find the minimum number of steps required to make word…
题目如下: You are given two strings s1 and s2 of equal length consisting of letters "x" and "y" only. Your task is to make these two strings equal to each other. You can swap any two characters that belong to different strings, which means…
题目如下: 解题思路:本题和[leetcode]583. Delete Operation for Two Strings 类似,区别在于word1[i] != word2[j]的时候,是删除word1[i]还是word2[j]取决于min(dp[i-1][j]+ord(word1[i-1]),dp[i][j-1]+ord(word2[j-1])). 代码如下: class Solution(object): def minimumDeleteSum(self, s1, s2): ""…