leetcode 1071 Greatest Common Divisor of Strings
lc1071 Greatest Common Divisor of Strings
找两个字符串的最长公共子串
假设:str1.length > str2.length
因为是公共子串,所以str2一定可以和str1前面一部分匹配上,否则不存在公共子串。
所以我们比较str2和str1的0~str2.length()-1部分,
若不同,则直接返回””,不存在公共子串。
若相同,继续比较str2和str1的剩下部分,这里就是递归了,调用原函数gcd(str2, str1.substring(str2.length))
还有一些细节:
比如保证str1永远>str2,可以用一个if(str1 < str2){swap(str1, str2)}
str1 == str2 直接检查 str1.equals(str2)
class Solution {
public String gcdOfStrings(String str1, String str2) {
return GCD(str1, str2);
}
public String GCD(String a, String b) {
if(a.length() > b.length()) {
for(int i = 0; i < b.length(); i++){
if(b.charAt(i) != a.charAt(i)){
return "";
}
}
String temp = a.substring(b.length());
return GCD(temp,b);
}
else if(b.length() > a.length())
return GCD(b, a);
else
return a.equals(b) ? a : "";
}
}
leetcode 1071 Greatest Common Divisor of Strings的更多相关文章
- 【Leetcode_easy】1071. Greatest Common Divisor of Strings
problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...
- 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...
- 【leetcode】1071. Greatest Common Divisor of Strings
题目如下: For strings S and T, we say "T divides S" if and only if S = T + ... + T (T concate ...
- LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45
1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...
- LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)
这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...
- [Swift]LeetCode1071.字符串的最大公因子 | Greatest Common Divisor of Strings
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- [UCSD白板题] Greatest Common Divisor
Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...
- greatest common divisor
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...
- 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)
定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...
随机推荐
- VC 复制移动删除重命名文件文件
说明: 1.以下封装了4个函数_CopyFile,_DeleteFile,_MoveFile,_ReNameFile 2.每个函数都先拷贝了原来的路径,保证了路径是以2个\0\0结尾.(如果不以2个\ ...
- poj 1742 Coins(二进制优化多重背包)
传送门 解题思路 多重背包,二进制优化.就是把每个物品拆分成一堆连续的\(2\)的幂加起来的形式,然后把最后剩下的也当成一个元素.直接类似\(0/1\)背包的跑就行了,时间复杂度\(O(nmlogc) ...
- c++中变量、变量名、变量地址、指针、引用等含义
首先了解内存,内存就是一排房间,编号从0开始,0,1,2,3,4,5...... 房间里面一定要住人,新人住进去了,原来的人就走了:不管你住不住,里面都有人. 编号就是地址.里面的人就是内容,为了我们 ...
- C#winform datagridview单元格的单击处理
首先看看效果图: 需求:要求是的在datagridview里面绑定数据后,可以任意点击想要点击的某列的单元格进行改变数据.需要在datagridview里面写3个事件 1.RowPrePaint事件: ...
- PAT甲级——A1128 N Queens Puzzle【20】
The "eight queens puzzle" is the problem of placing eight chess queens on an 8 chessboard ...
- 安装MySQL出现的this application
1,https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=1385 2,安装Windows 图像组件 (WIC)以及NET Fra ...
- <day004>小娜显示空白+CSV文件的基本操作+普通的代理使用
小知识: 当小娜搜索显示空白的时候,怎么解决? 任务管理器结束小娜进程就好了= =*(多半是惯得,关掉就好了!) 任务1:CSV文件的基本操作 import csv import pandas as ...
- 如何为ABAP程序添加权限检查
一.确认权限对象,及其关联字段: TCode: SU21 例如权限对象"M_MSEG_WMB",它关联字段为"WERKS",详见下图: 二.在ABAP代码中添加 ...
- virtualbox虚拟机下的cdlinux找不到无线网卡的解决方法
virtualbox虚拟机下的cdlinux找不到无线网卡的解决方法 自己解决了,记录一下. cdlinux 带reaver1.4的版本 http://pan.baidu.com/share/link ...
- springAop的使用
AspectJ使用org.aspectj.lang.JoinPoint接口表示目标类连接点对象,如果是环绕增强时,使用org.aspectj.lang.ProceedingJoinPoint表示连接点 ...