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的更多相关文章

  1. 【Leetcode_easy】1071. Greatest Common Divisor of Strings

    problem 1071. Greatest Common Divisor of Strings solution class Solution { public: string gcdOfStrin ...

  2. 【LeetCode】1071. Greatest Common Divisor of Strings 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 暴力遍历 日期 题目地址:https://leetc ...

  3. 【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 ...

  4. LeetCode 1071. 字符串的最大公因子(Greatest Common Divisor of Strings) 45

    1071. 字符串的最大公因子 1071. Greatest Common Divisor of Strings 题目描述 对于字符串 S 和 T,只有在 S = T + ... + T(T 与自身连 ...

  5. LeetCode.1071-字符串最大公约数(Greatest Common Divisor of Strings)

    这是小川的第391次更新,第421篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第253题(顺位题号是1071).对于字符串S和T,当且仅当S = T + ... + T ...

  6. [Swift]LeetCode1071.字符串的最大公因子 | Greatest Common Divisor of Strings

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  7. [UCSD白板题] Greatest Common Divisor

    Problem Introduction The greatest common divisor \(GCD(a, b)\) of two non-negative integers \(a\) an ...

  8. greatest common divisor

    One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the f ...

  9. 最大公约数和最小公倍数(Greatest Common Divisor and Least Common Multiple)

    定义: 最大公约数(英语:greatest common divisor,gcd).是数学词汇,指能够整除多个整数的最大正整数.而多个整数不能都为零.例如8和12的最大公因数为4. 最小公倍数是数论中 ...

随机推荐

  1. java中的break continue

    break语句 在任何循环语句的主体部分,均可用break控制循环的流程.break用于强行退出循环,不执行循环中剩余的语句.(break语句也在switch语句中使用) public class B ...

  2. 树上思维题——cf1060E

    只要算每条路径的贡献即可 显然长度为偶数的贡献是len/2 长度为奇数的贡献是(len+1)/2 所以结果就是(sum+tot)/2 sum:路径总长 tot:奇数路径数量 怎么求奇数路径数量:只有深 ...

  3. csp-s模拟测试85

    csp-s模拟测试85 $T1$全场秒切没有什么区分度,$T2$全场成功转化题意但是我并不会打,$T3$暴力都没打很遗憾. 100 00:21:49 02:56:35 02:56:49 135 02: ...

  4. csp-s模拟测试86

    csp-s模拟测试86 分屋前的最后一次考试,我早就放弃了自己. 02:02:46 70 02:02:57 03:16:08 100 03:16:08 $T1$忘了按位计算,达哥按位计算的$T1$当时 ...

  5. iOS 5 ARC 入门

    这篇文章还可以在这里找到 英语, 波兰语 Learn the ins and outs of ARC in iOS 5! 这是iOS 5 盛宴中的第12篇教程! 这篇教程是我们的新书 iOS 5 By ...

  6. iOS开发之SceneKit框架--SCNAction.h

    1.SCNAction简介 主要负责节点SCNNode的属性,实现node的渐变.移动.出现.消失.实现动画等. 2.相关API 节点的移动(earthNode的初始坐标(5,0,0)) //从当前位 ...

  7. c语言 局部变量做返回值 问题

    一般的来说,函数是可以返回局部变量的. 局部变量的作用域只在函数内部,在函数返回后,局部变量的内存已经释放了.因此,如果函数返回的是局部变量的值,不涉及地址,程序不会出错.但是如果返回的是局部变量的地 ...

  8. sessionStorage 和 localStorage的区别

    sessionStorage.setItem('userName',userName) // 存 sessionStorage.getItem('userName') // 取 sessionStor ...

  9. 继承关系中子类使用@Data注解问题

    HashSet中使用@Data注解问题 平时习惯使用lombok工具,免去了我们写get.set方法之类的,当然了,我们使用@Data注解后,equals().hashCode().toString( ...

  10. [Neo4j] 添加算法插件包

    下载graph-algorithms-algo-xxx.jar包,我下的是3.5.3.1,放到neo4j目录的plugins文件夹下 修改 conf目录下的配置文件 neo4j.conf ,加一行: ...