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. 树莓派3b+ 实现视频监控

    设备:树莓派3B+.Raspberry Pi Camera sudo raspi-config #启动camera sudo reboot #监测摄像头是否安装成功 raspistill -o ima ...

  2. List、Map、Set 三个接口,存取元素时,各有什么特点

    List与Set都是单列元素的集合,它们有一个功共同的父接口Collection. Set里面不允许有重复的元素, 存元素:add方法有一个boolean的返回值,当集合中没有某个元素,此时add方法 ...

  3. IPTABLES--iptables

    A网:https://12.102.246.15:8080 B网:https://12.100.246.15:8080   A网DNAT转换: iptables -t nat -A PREROUTIN ...

  4. C++ 俄罗斯方块

    #include <iostream> #include <string.h> #include <stdlib.h> #include <time.h> ...

  5. luoguP1154 奶牛分厩 [数论]

    题目描述 农夫约翰有N(1<=N<=5000)头奶牛,每头奶牛都有一个唯一的不同于其它奶牛的编号Si,所有的奶牛都睡在一个有K个厩的谷仓中,厩的编号为0到K-1.每头奶牛都知道自己该睡在哪 ...

  6. day 54 Django基础四之模板系统

    Django基础四之模板系统   本节目录 一 语法 二 变量 三 过滤器 四 标签Tags 五 模板继承 六 组件 七 自定义标签和过滤器 八 静态文件相关 一 语法   模板渲染的官方文档 关于模 ...

  7. JavaScript编码指南

    出其不意 1920年,William Strunk Jr的<英文写作指南>出版了,这本书给英语的风格定下了一个规范,而且已经沿用至今.代码其实也可以使用相似的方法加以改进. 本文接下来的部 ...

  8. docker企业级镜像仓库harbor

    第一步:安装docker和docker-compose 第二步:下载harbor-offine-installer-v1.5.1.tgz 第三步:上传到/opt,并解压 第四步:修改harbor.cf ...

  9. MybatisPlus联合分页查询

    跟单表分页查询差不多 1.编写查询语句 public interface QuestionMapper extends BaseMapper<Question> { @Select(&qu ...

  10. 2018-11-19-WPF-在image控件用鼠标拖拽出矩形

    title author date CreateTime categories WPF 在image控件用鼠标拖拽出矩形 lindexi 2018-11-19 15:35:13 +0800 2018- ...