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. 最小公倍数是数论中 ...
随机推荐
- 【转载】Abstract Factory Step by Step --- 抽象工厂
抽象工厂是创建型模式的代表,其他的还有单件(Singleton).生成器(Builder).工厂方法(Factory Method)以及原型(Prototype),模式本身没有好坏之分,只有适用不适用 ...
- java接口的意义,为什么接口可以多继承,而类不可以?
原文地址:http://www.cnblogs.com/yunxiblog/p/5240690.html java当中继承一个接口,要重写他的方法的话,那为什么还要多此一举的去实现一个接口呢? 直接把 ...
- c语言 局部变量做返回值 问题
一般的来说,函数是可以返回局部变量的. 局部变量的作用域只在函数内部,在函数返回后,局部变量的内存已经释放了.因此,如果函数返回的是局部变量的值,不涉及地址,程序不会出错.但是如果返回的是局部变量的地 ...
- 024_mysql应用
初级照片位置:https://www.cnblogs.com/a276665092/gallery/image/277598.html 好丑啊 ,这怎么改啊!!!! 高级:https://www.cn ...
- 运算符的基本概念以及常用Scanner、随机数Random、选择结构的初步了解
运算符 分类 算术运算符 位运算符 关系运算符|比较运算符 逻辑运算符 条件运算符 赋值运算符 其中优先级顺序从上到下,可以记忆口诀:单目乘除位关系,逻辑三目后赋值 操作数: 运算符左右两边的数 表达 ...
- 使用maven搭建Hibernate
使用maven搭建Hibernate框架(web项目) create table USERS ( ID NUMBER not null primary key, NAME ), PASSWORD ), ...
- python基础语法(数据类型转换)
- springcloud Eureka Finchley.RELEASE 版本
创建一个父项目cloud-demo pom.xml <?xml version="1.0" encoding="UTF-8"?> <proje ...
- 安装tomcat8.5
1.去官网下载安装文件 网址:https://tomcat.apache.org/download-80.cgi 2.在安装目录的bin中运行startup.bat即可启动 3.启动好tomcat后, ...
- MongDB4.0-入门学习之运算符
MongDB 4.0 入门学习之运算符 基本语法:db.collection.find({<key>:{$symbol:<value>}}) 条件查询匹配运算符 符号 描述 范 ...