【问题标题】:recognize palindromic sequences and count them on a given number识别回文序列并在给定的数字上计算它们
【发布时间】:2015-05-27 18:59:25
【问题描述】:

我有一个像这样的整数输入 1213121

我想数一下有多少回文序列(输出必须是5个回文序列)

因为第一个回文序列是 121。

第二个是1213121。

第三个是21312。

第四个是131。

第五个是121。

我试图在谷歌上搜索,但我发现的都是拆分数字或识别回文数字。

你能帮忙吗?

感谢您的宝贵时间+

【问题讨论】:

  • 第三个是 21312 吗?还有,哪个 Sql RDBMS
  • 请解释这与 SQL 有何关系。如果数据在数据库中,请显示表格布局和您的预期结果。
  • 你是对的斯图尔特。
  • 戈登,练习是用sql脚本写一个句子。
  • 您使用的是哪个 DBMS?后格雷斯?甲骨文?

标签: sql numbers integer


【解决方案1】:

您没有指定您的 DBMS,所以这里有一个 TSQL 解决方案,它至少向您展示了一个简单的方法来完成您需要做的事情。

DECLARE @input VARCHAR(25) = '1213121';

WITH CTE_nums --just a list of numbers from 1 to the length of the input which is 7 in this case
AS
(
    SELECT 1 AS pos
    UNION ALL
    SELECT pos + 1
    FROM CTE_nums
    WHERE pos < LEN(@input)
)

SELECT  SUBSTRING(@input,A.pos,B.pos - A.pos + 1) AS palindrome_seq
FROM CTE_nums A
CROSS JOIN CTE_nums B --cross join to find every possible combination of those numbers
WHERE A.pos <= B.pos - 2 --only choose strings that are at least 2 characters long(not sure if you want it to be 2 or 3)
AND SUBSTRING(@input,A.pos,B.pos - A.pos + 1) = REVERSE(SUBSTRING(@input,A.pos,B.pos - A.pos + 1)) --check if that substring is the same reversed substring

结果:

palindrome_seq
-------------------------
121
1213121
21312
131
121

【讨论】:

    猜你喜欢
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2016-04-20
    • 2017-10-07
    • 2013-07-02
    相关资源
    最近更新 更多