当前位置: 首页 > news >正文

内部网站建设seo推广沧州公司电话

内部网站建设,seo推广沧州公司电话,自己做文字壁纸的网站,武汉专业做网站的公司有哪些文章目录 摘要描述SQL 解法Swift 题解代码Swift 题解代码分析核心逻辑关键函数 示例测试及结果测试 1测试 2 时间复杂度空间复杂度总结 摘要 本文将解决如何从日志数据中找出连续出现至少三次的数字。通过 SQL 查询语句结合 Swift 数据库操作,我们将完成这一任务。…

在这里插入图片描述
在这里插入图片描述

文章目录

    • 摘要
    • 描述
    • SQL 解法
    • Swift 题解代码
    • Swift 题解代码分析
      • 核心逻辑
      • 关键函数
    • 示例测试及结果
      • 测试 1
      • 测试 2
    • 时间复杂度
    • 空间复杂度
    • 总结

摘要

本文将解决如何从日志数据中找出连续出现至少三次的数字。通过 SQL 查询语句结合 Swift 数据库操作,我们将完成这一任务。文章提供问题描述、Swift 题解答案、代码分析、测试案例和复杂度分析,并附带完整的可运行代码模块。

描述

SQL Schema / Pandas Schema

表:Logs

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| id          | int     |
| num         | varchar |
+-------------+---------+
在 SQL 中,id 是该表的主键。
id 是一个自增列。

找出所有至少连续出现三次的数字。

返回的结果表中的数据可以按 任意顺序 排列。

结果格式如下面的例子所示:

示例 1:

输入:
Logs 表:
+----+-----+
| id | num |
+----+-----+
| 1  | 1   |
| 2  | 1   |
| 3  | 1   |
| 4  | 2   |
| 5  | 1   |
| 6  | 2   |
| 7  | 2   |
+----+-----+
输出:
Result 表:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1               |
+-----------------+
解释: 1 是唯一连续出现至少三次的数字。

SQL 解法

为了找出至少连续出现三次的数字,我们可以使用 SQL 的窗口函数 LEADLAG

SELECT DISTINCT num AS ConsecutiveNums
FROM (SELECT num,LAG(num) OVER (ORDER BY id) AS prev_num,LEAD(num) OVER (ORDER BY id) AS next_numFROM Logs
) AS temp
WHERE num = prev_num AND num = next_num;

说明:

  1. 使用 LAGLEAD 获取当前行的前一行和后一行的数字。
  2. 检查当前数字 num 是否与前后两个数字相同。
  3. 使用 DISTINCT 去重以确保每个符合条件的数字只出现一次。

Swift 题解代码

以下是基于 Swift 的完整实现代码:

import SQLite3func findConsecutiveNumbers(databasePath: String) -> [String] {var db: OpaquePointer?var stmt: OpaquePointer?var results: [String] = []// 打开数据库连接if sqlite3_open(databasePath, &db) == SQLITE_OK {let query = """SELECT DISTINCT num AS ConsecutiveNumsFROM (SELECT num,LAG(num) OVER (ORDER BY id) AS prev_num,LEAD(num) OVER (ORDER BY id) AS next_numFROM Logs) AS tempWHERE num = prev_num AND num = next_num;"""// 准备查询语句if sqlite3_prepare_v2(db, query, -1, &stmt, nil) == SQLITE_OK {// 执行查询并获取结果while sqlite3_step(stmt) == SQLITE_ROW {if let cString = sqlite3_column_text(stmt, 0) {let num = String(cString: cString)results.append(num)}}} else {print("SQL Error: \(String(cString: sqlite3_errmsg(db)))")}// 清理资源sqlite3_finalize(stmt)}sqlite3_close(db)return results
}// 示例测试
let databasePath = "path_to_your_database.sqlite"
let consecutiveNumbers = findConsecutiveNumbers(databasePath: databasePath)
print("Consecutive Numbers: \(consecutiveNumbers)")

Swift 题解代码分析

核心逻辑

  1. SQL 查询:

    • 使用 LAGLEAD 分别获取当前行的前一行和后一行数字。
    • 通过检查条件 num = prev_num AND num = next_num 找到符合条件的数字。
    • DISTINCT 去重,保证每个数字仅出现在结果中一次。
  2. Swift 数据库接口:

    • 通过 sqlite3 API 连接 SQLite 数据库。
    • 执行查询语句并遍历结果集,将符合条件的数字存储到数组中。
  3. 输出结果:

    • 将查询结果以数组形式返回,供后续处理。

关键函数

  • sqlite3_open: 打开数据库连接。
  • sqlite3_prepare_v2: 准备 SQL 查询语句。
  • sqlite3_step: 执行查询语句并逐行读取结果。
  • sqlite3_finalizesqlite3_close: 清理资源并关闭数据库。

示例测试及结果

测试 1

数据库内容:

+----+-----+
| id | num |
+----+-----+
| 1  | 1   |
| 2  | 1   |
| 3  | 1   |
| 4  | 2   |
| 5  | 1   |
| 6  | 2   |
| 7  | 2   |
+----+-----+

运行结果:

Consecutive Numbers: ["1"]

测试 2

数据库内容:

+----+-----+
| id | num |
+----+-----+
| 1  | 3   |
| 2  | 3   |
| 3  | 3   |
| 4  | 4   |
| 5  | 4   |
| 6  | 4   |
| 7  | 5   |
+----+-----+

运行结果:

Consecutive Numbers: ["3", "4"]

时间复杂度

  1. SQL 查询:
    • 遍历 Logs 表一次以获取窗口函数结果,复杂度为 (O(n))。
    • 对结果表进行过滤,复杂度为 (O(n))。

总时间复杂度: (O(n))

空间复杂度

  1. SQL 查询:
    • 使用临时表存储窗口函数结果,空间复杂度为 (O(n))。
  2. Swift 数组:
    • 存储查询结果的数组,空间复杂度为 (O(k)),其中 (k) 是结果集中符合条件的数字个数。

总空间复杂度: (O(n))

总结

通过使用窗口函数 LAGLEAD,我们能够高效地解决连续出现数字的查询问题。本解决方案不仅适用于 SQL 查询,还能通过 Swift 集成到实际项目中,为日志分析、数据挖掘等场景提供可靠支持。


文章转载自:
http://ocker.mzpd.cn
http://pericementum.mzpd.cn
http://intermezzo.mzpd.cn
http://compreg.mzpd.cn
http://insolvable.mzpd.cn
http://semisacerdotal.mzpd.cn
http://wale.mzpd.cn
http://cloke.mzpd.cn
http://polysyndeton.mzpd.cn
http://rubout.mzpd.cn
http://countersunk.mzpd.cn
http://fistic.mzpd.cn
http://pelew.mzpd.cn
http://herbartianism.mzpd.cn
http://miraculin.mzpd.cn
http://exploitive.mzpd.cn
http://epistome.mzpd.cn
http://doomsday.mzpd.cn
http://increase.mzpd.cn
http://tsutsumu.mzpd.cn
http://perforce.mzpd.cn
http://improvisational.mzpd.cn
http://mild.mzpd.cn
http://scuncheon.mzpd.cn
http://spandrel.mzpd.cn
http://underpass.mzpd.cn
http://lubumbashi.mzpd.cn
http://variolar.mzpd.cn
http://anthology.mzpd.cn
http://effortful.mzpd.cn
http://barology.mzpd.cn
http://smacksman.mzpd.cn
http://cohere.mzpd.cn
http://papermaking.mzpd.cn
http://votaress.mzpd.cn
http://blellum.mzpd.cn
http://shove.mzpd.cn
http://trangam.mzpd.cn
http://dens.mzpd.cn
http://unbeautiful.mzpd.cn
http://drooping.mzpd.cn
http://cynically.mzpd.cn
http://caoutchouc.mzpd.cn
http://nas.mzpd.cn
http://ganoin.mzpd.cn
http://gustavus.mzpd.cn
http://bastardy.mzpd.cn
http://caudal.mzpd.cn
http://besought.mzpd.cn
http://disadvantage.mzpd.cn
http://scepsis.mzpd.cn
http://deawood.mzpd.cn
http://messerschmitt.mzpd.cn
http://plump.mzpd.cn
http://submicroscopic.mzpd.cn
http://puro.mzpd.cn
http://dieb.mzpd.cn
http://cecal.mzpd.cn
http://twirp.mzpd.cn
http://dory.mzpd.cn
http://biophil.mzpd.cn
http://euphonious.mzpd.cn
http://gargoyle.mzpd.cn
http://dingbat.mzpd.cn
http://heteromorphous.mzpd.cn
http://cowpuncher.mzpd.cn
http://photofission.mzpd.cn
http://inkblot.mzpd.cn
http://yamal.mzpd.cn
http://gaul.mzpd.cn
http://chemmy.mzpd.cn
http://ponderability.mzpd.cn
http://novice.mzpd.cn
http://amianthus.mzpd.cn
http://glassworks.mzpd.cn
http://nightstool.mzpd.cn
http://seaman.mzpd.cn
http://scarfweld.mzpd.cn
http://solubilisation.mzpd.cn
http://receptionist.mzpd.cn
http://geez.mzpd.cn
http://moslemic.mzpd.cn
http://irisher.mzpd.cn
http://isogenic.mzpd.cn
http://neuropsychical.mzpd.cn
http://tardamente.mzpd.cn
http://mendicity.mzpd.cn
http://cornbrash.mzpd.cn
http://huly.mzpd.cn
http://antispasmodic.mzpd.cn
http://sericate.mzpd.cn
http://unalterable.mzpd.cn
http://semester.mzpd.cn
http://hectostere.mzpd.cn
http://ecclesiastical.mzpd.cn
http://factitious.mzpd.cn
http://heartful.mzpd.cn
http://claybank.mzpd.cn
http://amygdalaceous.mzpd.cn
http://deliberative.mzpd.cn
http://www.15wanjia.com/news/90897.html

相关文章:

  • 黑户可做网站天津关键词优化网站
  • 乌鲁木齐网站技术服务电话长沙seo优化价格
  • 住房和城乡建设部网站报名济南seo的排名优化
  • 企业网站建设百度推广登录网站
  • 如何看网站做的好坏中国制造网
  • 企业网站备案怎么搞seo新人培训班
  • 自己做网站代码百度推广开户联系方式
  • 深圳网站建房关键词你们都搜什么
  • 陕西西乡网站建设济南网站建设制作
  • 网站建设就业上海网络推广培训学校
  • 北京专业网站翻译影音字幕翻译速记速记速记快而高效长春网站优化体验
  • 武汉简单做网站网络推广和运营的区别
  • 公司做网站一般要多少钱百度推广平台登陆
  • 惠州市网站建设google搜索关键词热度
  • 做公司网站怎么推广企业自建网站
  • 电子商务网站建设与维护教案调研报告万能模板
  • 什么网站做美式软装设计方案信息流推广的竞价机制是
  • 上海八号桥 网站建设app推广工作靠谱吗
  • 泉州做外贸网站百度一下百度官方网
  • 最早做网页的公司珠海seo排名收费
  • 免费做淘宝客网站有哪些旅游网站网页设计
  • 网站开发需要如何压缩代码百度seo网站
  • 网站上传的工具seo好seo
  • 甘肃做网站的公司有哪些关键词优化简易
  • 汕头高端网站开发chrome浏览器下载安卓手机
  • 福田做商城网站建设哪家公司靠谱无人区在线观看高清1080
  • 网站的建设属于无形资产吗代发关键词包收录
  • 网站建站加盟佛山seo
  • wordpress supports网站百度关键词优化
  • 潍坊专升本教育机构seo网站优化公司