├── .DS_Store
├── LeetCode
├── .DS_Store
├── 0二叉堆实现.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 1. 两数之和.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ ├── kent.sun.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 1013. 将数组分成和相等的三个部分.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 1094. 拼车.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 11. 盛最多水的容器.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 1109. 航班预订统计.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 120. 三角形最小路径和.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 121. 买卖股票的最佳时机.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 122. 买卖股票的最佳时机 II.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 14. 最长公共前缀.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 141. 环形链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ ├── kent.sun.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 142. 环形链表 II.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 146. LRU缓存机制.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 148. 排序链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 15. 三数之和.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 151. 反转字符串中的单词.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 155. 最小栈.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 160. 相交链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 176. 第二高的薪水.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 2. 两数相加.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 203. 移除链表元素.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 206. 反转链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ ├── kent.sun.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 21. 合并两个有序链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 215. 数组中的第K个最大元素.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 221. 最大正方形.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 23. 合并K个排序链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ ├── kent.sun.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 236. 二叉树的最近公共祖先.java
├── 237. 删除链表中的节点.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 25. K 个一组翻转链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 26. 删除有序数组中的重复项.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 27. 移除元素.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 283. 移动零.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 283. 移动零的副本.playground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 3. 无重复字符的最长子串.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 33. 搜索旋转排序数组.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 354. 俄罗斯套娃信封问题.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 370. 区间加法.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 393. UTF-8 编码验证.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 42. 接雨水.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 432. 全 O(1) 的数据结构.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 438. 找到字符串中所有字母异位词.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 48. 旋转图像.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 53. 最大子序和.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 547. 朋友圈.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 56. 合并区间.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 567. 字符串的排列.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 60. 第k个排列.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 674. 最长连续递增序列.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 69. x 的平方根.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 695. 岛屿的最大面积.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 70. 爬楼梯.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 71. 简化路径.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 76. 最小覆盖子串.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── 83. 删除排序链表中的重复元素.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 93. 复原IP地址.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
├── LeetCode
│ ├── LeetCode.xcodeproj
│ │ ├── project.pbxproj
│ │ ├── project.xcworkspace
│ │ │ ├── contents.xcworkspacedata
│ │ │ ├── xcshareddata
│ │ │ │ └── IDEWorkspaceChecks.plist
│ │ │ └── xcuserdata
│ │ │ │ └── sunkaifeng.xcuserdatad
│ │ │ │ └── UserInterfaceState.xcuserstate
│ │ └── xcuserdata
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── xcschemes
│ │ │ └── xcschememanagement.plist
│ ├── LeetCode
│ │ ├── AppDelegate.swift
│ │ ├── Assets.xcassets
│ │ │ ├── AppIcon.appiconset
│ │ │ │ └── Contents.json
│ │ │ └── Contents.json
│ │ ├── Base.lproj
│ │ │ ├── LaunchScreen.storyboard
│ │ │ └── Main.storyboard
│ │ ├── Info.plist
│ │ └── ViewController.swift
│ ├── LeetCodeTests
│ │ ├── Info.plist
│ │ └── LeetCodeTests.swift
│ └── LeetCodeUITests
│ │ ├── Info.plist
│ │ └── LeetCodeUITests.swift
├── gen.python
├── 剑指 Offer II 022. 链表中环的入口节点.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ ├── kent.sun.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 剑指 Offer II 022. 链表中环的入口节点的副本.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ ├── kent.sun.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
├── 剑指 Offer II 078. 合并排序链表.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ ├── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ │ ├── kent.sun.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ │ │ └── sunkaifeng.xcuserdatad
│ │ │ └── UserInterfaceState.xcuserstate
│ └── xcuserdata
│ │ └── kent.sun.xcuserdatad
│ │ └── xcschemes
│ │ └── xcschememanagement.plist
└── 面试金典
│ ├── 面试题 01.01. 判定字符是否唯一.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ ├── 面试题 01.02. 判定是否互为字符重排.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ ├── 面试题 01.03. URL化.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ └── xcuserdata
│ │ ├── kent.sun.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── 面试题 01.04. 回文排列.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ └── xcuserdata
│ ├── kent.sun.xcuserdatad
│ └── UserInterfaceState.xcuserstate
│ └── sunkaifeng.xcuserdatad
│ └── UserInterfaceState.xcuserstate
├── README.md
├── test.sh
└── 恋上数据结构
├── .DS_Store
├── day1
└── 斐波那契
│ └── fibonacciNumber.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ └── May.xcuserdatad
│ └── UserInterfaceState.xcuserstate
├── day2
├── .DS_Store
└── 链表
│ ├── .DS_Store
│ ├── LinkedList.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ │ ├── contents.xcworkspacedata
│ │ ├── xcshareddata
│ │ └── IDEWorkspaceChecks.plist
│ │ └── xcuserdata
│ │ └── sunkaifeng.xcuserdatad
│ │ └── UserInterfaceState.xcuserstate
│ └── LinkedList2.playground
│ ├── Contents.swift
│ ├── contents.xcplayground
│ └── playground.xcworkspace
│ ├── contents.xcworkspacedata
│ ├── xcshareddata
│ └── IDEWorkspaceChecks.plist
│ └── xcuserdata
│ └── sunkaifeng.xcuserdatad
│ └── UserInterfaceState.xcuserstate
└── day3
├── .DS_Store
└── 双向链表
├── .DS_Store
└── LinkedList.playground
├── Contents.swift
├── contents.xcplayground
└── playground.xcworkspace
├── contents.xcworkspacedata
├── xcshareddata
└── IDEWorkspaceChecks.plist
└── xcuserdata
└── sunkaifeng.xcuserdatad
└── UserInterfaceState.xcuserstate
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/.DS_Store
--------------------------------------------------------------------------------
/LeetCode/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/.DS_Store
--------------------------------------------------------------------------------
/LeetCode/0二叉堆实现.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/0二叉堆实现.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/0二叉堆实现.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/0二叉堆实现.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/0二叉堆实现.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/0二叉堆实现.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/1. 两数之和.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
4 | 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
5 | 示例:
6 | 给定 nums = [2, 7, 11, 15], target = 9
7 | 因为 nums[0] + nums[1] = 2 + 7 = 9
8 | 所以返回 [0, 1]
9 | 来源:力扣(LeetCode)
10 | 链接:https://leetcode-cn.com/problems/two-sum
11 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
12 | */
13 |
14 |
15 | class Solution {
16 | func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
17 | let lastArray:[Int] = []
18 | var dic:[Int:Int] = [:]
19 | for (i,object) in nums.enumerated() {
20 | let temp = target - nums[i]
21 | if dic.keys.contains(temp) {
22 | return [dic[temp] ?? 0 , i]
23 | }
24 | dic[object] = i
25 | }
26 | return lastArray
27 | }
28 | }
29 |
30 | //MARK: - 思路
31 | /*
32 | 对于这道题,首先想到的就是暴力方法,即使用两个for循环,遍历两次数组,看有没有和是目标值的。显然这样时间复杂度太大,O(n*n)
33 | 因此需要改良的算法,用到哈希查找的方法。
34 | 建立哈希表,从左向右扫描一遍,将整数与索引存放到map中。扫描一遍,对其中的每一个整数K,搜索 target-K 在map中是否存在即可。若存在,则输出 K 与 target-K 的下标即可。此算法的时间复杂度为O(n)
35 | */
36 |
37 | //MARK: - Test
38 | let s = Solution()
39 | s.twoSum([3,2,4], 6)
40 |
--------------------------------------------------------------------------------
/LeetCode/1. 两数之和.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/1. 两数之和.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/1. 两数之和.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/1. 两数之和.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/1. 两数之和.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/1. 两数之和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/1. 两数之和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/1. 两数之和.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 1. 两数之和 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/1013. 将数组分成和相等的三个部分.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个整数数组 A,只有我们可以将其划分为三个和相等的非空部分时才返回 true,否则返回 false。
4 |
5 | 形式上,如果我们可以找出索引 i+1 < j 且满足 (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) 就可以将数组三等分。
6 |
7 |
8 |
9 | 示例 1:
10 |
11 | 输出:[0,2,1,-6,6,-7,9,1,2,0,1]
12 | 输出:true
13 | 解释:0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
14 |
15 | 示例 2:
16 |
17 | 输入:[0,2,1,-6,6,7,9,-1,2,0,1]
18 | 输出:false
19 |
20 | 示例 3:
21 |
22 | 输入:[3,3,6,5,-2,2,5,1,-9,4]
23 | 输出:true
24 | 解释:3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
25 |
26 | 来源:力扣(LeetCode)
27 | 链接:https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum
28 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
29 | */
30 |
31 |
32 | class Solution {
33 | func canThreePartsEqualSum(_ A: [Int]) -> Bool {
34 | var sum = 0
35 | for i in A {
36 | sum += i
37 | }
38 | var key = sum / 3
39 | var temp = 0
40 | for i in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/1013. 将数组分成和相等的三个部分.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/1013. 将数组分成和相等的三个部分.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/1013. 将数组分成和相等的三个部分.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/1013. 将数组分成和相等的三个部分.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/1094. 拼车.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 车上最初有 capacity 个空座位。车 只能 向一个方向行驶(也就是说,不允许掉头或改变方向)
4 |
5 | 给定整数 capacity 和一个数组 trips , trip[i] = [numPassengersi, fromi, toi] 表示第 i 次旅行有 numPassengersi 乘客,接他们和放他们的位置分别是 fromi 和 toi 。这些位置是从汽车的初始位置向东的公里数。
6 |
7 | 当且仅当你可以在所有给定的行程中接送所有乘客时,返回 true,否则请返回 false。
8 |
9 |
10 |
11 | 示例 1:
12 |
13 | 输入:trips = [[2,1,5],[3,3,7]], capacity = 4
14 | 输出:false
15 | 示例 2:
16 |
17 | 输入:trips = [[2,1,5],[3,3,7]], capacity = 5
18 | 输出:true
19 |
20 |
21 | 提示:
22 |
23 | 1 <= trips.length <= 1000
24 | trips[i].length == 3
25 | 1 <= numPassengersi <= 100
26 | 0 <= fromi < toi <= 1000
27 | 1 <= capacity <= 105
28 |
29 |
30 | 来源:力扣(LeetCode)
31 | 链接:https://leetcode.cn/problems/car-pooling
32 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
33 | */
34 |
35 | class Solution {
36 | func carPooling(_ trips: [[Int]], _ capacity: Int) -> Bool {
37 | var origin = [Int](repeating: 0, count: 1001)
38 | let diff = Difference()
39 | diff.difference(origin)
40 | for value in trips {
41 | diff.increment(value[1], value[2] - 1,value[0])
42 | }
43 | var res = diff.result()
44 |
45 | for i in res {
46 | if capacity < i {
47 | return false
48 | }
49 | }
50 | return true
51 | }
52 | }
53 |
54 |
55 | class Difference {
56 | var diff: [Int] = []
57 | func difference(_ nums: [Int]) {
58 | if nums.isEmpty {
59 | return
60 | }
61 | diff = [Int](repeating: 0, count: nums.count)
62 | diff[0] = nums[0]
63 | for i in 1.. [Int] {
76 | var res: [Int] = [Int](repeating: 0, count: diff.count)
77 | res[0] = diff[0]
78 | for i in 1..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/1094. 拼车.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/1094. 拼车.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/1094. 拼车.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/1094. 拼车.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/1094. 拼车.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/11. 盛最多水的容器.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 11. 盛最多水的容器
4 | 给定一个长度为 n 的整数数组 height 。有 n 条垂线,第 i 条线的两个端点是 (i, 0) 和 (i, height[i]) 。
5 |
6 | 找出其中的两条线,使得它们与 x 轴共同构成的容器可以容纳最多的水。
7 |
8 | 返回容器可以储存的最大水量。
9 |
10 | 说明:你不能倾斜容器。
11 |
12 |
13 |
14 | 示例 1:
15 |
16 |
17 |
18 | 输入:[1,8,6,2,5,4,8,3,7]
19 | 输出:49
20 | 解释:图中垂直线代表输入数组 [1,8,6,2,5,4,8,3,7]。在此情况下,容器能够容纳水(表示为蓝色部分)的最大值为 49。
21 | 示例 2:
22 |
23 | 输入:height = [1,1]
24 | 输出:1
25 |
26 |
27 | 提示:
28 |
29 | n == height.length
30 | 2 <= n <= 105
31 | 0 <= height[i] <= 104
32 | */
33 |
34 |
35 | class Solution {
36 | func maxArea(_ height: [Int]) -> Int {
37 | var left = 0, right = height.count - 1
38 | var maxWater = 0
39 | while left < right {
40 | maxWater = max(maxWater, (right - left) * min(height[left], height[right]))
41 | if height[left] < height[right] {
42 | left += 1
43 | } else {
44 | right -= 1
45 | }
46 | }
47 | return maxWater
48 | }
49 | }
50 |
51 | //MARK: - 思路
52 | /*
53 | 类似木桶装水 最多取决于最矮那块板子
54 | time: o(n)
55 | space:o(1)
56 | */
57 |
58 |
59 | let kkk = Array.init(repeating: <#T##_#>, count: <#T##Int#>)
60 |
--------------------------------------------------------------------------------
/LeetCode/11. 盛最多水的容器.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/11. 盛最多水的容器.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/11. 盛最多水的容器.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/11. 盛最多水的容器.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/11. 盛最多水的容器.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/11. 盛最多水的容器.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/1109. 航班预订统计.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 这里有 n 个航班,它们分别从 1 到 n 进行编号。
4 |
5 | 有一份航班预订表 bookings ,表中第 i 条预订记录 bookings[i] = [firsti, lasti, seatsi] 意味着在从 firsti 到 lasti (包含 firsti 和 lasti )的 每个航班 上预订了 seatsi 个座位。
6 |
7 | 请你返回一个长度为 n 的数组 answer,里面的元素是每个航班预定的座位总数。
8 |
9 |
10 |
11 | 示例 1:
12 |
13 | 输入:bookings = [[1,2,10],[2,3,20],[2,5,25]], n = 5
14 | 输出:[10,55,45,25,25]
15 | 解释:
16 | 航班编号 1 2 3 4 5
17 | 预订记录 1 : 10 10
18 | 预订记录 2 : 20 20
19 | 预订记录 3 : 25 25 25 25
20 | 总座位数: 10 55 45 25 25
21 | 因此,answer = [10,55,45,25,25]
22 | 示例 2:
23 |
24 | 输入:bookings = [[1,2,10],[2,2,15]], n = 2
25 | 输出:[10,25]
26 | 解释:
27 | 航班编号 1 2
28 | 预订记录 1 : 10 10
29 | 预订记录 2 : 15
30 | 总座位数: 10 25
31 | 因此,answer = [10,25]
32 |
33 |
34 | 提示:
35 |
36 | 1 <= n <= 2 * 104
37 | 1 <= bookings.length <= 2 * 104
38 | bookings[i].length == 3
39 | 1 <= firsti <= lasti <= n
40 | 1 <= seatsi <= 104
41 |
42 | 来源:力扣(LeetCode)
43 | 链接:https://leetcode.cn/problems/corporate-flight-bookings
44 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
45 | */
46 |
47 | class Solution {
48 | func corpFlightBookings(_ bookings: [[Int]], _ n: Int) -> [Int] {
49 | let origin = [Int](repeating: 0, count: n)
50 | let diff = Difference()
51 | diff.difference(origin)
52 | for value in bookings {
53 | diff.increment(value[0] - 1, value[1] - 1, value[2])
54 | }
55 | return diff.result()
56 | }
57 | }
58 |
59 | class Difference {
60 | var diff: [Int] = []
61 | func difference(_ nums: [Int]) {
62 | if nums.isEmpty {
63 | return
64 | }
65 | diff = [Int](repeating: 0, count: nums.count)
66 | diff[0] = nums[0]
67 | for i in 1.. [Int] {
80 | var res: [Int] = [Int](repeating: 0, count: diff.count)
81 | res[0] = diff[0]
82 | for i in 1..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/1109. 航班预订统计.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/1109. 航班预订统计.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/1109. 航班预订统计.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/1109. 航班预订统计.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/1109. 航班预订统计.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/120. 三角形最小路径和.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个三角形,找出自顶向下的最小路径和。每一步只能移动到下一行中相邻的结点上。
4 |
5 | 例如,给定三角形:
6 |
7 | [
8 | [2],
9 | [3,4],
10 | [6,5,7],
11 | [4,1,8,3]
12 | ]
13 |
14 | 自顶向下的最小路径和为 11(即,2 + 3 + 5 + 1 = 11)。
15 |
16 | 说明:
17 |
18 | 如果你可以只使用 O(n) 的额外空间(n 为三角形的总行数)来解决这个问题,那么你的算法会很加分。
19 |
20 | 来源:力扣(LeetCode)
21 | 链接:https://leetcode-cn.com/problems/triangle
22 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23 | */
24 |
25 | class Solution {
26 | func minimumTotal(_ triangle: [[Int]]) -> Int {
27 | var resultTriangle = triangle
28 | let count = resultTriangle.count
29 | if count < 1 {
30 | return 0
31 | }
32 | if count == 1 {
33 | return resultTriangle[0][0]
34 | }
35 | var i = count - 2
36 | while i >= 0 {
37 | for j in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/120. 三角形最小路径和.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/120. 三角形最小路径和.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/120. 三角形最小路径和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/120. 三角形最小路径和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/121. 买卖股票的最佳时机.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
4 |
5 | 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。
6 |
7 | 注意你不能在买入股票前卖出股票。
8 |
9 | 示例 1:
10 |
11 | 输入: [7,1,5,3,6,4]
12 | 输出: 5
13 | 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
14 | 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。
15 |
16 | 示例 2:
17 |
18 | 输入: [7,6,4,3,1]
19 | 输出: 0
20 | 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
21 |
22 | 在真实的面试中遇到过这道题?
23 |
24 | 来源:力扣(LeetCode)
25 | 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock
26 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
27 | */
28 |
29 |
30 | class Solution {
31 | func maxProfit(_ prices: [Int]) -> Int {
32 | var minPrice = Int.max
33 | var maxProfit = 0
34 | for i in prices {
35 | if i < minPrice {
36 | minPrice = i
37 | } else if i - minPrice > maxProfit {
38 | maxProfit = i - minPrice
39 | }
40 | }
41 | return maxProfit
42 | }
43 | }
44 |
45 | //MARK: - 思路
46 | /*
47 | 思想: 需要找到最小的谷之后的最大的峰。 我们可以维持两个变量——minprice 和 maxprofit,它们分别对应迄今为止所得到的最小的谷值和最大的利润(卖出价格与最低价格之间的最大差值)。
48 |
49 | 时间复杂度:O(n)
50 | */
51 |
--------------------------------------------------------------------------------
/LeetCode/121. 买卖股票的最佳时机.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/121. 买卖股票的最佳时机.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/121. 买卖股票的最佳时机.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/121. 买卖股票的最佳时机.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/121. 买卖股票的最佳时机.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/122. 买卖股票的最佳时机 II.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。
4 | 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。
5 | 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
6 | 示例 1:
7 | 输入: [7,1,5,3,6,4]
8 | 输出: 7
9 | 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
10 | 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。
11 | 示例 2:
12 | 输入: [1,2,3,4,5]
13 | 输出: 4
14 | 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。
15 | 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。
16 | 因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。
17 | 示例 3:
18 | 输入: [7,6,4,3,1]
19 | 输出: 0
20 | 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。
21 | 在真实的面试中遇到过这道题?
22 |
23 | 来源:力扣(LeetCode)
24 | 链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii
25 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
26 | */
27 |
28 |
29 | class Solution {
30 | func maxProfit(_ prices: [Int]) -> Int {
31 | if prices.count == 0 || prices.count == 1 {
32 | return 0
33 | }
34 | var max = 0
35 | for i in 1.. prices[i - 1]{
37 | max += prices[i] - prices[i - 1]
38 | }
39 | }
40 | return max
41 | }
42 | }
43 |
44 | //MARK: - 思路
45 | /*
46 | 考虑买股票的策略:设今天价格p1,明天价格p2,若p1 < p2则今天买入明天卖出,赚取p2 - p1;
47 |
48 | 若遇到连续上涨的交易日,第一天买最后一天卖收益最大,等价于每天买卖(因为没有交易手续费);
49 | 遇到价格下降的交易日,不买卖,因此永远不会亏钱。
50 |
51 | 赚到了所有交易日的钱,所有亏钱的交易日都未交易,理所当然会利益最大化。
52 |
53 | 时间复杂度:O(n)
54 | */
55 |
--------------------------------------------------------------------------------
/LeetCode/122. 买卖股票的最佳时机 II.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/122. 买卖股票的最佳时机 II.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/122. 买卖股票的最佳时机 II.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/122. 买卖股票的最佳时机 II.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/122. 买卖股票的最佳时机 II.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/14. 最长公共前缀.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 编写一个函数来查找字符串数组中的最长公共前缀。
4 |
5 | 如果不存在公共前缀,返回空字符串 ""。
6 |
7 | 示例 1:
8 |
9 | 输入: ["flower","flow","flight"]
10 | 输出: "fl"
11 |
12 | 示例 2:
13 |
14 | 输入: ["dog","racecar","car"]
15 | 输出: ""
16 | 解释: 输入不存在公共前缀。
17 |
18 | 说明:
19 |
20 | 所有输入只包含小写字母 a-z 。
21 |
22 | 来源:力扣(LeetCode)
23 | 链接:https://leetcode-cn.com/problems/longest-common-prefix
24 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
25 | */
26 |
27 |
28 |
29 |
30 | class Solution {
31 | func longestCommonPrefix(_ strs: [String]) -> String {
32 | if strs.count == 0 {
33 | return ""
34 | }
35 | var reslut = strs[0]
36 | for i in 1..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/14. 最长公共前缀.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/14. 最长公共前缀.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/14. 最长公共前缀.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/14. 最长公共前缀.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/141. 环形链表.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个链表,判断链表中是否有环。
4 |
5 | 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
6 |
7 |
8 |
9 | 示例 1:
10 |
11 | 输入:head = [3,2,0,-4], pos = 1
12 | 输出:true
13 | 解释:链表中有一个环,其尾部连接到第二个节点。
14 |
15 | 示例 2:
16 |
17 | 输入:head = [1,2], pos = 0
18 | 输出:true
19 | 解释:链表中有一个环,其尾部连接到第一个节点。
20 |
21 |
22 |
23 | 来源:力扣(LeetCode)
24 | 链接:https://leetcode-cn.com/problems/linked-list-cycle
25 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
26 | */
27 |
28 | class ListNode : Equatable where T : Equatable {
29 | static func == (lhs: ListNode, rhs: ListNode) -> Bool {
30 | return lhs.element == rhs.element && lhs.next == rhs.next
31 | }
32 | var element:T!
33 | var next:ListNode!
34 | init(_ element : T, next:ListNode? = nil) {
35 | self.element = element
36 | self.next = next
37 | }
38 | }
39 | // 思路,快慢指针
40 | class Solution where T : Equatable {
41 | /// 判断链表有环
42 | func hasCycle(_ head: ListNode?) -> Bool {
43 | if (head == nil || head?.next == nil) {
44 | return false
45 | }
46 |
47 | var slow = head
48 | var fast = head?.next
49 | while fast != nil && fast?.next != nil {
50 | if fast == slow {
51 | return true
52 | }
53 | slow = slow?.next
54 | fast = fast?.next?.next
55 | }
56 | return false
57 | }
58 | }
59 |
60 |
--------------------------------------------------------------------------------
/LeetCode/141. 环形链表.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/141. 环形链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/141. 环形链表.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/141. 环形链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/141. 环形链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/141. 环形链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/141. 环形链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/141. 环形链表.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 141. 环形链表 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/142. 环形链表 II.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个链表,返回链表开始入环的第一个节点。 如果链表无环,则返回 null。
4 | 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。
5 | 说明:不允许修改给定的链表。
6 | 示例 1:
7 | 输入:head = [3,2,0,-4], pos = 1
8 | 输出:tail connects to node index 1
9 | 解释:链表中有一个环,其尾部连接到第二个节点。
10 | 示例 2:
11 |
12 | 输入:head = [1,2], pos = 0
13 | 输出:tail connects to node index 0
14 | 解释:链表中有一个环,其尾部连接到第一个节点。
15 |
16 | 示例 3:
17 |
18 | 输入:head = [1], pos = -1
19 | 输出:no cycle
20 | 解释:链表中没有环。
21 |
22 | 来源:力扣(LeetCode)
23 | 链接:https://leetcode-cn.com/problems/linked-list-cycle-ii
24 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
25 | */
26 |
27 | class ListNode : Equatable where T : Equatable {
28 | static func == (lhs: ListNode, rhs: ListNode) -> Bool {
29 | return lhs.element == rhs.element && lhs.next == rhs.next
30 | }
31 | var element:T!
32 | var next:ListNode!
33 | init(_ element : T, next:ListNode? = nil) {
34 | self.element = element
35 | self.next = next
36 | }
37 | }
38 |
39 | class Solution where T : Equatable {
40 | func detectCycle(_ head: ListNode?) -> ListNode? {
41 | if head == nil || head?.next == nil {
42 | return nil
43 | }
44 | // 第一步,快慢指针,判断是否有环
45 | var slow = head
46 | var fast = head?.next
47 | while fast != nil && fast?.next != nil {
48 | fast = fast?.next.next
49 | slow = slow?.next
50 | if fast == slow {
51 | break
52 | }
53 | }
54 | if fast != slow {
55 | return nil
56 | }
57 | // 说明有环
58 | slow = head
59 | while fast != slow {
60 | fast = fast?.next
61 | slow = slow?.next
62 | }
63 | return fast
64 | }
65 | }
66 |
67 |
68 | /// 思路
69 | /*
70 |
71 | */
72 |
--------------------------------------------------------------------------------
/LeetCode/142. 环形链表 II.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/142. 环形链表 II.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/142. 环形链表 II.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/142. 环形链表 II.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/142. 环形链表 II.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/146. LRU缓存机制.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/146. LRU缓存机制.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/146. LRU缓存机制.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/146. LRU缓存机制.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/146. LRU缓存机制.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/146. LRU缓存机制.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/146. LRU缓存机制.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/148. 排序链表.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 在 O(n log n) 时间复杂度和常数级空间复杂度下,对链表进行排序。
4 |
5 | 示例 1:
6 |
7 | 输入: 4->2->1->3
8 | 输出: 1->2->3->4
9 |
10 | 示例 2:
11 |
12 | 输入: -1->5->3->4->0
13 | 输出: -1->0->3->4->5
14 |
15 | 来源:力扣(LeetCode)
16 | 链接:https://leetcode-cn.com/problems/sort-list
17 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
18 | */
19 |
20 |
21 | public class ListNode {
22 | public var val: Int
23 | public var next: ListNode?
24 | public init(_ val: Int) {
25 | self.val = val
26 | self.next = nil
27 | }
28 | }
29 | class Solution {
30 | func sortList(_ head: ListNode?) -> ListNode? {
31 | if head == nil {
32 | return nil
33 | }
34 | var arr:[Int] = []
35 | var node = head
36 | while node != nil {
37 | arr.append(node!.val)
38 | node = node?.next
39 | }
40 | arr = arr.sorted()
41 | let heaeNew = ListNode.init(arr[0])
42 | var tmpNode:ListNode? = heaeNew
43 | for i in 1..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/148. 排序链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/148. 排序链表.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/148. 排序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/148. 排序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/148. 排序链表.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 148. 排序链表 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/15. 三数之和.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
4 |
5 | 注意:答案中不可以包含重复的三元组。
6 |
7 | 例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
8 |
9 | 满足要求的三元组集合为:
10 | [
11 | [-1, 0, 1],
12 | [-1, -1, 2]
13 | ]
14 |
15 | 来源:力扣(LeetCode)
16 | 链接:https://leetcode-cn.com/problems/3sum
17 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
18 | */
19 |
20 |
21 | class Solution {
22 | func threeSum(_ nums: [Int]) -> [[Int]] {
23 | if nums.count <= 2 {
24 | return [[Int]]()
25 | }
26 | var tNums = nums.sorted()
27 | var res = [[Int]]()
28 | for i in 0.. 0 && tNums[i] != tNums[i-1]) {
31 | //加入两个指针,头指针从 i + 1 开始,防止加入重复的元素
32 | var head = i + 1
33 | var tail = tNums.count - 1
34 | let sum = 0 - tNums[i]
35 | while head < tail {
36 | if (tNums[head] + tNums[tail] == sum) {
37 | res.append([tNums[i], tNums[head], tNums[tail]])
38 | //相同元素要后移,防止加入重复的 list
39 | while head < tail && tNums[head] == tNums[head + 1] {
40 | head += 1
41 | }
42 | while head < tail && tNums[tail] == tNums[tail - 1] {
43 | tail -= 1
44 | }
45 | head += 1
46 | tail -= 1
47 | } else if (tNums[head] + tNums[tail] < sum) {
48 | head += 1
49 | } else {
50 | tail -= 1
51 | }
52 | }
53 | }
54 | }
55 | return res
56 | }
57 | }
58 |
59 | //MARK: - 思路
60 | /*
61 | 主要思想是,遍历数组,用 0 减去当前的 object ,作为 sum ,然后再找两个 object 使得和为 sum
62 | 这样遍历需要 O(n),再找两个数需要 O(n²)的复杂度还是需要 O(n³)。
63 | 所以可以将给定的 num 排序, 然后用两个指针,一个指向头,一个指向尾,去找这两个 object ,这样的话,找另外两个数时间复杂度就会从 O(n²),降到 O(n)。
64 | */
65 |
66 | //MARK: - Test
67 |
68 | /*
69 | 1、双指针法
70 | setp:
71 | 1 数组排序,定义 res
72 | 2 定义左右指针
73 | 3 定义要return的 special condition
74 | 4 如果 i + left + right < 0, 左指针右移,i + left + right > 0, 右指针左移
75 | 5 如果左右重复元素,跳过
76 | */
77 |
--------------------------------------------------------------------------------
/LeetCode/15. 三数之和.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/15. 三数之和.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/15. 三数之和.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/15. 三数之和.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/15. 三数之和.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/15. 三数之和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/15. 三数之和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/151. 反转字符串中的单词.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给你一个字符串 s ,请你反转字符串中 单词 的顺序。
4 |
5 | 单词 是由非空格字符组成的字符串。s 中使用至少一个空格将字符串中的 单词 分隔开。
6 |
7 | 返回 单词 顺序颠倒且 单词 之间用单个空格连接的结果字符串。
8 |
9 | 注意:输入字符串 s中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。
10 |
11 |
12 |
13 | 示例 1:
14 |
15 | 输入:s = "the sky is blue"
16 | 输出:"blue is sky the"
17 | 示例 2:
18 |
19 | 输入:s = " hello world "
20 | 输出:"world hello"
21 | 解释:反转后的字符串中不能存在前导空格和尾随空格。
22 | 示例 3:
23 |
24 | 输入:s = "a good example"
25 | 输出:"example good a"
26 | 解释:如果两个单词间有多余的空格,反转后的字符串需要将单词间的空格减少到仅有一个。
27 |
28 | 来源:力扣(LeetCode)
29 | 链接:https://leetcode.cn/problems/reverse-words-in-a-string
30 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
31 | */
32 |
33 | class Solution {
34 | func reverseWords(_ s: String) -> String {
35 | let kkk = removeSBank(s)
36 | var s = Array(kkk).map{"\($0)"}
37 | let n = s.count
38 | reverse(&s, 0, s.count - 1)
39 | var i = 0
40 | while i < n {
41 | for j in i.. String {
53 | let s = Array(s)
54 | var res: [String] = []
55 | for i in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/151. 反转字符串中的单词.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/151. 反转字符串中的单词.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/151. 反转字符串中的单词.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/151. 反转字符串中的单词.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/151. 反转字符串中的单词.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/155. 最小栈.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。
4 |
5 | push(x) -- 将元素 x 推入栈中。
6 | pop() -- 删除栈顶的元素。
7 | top() -- 获取栈顶元素。
8 | getMin() -- 检索栈中的最小元素。
9 |
10 | 示例:
11 |
12 | MinStack minStack = new MinStack();
13 | minStack.push(-2);
14 | minStack.push(0);
15 | minStack.push(-3);
16 | minStack.getMin(); --> 返回 -3.
17 | minStack.pop();
18 | minStack.top(); --> 返回 0.
19 | minStack.getMin(); --> 返回 -2.
20 |
21 | 来源:力扣(LeetCode)
22 | 链接:https://leetcode-cn.com/problems/min-stack
23 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
24 | */
25 |
26 |
27 | class MinStack {
28 | var stack: [Int]
29 | var minStack: [Int]
30 | /** initialize your data structure here. */
31 | init() {
32 | stack = []
33 | minStack = []
34 | }
35 |
36 | func push(_ x: Int) {
37 | stack.append(x)
38 | if minStack.count == 0 {
39 | minStack.append(x)
40 | } else {
41 | minStack.append(min(x, minStack[minStack.count - 1]))
42 | }
43 | }
44 |
45 | func pop() {
46 | stack.removeLast()
47 | minStack.removeLast()
48 | }
49 |
50 | func top() -> Int {
51 | return stack[stack.count - 1]
52 | }
53 |
54 | func getMin() -> Int {
55 | return minStack[minStack.count - 1]
56 | }
57 | }
58 |
--------------------------------------------------------------------------------
/LeetCode/155. 最小栈.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/155. 最小栈.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/155. 最小栈.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/155. 最小栈.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/155. 最小栈.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/160. 相交链表.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 编写一个程序,找到两个单链表相交的起始节点。
4 |
5 | 如下面的两个链表:
6 |
7 | 在节点 c1 开始相交。
8 |
9 |
10 |
11 | 示例 1:
12 |
13 | 输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
14 | 输出:Reference of the node with value = 8
15 | 输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。
16 |
17 |
18 |
19 | 示例 2:
20 |
21 | 输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
22 | 输出:Reference of the node with value = 2
23 | 输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
24 |
25 |
26 |
27 | 示例 3:
28 |
29 | 输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
30 | 输出:null
31 | 输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
32 | 解释:这两个链表不相交,因此返回 null。
33 |
34 |
35 |
36 | 注意:
37 |
38 | 如果两个链表没有交点,返回 null.
39 | 在返回结果后,两个链表仍须保持原有的结构。
40 | 可假定整个链表结构中没有循环。
41 | 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
42 |
43 | 来源:力扣(LeetCode)
44 | 链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists
45 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
46 | */
47 | class Solution {
48 | func getIntersectionNode(_ headA: ListNode?, _ headB: ListNode?) -> ListNode? {
49 | var p1 = headA, p2 = headB
50 | while p1 !== p2 {
51 | if p1 == nil {
52 | p1 = headB
53 | } else {
54 | p1 = p1?.next
55 | }
56 | if p2 == nil {
57 | p2 = headA
58 | } else {
59 | p2 = p2?.next
60 | }
61 |
62 | }
63 |
64 | return p1
65 | }ß
66 | }
67 |
--------------------------------------------------------------------------------
/LeetCode/160. 相交链表.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/160. 相交链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/160. 相交链表.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/160. 相交链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/160. 相交链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/160. 相交链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/160. 相交链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/176. 第二高的薪水.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 编写一个 SQL 查询,获取 Employee 表中第二高的薪水(Salary) 。
4 |
5 | +----+--------+
6 | | Id | Salary |
7 | +----+--------+
8 | | 1 | 100 |
9 | | 2 | 200 |
10 | | 3 | 300 |
11 | +----+--------+
12 |
13 | 例如上述 Employee 表,SQL查询应该返回 200 作为第二高的薪水。如果不存在第二高的薪水,那么查询应返回 null。
14 |
15 | +---------------------+
16 | | SecondHighestSalary |
17 | +---------------------+
18 | | 200 |
19 | +---------------------+
20 |
21 |
22 | 来源:力扣(LeetCode)
23 | 链接:https://leetcode-cn.com/problems/second-highest-salary
24 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
25 | */
26 |
27 | /*
28 | 题解
29 | // max(字段名) # 返回该字段的最大值
30 | select max(Salary) as SecondHighestSalary
31 | from Employee
32 | where Salary < (
33 | select max(Salary) from Employee
34 | )
35 | */
36 |
37 |
--------------------------------------------------------------------------------
/LeetCode/176. 第二高的薪水.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/176. 第二高的薪水.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/176. 第二高的薪水.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/176. 第二高的薪水.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/176. 第二高的薪水.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/2. 两数相加.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。
4 | 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。
5 | 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。
6 | 示例:
7 | 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4)
8 | 输出:7 -> 0 -> 8
9 | 原因:342 + 465 = 807
10 | 来源:力扣(LeetCode)
11 | 链接:https://leetcode-cn.com/problems/add-two-numbers
12 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
13 | */
14 |
15 |
16 | public class ListNode {
17 | public var val: Int
18 | public var next: ListNode?
19 | public init(_ val: Int) {
20 | self.val = val
21 | self.next = nil
22 | }
23 | }
24 |
25 | class Solution {
26 | func addTwoNumbers(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
27 | let result = ListNode(0)
28 | var carrry = 0
29 | var head = result
30 | var l1 = l1
31 | var l2 = l2
32 | while l1 != nil || l2 != nil {
33 | let sum = l1?.val ?? 0 + (l2?.val ?? 0) + carrry
34 | carrry = sum / 10
35 | head.next = ListNode(sum % 10)
36 | head = head.next!
37 | l1 = l1?.next
38 | l2 = l2?.next
39 | }
40 | while carrry > 0 {
41 | head.next = ListNode(carrry)
42 | }
43 | return result.next
44 | }
45 | }
46 |
47 | // 思路
48 |
49 | /*
50 | 1.遍历两个表node
51 | 2.计算新表node点sum value
52 | 3.由于第一个节点不需要计算carry值,导致逻辑不一致,故先默认创建一个初始node
53 | 4.最后添加进位产生的新node
54 | 5.返回新建node.next指向的表(不包含初始node)
55 | */
56 |
--------------------------------------------------------------------------------
/LeetCode/2. 两数相加.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/2. 两数相加.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/2. 两数相加.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/2. 两数相加.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/2. 两数相加.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/203. 移除链表元素.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 删除链表中等于给定值 val 的所有节点。
4 |
5 | 示例:
6 |
7 | 输入: 1->2->6->3->4->5->6, val = 6
8 | 输出: 1->2->3->4->5
9 |
10 | 来源:力扣(LeetCode)
11 | 链接:https://leetcode-cn.com/problems/remove-linked-list-elements
12 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
13 | */
14 |
15 |
16 | public class ListNode {
17 | public var val: Int
18 | public var next: ListNode?
19 | public init(_ val: Int) {
20 | self.val = val
21 | self.next = nil
22 | }
23 | }
24 |
25 |
26 | class Solution {
27 | func removeElements(_ head: ListNode?, _ val: Int) -> ListNode? {
28 | if head == nil {
29 | return head
30 | }
31 | if head?.next == nil {
32 | if head?.val == val {
33 | return head?.next
34 | }
35 | return head
36 | }
37 | var slow = head
38 | var fast = head?.next
39 | while fast != nil {
40 | if fast?.val == val {
41 | fast = fast?.next
42 | slow?.next = fast
43 | } else {
44 | fast = fast?.next
45 | slow = slow?.next
46 | }
47 | }
48 | if head?.val == val {
49 | return head?.next
50 | }
51 | return head
52 | }
53 | }
54 |
55 | //MARK: - 思路
56 | // 快慢指针
57 |
58 |
--------------------------------------------------------------------------------
/LeetCode/203. 移除链表元素.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/203. 移除链表元素.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/203. 移除链表元素.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/203. 移除链表元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/203. 移除链表元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/206. 反转链表.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 反转一个单链表。
4 |
5 | 示例:
6 |
7 | 输入: 1->2->3->4->5->NULL
8 | 输出: 5->4->3->2->1->NULL
9 |
10 | 进阶:
11 | 你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
12 |
13 | 来源:力扣(LeetCode)
14 | 链接:https://leetcode-cn.com/problems/reverse-linked-list
15 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
16 | */
17 |
18 |
19 | public struct ListNode {
20 | public var val: Int
21 | public var next: ListNode?
22 | public init(_ val: Int) {
23 | self.val = val
24 | self.next = nil
25 | }
26 | }
27 |
28 | class Solution {
29 | /// 递归反转
30 | func reverseList(_ head: ListNode?) -> ListNode? {
31 | if (head == nil || head?.next == nil) {
32 | return head
33 | }
34 | let newHead = reverseList(head?.next)
35 | head?.next?.next = head
36 | head?.next = nil
37 | return newHead
38 | }
39 |
40 | /// 非递归
41 | func reverseListTwo(_ head: ListNode?) -> ListNode? {
42 | if (head == nil || head?.next == nil) {
43 | return head
44 | }
45 | var hHead = head
46 | var newHead: ListNode? = nil
47 | while hHead != nil {
48 | let tmp = hHead?.next
49 | hHead?.next = newHead
50 | newHead = hHead
51 | hHead = tmp
52 | }
53 | return newHead
54 | }
55 |
56 | }
57 |
58 | extension ListNode: Equatable {
59 | equalTo(self: <#T##CGPoint#>, <#T##CGPoint#>)
60 | }
61 |
--------------------------------------------------------------------------------
/LeetCode/206. 反转链表.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/206. 反转链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/206. 反转链表.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/206. 反转链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/206. 反转链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/206. 反转链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/206. 反转链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/206. 反转链表.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 206. 反转链表 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/21. 合并两个有序链表.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。
4 |
5 | 示例:
6 |
7 | 输入:1->2->4, 1->3->4
8 | 输出:1->1->2->3->4->4
9 |
10 |
11 | 来源:力扣(LeetCode)
12 | 链接:https://leetcode-cn.com/problems/merge-two-sorted-lists
13 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
14 | */
15 |
16 |
17 | public class ListNode {
18 | public var val: Int
19 | public var next: ListNode?
20 | public init(_ val: Int) {
21 | self.val = val
22 | self.next = nil
23 | }
24 | }
25 | class Solution {
26 | func mergeTwoLists(_ l1: ListNode?, _ l2: ListNode?) -> ListNode? {
27 | if l1 == nil {
28 | return l2
29 | }
30 | if l2 == nil {
31 | return l1
32 | }
33 | var arr:[Int] = []
34 | var node1 = l1
35 | while node1 != nil {
36 | arr.append(node1!.val)
37 | node1 = node1?.next
38 | }
39 | var node2 = l2
40 | while node2 != nil {
41 | arr.append(node2!.val)
42 | node2 = node2?.next
43 | }
44 | arr = arr.sorted()
45 | let head = ListNode.init(arr[0])
46 | var node:ListNode? = head
47 | for i in 1..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/21. 合并两个有序链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/21. 合并两个有序链表.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/21. 合并两个有序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/21. 合并两个有序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/215. 数组中的第K个最大元素.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
4 |
5 | 示例 1:
6 |
7 | 输入: [3,2,1,5,6,4] 和 k = 2
8 | 输出: 5
9 |
10 | 示例 2:
11 |
12 | 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4
13 | 输出: 4
14 |
15 | 说明:
16 |
17 | 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。
18 |
19 | 来源:力扣(LeetCode)
20 | 链接:https://leetcode-cn.com/problems/kth-largest-element-in-an-array
21 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
22 | */
23 |
24 | class Solution {
25 | func findKthLargest(_ nums: [Int], _ k: Int) -> Int {
26 | var array = nums.sorted()
27 | return array[nums.count - k]
28 | }
29 | }
30 | //MARK: - 思路
31 | /*
32 | */
33 |
34 |
--------------------------------------------------------------------------------
/LeetCode/215. 数组中的第K个最大元素.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/215. 数组中的第K个最大元素.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/215. 数组中的第K个最大元素.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/215. 数组中的第K个最大元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/215. 数组中的第K个最大元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/221. 最大正方形.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 在一个由 0 和 1 组成的二维矩阵内,找到只包含 1 的最大正方形,并返回其面积。
4 | 示例:
5 | 输入:
6 |
7 | 1 0 1 0 0
8 | 1 0 1 1 1
9 | 1 1 1 1 1
10 | 1 0 0 1 0
11 |
12 | 输出: 4
13 |
14 | 来源:力扣(LeetCode)
15 | 链接:https://leetcode-cn.com/problems/maximal-square
16 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
17 | */
18 |
19 |
20 | class Solution {
21 | func maximalSquare(_ matrix: [[Character]]) -> Int {
22 | guard matrix.count > 0, matrix[0].count > 0 else { return 0 }
23 | let m = matrix.count
24 | let n = matrix[0].count
25 | var dp = Array(repeating: Array(repeating: 0, count: n+1), count: m+1)
26 | var maxsqlen = 0
27 | for i in 1...m {
28 | for j in 1...n {
29 | if matrix[i-1][j-1] == "1" {
30 | dp[i][j] = min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1]) + 1
31 | maxsqlen = max(maxsqlen, dp[i][j])
32 | }
33 | }
34 | }
35 | return maxsqlen * maxsqlen
36 | }
37 | }
38 | //MARK: - 思路
39 | /*
40 | 我们用 0 初始化另一个矩阵 dp,维数和原始矩阵维数相同;
41 | dp(i,j) 表示的是由 1 组成的最大正方形的边长;
42 | 从 (0,0)(0,0)(0,0) 开始,对原始矩阵中的每一个 1,我们将当前元素的值更新为
43 |
44 | dp(i, j)= min(min(dp[i-1][j], dp[i][j-1]), dp[i-1][j-1])
45 |
46 | 我们还用一个变量记录当前出现的最大边长,这样遍历一次,找到最大的正方形边长 maxsqlen,那么结果就是 maxsqlen * maxsqlen 。
47 | */
48 |
--------------------------------------------------------------------------------
/LeetCode/221. 最大正方形.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/221. 最大正方形.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/221. 最大正方形.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/221. 最大正方形.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/221. 最大正方形.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/23. 合并K个排序链表.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 合并 k 个排序链表,返回合并后的排序链表。请分析和描述算法的复杂度。
4 | 示例:
5 | 输入:
6 | [
7 | 1->4->5,
8 | 1->3->4,
9 | 2->6
10 | ]
11 | 输出: 1->1->2->3->4->4->5->6
12 |
13 | 来源:力扣(LeetCode)
14 | 链接:https://leetcode-cn.com/problems/merge-k-sorted-lists
15 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
16 | */
17 |
18 |
19 | public class ListNode {
20 | public var val: Int
21 | public var next: ListNode?
22 | public init(_ val: Int) {
23 | self.val = val
24 | self.next = nil
25 | }
26 | }
27 | class Solution {
28 | func mergeKLists(_ lists: [ListNode?]) -> ListNode? {
29 | if lists.count == 0 {
30 | return nil
31 | }
32 | var ar = [Int]()
33 | for i in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/23. 合并K个排序链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/23. 合并K个排序链表.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/23. 合并K个排序链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/23. 合并K个排序链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/23. 合并K个排序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/23. 合并K个排序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/23. 合并K个排序链表.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 23. 合并K个排序链表 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/237. 删除链表中的节点.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 请编写一个函数,使其可以删除某个链表中给定的(非末尾)节点,你将只被给定要求被删除的节点。
4 |
5 | 现有一个链表 -- head = [4,5,1,9],它可以表示为:
6 |
7 |
8 |
9 | 示例 1:
10 |
11 | 输入: head = [4,5,1,9], node = 5
12 | 输出: [4,1,9]
13 | 解释: 给定你链表中值为 5 的第二个节点,那么在调用了你的函数之后,该链表应变为 4 -> 1 -> 9.
14 |
15 | 示例 2:
16 |
17 | 输入: head = [4,5,1,9], node = 1
18 | 输出: [4,5,9]
19 | 解释: 给定你链表中值为 1 的第三个节点,那么在调用了你的函数之后,该链表应变为 4 -> 5 -> 9.
20 |
21 |
22 |
23 | 说明:
24 |
25 | 链表至少包含两个节点。
26 | 链表中所有节点的值都是唯一的。
27 | 给定的节点为非末尾节点并且一定是链表中的一个有效节点。
28 | 不要从你的函数中返回任何结果。
29 |
30 |
31 |
32 | 来源:力扣(LeetCode)
33 | 链接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list
34 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
35 | */
36 |
37 |
38 | public class ListNode {
39 | public var val: Int
40 | public var next: ListNode?
41 | public init(_ val: Int) {
42 | self.val = val
43 | self.next = nil
44 | }
45 | }
46 | class Solution {
47 | func deleteNode(_ node:ListNode) {
48 | var tmp: ListNode? = node
49 | if tmp?.next == nil {
50 | tmp = tmp?.next
51 | return
52 | }
53 | tmp?.val = tmp?.next?.val ?? 0
54 | tmp?.next = tmp?.next?.next
55 | }
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/LeetCode/237. 删除链表中的节点.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/237. 删除链表中的节点.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/237. 删除链表中的节点.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/237. 删除链表中的节点.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/237. 删除链表中的节点.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/25. K 个一组翻转链表.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 25. K 个一组翻转链表
4 | 给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
5 |
6 | k 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
7 |
8 | 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
9 |
10 | https://leetcode.cn/problems/reverse-nodes-in-k-group/
11 | */
12 |
13 |
14 | class Solution {
15 | func reverseKGroup(_ head: ListNode?, _ k: Int) -> ListNode? {
16 | if head == nil {
17 | return nil
18 | }
19 | var a = head, b = head
20 | for i in 0.. ListNode? {
32 | var pre: ListNode? = nil, cur = a, next = a
33 | while cur != b {
34 | next = cur?.next
35 | cur?.next = pre
36 | pre = cur
37 | cur = next
38 | }
39 | //返回反转后的头结点
40 | return pre
41 | }
42 | }
43 |
44 | extension ListNode: Equatable {
45 | public static func == (lhs: ListNode, rhs: ListNode) -> Bool {
46 | return lhs.val == rhs.val && lhs.next == rhs.next
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/LeetCode/25. K 个一组翻转链表.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/25. K 个一组翻转链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/25. K 个一组翻转链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/25. K 个一组翻转链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/25. K 个一组翻转链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/25. K 个一组翻转链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/26. 删除有序数组中的重复项.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给你一个 升序排列 的数组 nums ,请你 原地 删除重复出现的元素,使每个元素 只出现一次 ,返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。
4 |
5 | 由于在某些语言中不能改变数组的长度,所以必须将结果放在数组nums的第一部分。更规范地说,如果在删除重复项之后有 k 个元素,那么 nums 的前 k 个元素应该保存最终结果。
6 |
7 | 将最终结果插入 nums 的前 k 个位置后返回 k 。
8 |
9 | 不要使用额外的空间,你必须在 原地 修改输入数组 并在使用 O(1) 额外空间的条件下完成。
10 |
11 | 判题标准:
12 |
13 | 系统会用下面的代码来测试你的题解:
14 |
15 | int[] nums = [...]; // 输入数组
16 | int[] expectedNums = [...]; // 长度正确的期望答案
17 |
18 | int k = removeDuplicates(nums); // 调用
19 |
20 | assert k == expectedNums.length;
21 | for (int i = 0; i < k; i++) {
22 | assert nums[i] == expectedNums[i];
23 | }
24 | 如果所有断言都通过,那么您的题解将被 通过。
25 |
26 |
27 |
28 | 示例 1:
29 |
30 | 输入:nums = [1,1,2]
31 | 输出:2, nums = [1,2,_]
32 | 解释:函数应该返回新的长度 2 ,并且原数组 nums 的前两个元素被修改为 1, 2 。不需要考虑数组中超出新长度后面的元素。
33 | 示例 2:
34 |
35 | 输入:nums = [0,0,1,1,1,2,2,3,3,4]
36 | 输出:5, nums = [0,1,2,3,4]
37 | 解释:函数应该返回新的长度 5 , 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4 。不需要考虑数组中超出新长度后面的元素。
38 |
39 |
40 | 提示:
41 |
42 | 1 <= nums.length <= 3 * 104
43 | -104 <= nums[i] <= 104
44 | nums 已按 升序 排列
45 |
46 | 来源:力扣(LeetCode)
47 | 链接:https://leetcode.cn/problems/remove-duplicates-from-sorted-array
48 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
49 | */
50 |
51 | class Solution {
52 | func removeDuplicates(_ nums: inout [Int]) -> Int {
53 | guard !nums.isEmpty else {
54 | return 0
55 | }
56 |
57 | var fast: Int = 0, slow = 0
58 | while fast < nums.count {
59 | if nums[fast] != nums[slow] {
60 | slow += 1
61 | nums[slow] = nums[fast]
62 | }
63 | fast += 1
64 | }
65 | return slow + 1
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/LeetCode/26. 删除有序数组中的重复项.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/26. 删除有序数组中的重复项.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/26. 删除有序数组中的重复项.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/26. 删除有序数组中的重复项.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/26. 删除有序数组中的重复项.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/26. 删除有序数组中的重复项.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/27. 移除元素.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给你一个数组 nums 和一个值 val,你需要 原地 移除所有数值等于 val 的元素,并返回移除后数组的新长度。
4 |
5 | 不要使用额外的数组空间,你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
6 |
7 | 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
8 |
9 |
10 |
11 | 说明:
12 |
13 | 为什么返回数值是整数,但输出的答案是数组呢?
14 |
15 | 请注意,输入数组是以「引用」方式传递的,这意味着在函数里修改输入数组对于调用者是可见的。
16 |
17 | 你可以想象内部操作如下:
18 |
19 | // nums 是以“引用”方式传递的。也就是说,不对实参作任何拷贝
20 | int len = removeElement(nums, val);
21 |
22 | // 在函数里修改输入数组对于调用者是可见的。
23 | // 根据你的函数返回的长度, 它会打印出数组中 该长度范围内 的所有元素。
24 | for (int i = 0; i < len; i++) {
25 | print(nums[i]);
26 | }
27 |
28 |
29 | 示例 1:
30 |
31 | 输入:nums = [3,2,2,3], val = 3
32 | 输出:2, nums = [2,2]
33 | 解释:函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。例如,函数返回的新长度为 2 ,而 nums = [2,2,3,3] 或 nums = [2,2,0,0],也会被视作正确答案。
34 | 示例 2:
35 |
36 | 输入:nums = [0,1,2,2,3,0,4,2], val = 2
37 | 输出:5, nums = [0,1,4,0,3]
38 | 解释:函数应该返回新的长度 5, 并且 nums 中的前五个元素为 0, 1, 3, 0, 4。注意这五个元素可为任意顺序。你不需要考虑数组中超出新长度后面的元素。
39 |
40 |
41 | 提示:
42 |
43 | 0 <= nums.length <= 100
44 | 0 <= nums[i] <= 50
45 | 0 <= val <= 100
46 |
47 |
48 | 来源:力扣(LeetCode)
49 | 链接:https://leetcode.cn/problems/remove-element
50 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
51 | */
52 |
53 | class Solution {
54 | func removeElement(_ nums: inout [Int], _ val: Int) -> Int {
55 | var slow = 0, fast = 0
56 | for i in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/27. 移除元素.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/27. 移除元素.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/27. 移除元素.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/27. 移除元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/27. 移除元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/283. 移动零.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个整数数组 A,只有我们可以将其划分为三个和相等的非空部分时才返回 true,否则返回 false。
4 |
5 | 形式上,如果我们可以找出索引 i+1 < j 且满足 (A[0] + A[1] + ... + A[i] == A[i+1] + A[i+2] + ... + A[j-1] == A[j] + A[j-1] + ... + A[A.length - 1]) 就可以将数组三等分。
6 |
7 |
8 |
9 | 示例 1:
10 |
11 | 输出:[0,2,1,-6,6,-7,9,1,2,0,1]
12 | 输出:true
13 | 解释:0 + 2 + 1 = -6 + 6 - 7 + 9 + 1 = 2 + 0 + 1
14 |
15 | 示例 2:
16 |
17 | 输入:[0,2,1,-6,6,7,9,-1,2,0,1]
18 | 输出:false
19 |
20 | 示例 3:
21 |
22 | 输入:[3,3,6,5,-2,2,5,1,-9,4]
23 | 输出:true
24 | 解释:3 + 3 = 6 = 5 - 2 + 2 + 5 + 1 - 9 + 4
25 |
26 | 来源:力扣(LeetCode)
27 | 链接:https://leetcode-cn.com/problems/partition-array-into-three-parts-with-equal-sum
28 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
29 | */
30 |
31 |
32 | class Solution {
33 | func canThreePartsEqualSum(_ A: [Int]) -> Bool {
34 | var sum = 0
35 | for i in A {
36 | sum += i
37 | }
38 | var key = sum / 3
39 | var temp = 0
40 | for i in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/283. 移动零.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/283. 移动零.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/283. 移动零.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/283. 移动零的副本.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/283. 移动零的副本.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/283. 移动零的副本.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/3. 无重复字符的最长子串.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度。
4 | 示例 1:
5 | 输入: "abcabcbb"
6 | 输出: 3
7 | 解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。
8 | 示例 2:
9 | 输入: "bbbbb"
10 | 输出: 1
11 | 解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。
12 | 示例 3:
13 | 输入: "pwwkew"
14 | 输出: 3
15 | 解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
16 | 请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。
17 |
18 |
19 | 来源:力扣(LeetCode)
20 | 链接:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters
21 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
22 | */
23 |
24 |
25 | class Solution {
26 | func lengthOfLongestSubstring(_ s: String) -> Int {
27 | var s = Array(s)
28 | var window: [Character: Int] = [:]
29 | var left = 0, right = 0, res = 0
30 | while right < s.count {
31 | let c = s[right]
32 | right += 1
33 | if let count = window[c] {
34 | window[c] = count + 1
35 | } else {
36 | window[c] = 1
37 | }
38 | while (window[c] ?? 0) > 1 {
39 | let d = s[left]
40 | left += 1
41 | if let count = window[d] {
42 | window[d] = count - 1
43 | }
44 | }
45 | res = max(res, right - left)
46 | }
47 | return res
48 | }
49 | }
50 |
51 |
--------------------------------------------------------------------------------
/LeetCode/3. 无重复字符的最长子串.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/3. 无重复字符的最长子串.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/3. 无重复字符的最长子串.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/3. 无重复字符的最长子串.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/3. 无重复字符的最长子串.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/3. 无重复字符的最长子串.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/3. 无重复字符的最长子串.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/33. 搜索旋转排序数组.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 假设按照升序排序的数组在预先未知的某个点上进行了旋转。
4 | ( 例如,数组 [0,1,2,4,5,6,7] 可能变为 [4,5,6,7,0,1,2] )。
5 | 搜索一个给定的目标值,如果数组中存在这个目标值,则返回它的索引,否则返回 -1 。
6 | 你可以假设数组中不存在重复的元素。
7 | 你的算法时间复杂度必须是 O(log n) 级别。
8 | 示例 1:
9 | 输入: nums = [4,5,6,7,0,1,2], target = 0
10 | 输出: 4
11 | 示例 2:
12 | 输入: nums = [4,5,6,7,0,1,2], target = 3
13 | 输出: -1
14 | 来源:力扣(LeetCode)
15 | 链接:https://leetcode-cn.com/problems/search-in-rotated-sorted-array
16 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
17 | */
18 | class Solution {
19 | func search(_ nums: [Int], _ target: Int) -> Int {
20 | var head = 0, tail = nums.count - 1
21 | while head <= tail {
22 | let mid = (head + tail) / 2
23 | if target == nums[mid] {
24 | return mid
25 | }
26 | // 左半段有序
27 | if (nums[head] <= nums[mid]) {
28 | // target 在 这段里
29 | if target >= nums[head] && target <= nums[mid] {
30 | tail = mid - 1
31 | } else {
32 | head = mid + 1
33 | }
34 | } else {
35 | // 右半段有序
36 | if target >= nums[mid] && target <= nums[tail] {
37 | head = mid + 1
38 | } else {
39 | tail = mid - 1
40 | }
41 | }
42 | }
43 | return -1
44 | }
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/LeetCode/33. 搜索旋转排序数组.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/33. 搜索旋转排序数组.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/33. 搜索旋转排序数组.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/33. 搜索旋转排序数组.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/33. 搜索旋转排序数组.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/354. 俄罗斯套娃信封问题.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一些标记了宽度和高度的信封,宽度和高度以整数对形式 (w, h) 出现。当另一个信封的宽度和高度都比这个信封大的时候,这个信封就可以放进另一个信封里,如同俄罗斯套娃一样。
4 |
5 | 请计算最多能有多少个信封能组成一组“俄罗斯套娃”信封(即可以把一个信封放到另一个信封里面)。
6 |
7 | 说明:
8 | 不允许旋转信封。
9 |
10 | 示例:
11 |
12 | 输入: envelopes = [[5,4],[6,4],[6,7],[2,3]]
13 | 输出: 3
14 | 解释: 最多信封的个数为 3, 组合为: [2,3] => [5,4] => [6,7]。
15 |
16 |
17 | 来源:力扣(LeetCode)
18 | 链接:https://leetcode-cn.com/problems/russian-doll-envelopes
19 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
20 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
21 | */
22 |
23 |
24 | class Solution {
25 | //二分法
26 | func maxEnvelopes(_ envelopes: [[Int]]) -> Int {
27 | var envelopes = envelopes
28 | var dp:[Int] = [Int]()
29 | envelopes.sort(by:sortArray)
30 | for i in 0..= dp.count {
43 | dp.append(t)
44 | } else {
45 | dp[right] = t
46 | }
47 | }
48 | return dp.count
49 | }
50 |
51 | func sortArray(_ a:[Int],_ b:[Int]) -> Bool
52 | {
53 | if a[0] == b[0] {
54 | return a[1] > b[1]
55 | }
56 | return a[0] < b[0]
57 | }
58 | }
59 |
60 | //MARK: - 思路
61 | /*
62 | 建立哈希表,从左向右扫描一遍,将整数与索引存放到map中。扫描一遍,对其中的每一个整数K,搜索 target-K 在map中是否存在即可。若存在,则输出 K 与 target-K 的下标即可。此算法的时间复杂度为O(n)
63 | */
64 |
--------------------------------------------------------------------------------
/LeetCode/354. 俄罗斯套娃信封问题.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/354. 俄罗斯套娃信封问题.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/354. 俄罗斯套娃信封问题.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/354. 俄罗斯套娃信封问题.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/354. 俄罗斯套娃信封问题.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/370. 区间加法.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 假设你有一个长度为 n 的数组,初始情况下所有的数字均为 0,你将会被给出 k 个更新的操作。
4 |
5 | 其中,每个操作会被表示为一个三元组:[startIndex, endIndex, inc],你需要将子数组 A[startIndex ... endIndex](包括 startIndex 和 endIndex)增加 inc。
6 |
7 | 请你返回 k 次操作后的数组。
8 |
9 | 示例:
10 |
11 | 输入: length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
12 | 输出: [-2,0,3,5,3]
13 | 解释:
14 |
15 | 初始状态:
16 | [0,0,0,0,0]
17 |
18 | 进行了操作 [1,3,2] 后的状态:
19 | [0,2,2,2,0]
20 |
21 | 进行了操作 [2,4,3] 后的状态:
22 | [0,2,5,5,3]
23 |
24 | 进行了操作 [0,2,-2] 后的状态:
25 | [-2,0,3,5,3]
26 |
27 | 来源:力扣(LeetCode)
28 | 链接:https://leetcode.cn/problems/range-addition
29 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
30 | */
31 |
32 |
33 | class Solution {
34 | func getModifiedArray(_ length: Int, _ updates: [[Int]]) -> [Int] {
35 | let diff = Difference()
36 | let origin = [Int](repeating: 0, count: length)
37 | diff.difference(origin)
38 | for value in updates {
39 | print("value: \(value)")
40 | diff.increment(value[0], value[1], value[2])
41 | }
42 | return diff.result()
43 | }
44 | }
45 |
46 | class Difference {
47 | var diff: [Int] = []
48 | func difference(_ nums: [Int]) {
49 | if nums.isEmpty {
50 | return
51 | }
52 | diff = [Int](repeating: 0, count: nums.count)
53 | diff[0] = nums[0]
54 | for i in 1.. [Int] {
67 | var res: [Int] = [Int](repeating: 0, count: diff.count)
68 | res[0] = diff[0]
69 | for i in 1..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/370. 区间加法.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/370. 区间加法.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/370. 区间加法.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/370. 区间加法.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/370. 区间加法.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/393. UTF-8 编码验证.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | UTF-8 中的一个字符可能的长度为 1 到 4 字节,遵循以下的规则:
4 | 对于 1 字节的字符,字节的第一位设为0,后面7位为这个符号的unicode码。
5 | 对于 n 字节的字符 (n > 1),第一个字节的前 n 位都设为1,第 n+1 位设为0,后面字节的前两位一律设为10。剩下的没有提及的二进制位,全部为这个符号的unicode码。
6 | 这是 UTF-8 编码的工作方式:
7 | Char. number range | UTF-8 octet sequence
8 | (hexadecimal) | (binary)
9 | --------------------+---------------------------------------------
10 | 0000 0000-0000 007F | 0xxxxxxx
11 | 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
12 | 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
13 | 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
14 | 给定一个表示数据的整数数组,返回它是否为有效的 utf-8 编码。
15 | 注意:
16 | 输入是整数数组。只有每个整数的最低 8 个有效位用来存储数据。这意味着每个整数只表示 1 字节的数据。
17 | 示例 1:
18 | data = [197, 130, 1], 表示 8 位的序列: 11000101 10000010 00000001.
19 | 返回 true 。
20 | 这是有效的 utf-8 编码,为一个2字节字符,跟着一个1字节字符。
21 | 示例 2:
22 | data = [235, 140, 4], 表示 8 位的序列: 11101011 10001100 00000100.
23 | 返回 false 。
24 | 前 3 位都是 1 ,第 4 位为 0 表示它是一个3字节字符。
25 | 下一个字节是开头为 10 的延续字节,这是正确的。
26 | 但第二个延续字节不以 10 开头,所以是不符合规则的。
27 | 来源:力扣(LeetCode)
28 | 链接:https://leetcode-cn.com/problems/utf-8-validation
29 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
30 | */
31 |
32 |
33 | class Solution {
34 | func validUtf8(_ data: [Int]) -> Bool {
35 | var cnt: Int = 0
36 | for d in data {
37 | if cnt == 0 {
38 | if (d >> 5) == 0b110 {
39 | cnt = 1
40 | } else if (d >> 4) == 0b1110 {
41 | cnt = 2
42 | } else if (d >> 3) == 0b11110 {
43 | cnt = 3
44 | } else if d >> 7 == 1 {
45 | return false
46 | }
47 | } else {
48 | if (d >> 6) != 0b10 {
49 | return false
50 | }
51 | cnt -= 1
52 | }
53 | }
54 | return cnt == 0
55 | }
56 | }
57 |
58 | //思路
59 | /*
60 | >> 位运算
61 | 1,for 循环进行位运算,判断 data[i] 符合下边的哪一个,记录字节数
62 | 0000 0000-0000 007F | 0xxxxxxx
63 | 0000 0080-0000 07FF | 110xxxxx 10xxxxxx
64 | 0000 0800-0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
65 | 0001 0000-0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
66 | 2、得到字节数,在判断之后字节,是否满足 10 开头
67 | */
68 |
--------------------------------------------------------------------------------
/LeetCode/393. UTF-8 编码验证.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/393. UTF-8 编码验证.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/393. UTF-8 编码验证.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/393. UTF-8 编码验证.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/393. UTF-8 编码验证.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/42. 接雨水.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
4 | 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。 感谢 Marcos 贡献此图。
5 | 示例:
6 |
7 | 输入: [0,1,0,2,1,0,1,3,2,1,2,1]
8 | 输出: 6
9 |
10 | 在真实的面试中遇到过这道题?
11 |
12 | 来源:力扣(LeetCode)
13 | 链接:https://leetcode-cn.com/problems/trapping-rain-water
14 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
15 | */
16 |
17 |
18 | class Solution {
19 | func trap(_ height: [Int]) -> Int {
20 | var left = 0, right = height.count - 1
21 | var lMax = 0, rMax = 0, res = 0
22 | while left < right {
23 | lMax = max(lMax, height[left])
24 | rMax = max(rMax, height[right])
25 | if lMax < rMax {
26 | res += lMax - height[left]
27 | left += 1
28 | } else {
29 | res += rMax - height[right]
30 | right -= 1
31 | }
32 | }
33 | return res
34 | }
35 | }
36 |
37 |
38 |
39 | var array = [1, 2, 3]
40 |
41 | //aaa.remove(at: 10)
42 |
43 |
44 | for i in array {
45 | // print("value 1: \(i) array: \(array)")
46 | array.removeLast()
47 | print("value 0: \(i) array: \(array)")
48 |
49 | }
50 |
51 |
52 |
--------------------------------------------------------------------------------
/LeetCode/42. 接雨水.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/42. 接雨水.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/42. 接雨水.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/42. 接雨水.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/42. 接雨水.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/42. 接雨水.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/42. 接雨水.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/432. 全 O(1) 的数据结构.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 实现一个数据结构支持以下操作:
4 |
5 | Inc(key) - 插入一个新的值为 1 的 key。或者使一个存在的 key 增加一,保证 key 不为空字符串。
6 | Dec(key) - 如果这个 key 的值是 1,那么把他从数据结构中移除掉。否者使一个存在的 key 值减一。如果这个 key 不存在,这个函数不做任何事情。key 保证不为空字符串。
7 | GetMaxKey() - 返回 key 中值最大的任意一个。如果没有元素存在,返回一个空字符串""。
8 | GetMinKey() - 返回 key 中值最小的任意一个。如果没有元素存在,返回一个空字符串""。
9 |
10 | 挑战:以 O(1) 的时间复杂度实现所有操作。
11 |
12 | 来源:力扣(LeetCode)
13 | 链接:https://leetcode-cn.com/problems/all-oone-data-structure
14 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
15 | */
16 |
17 |
18 | class AllOne {
19 | var obj:[String:Int]
20 | /** Initialize your data structure here. */
21 | init() {
22 | self.obj = [:]
23 | }
24 |
25 | /** Inserts a new key with value 1. Or increments an existing key by 1. */
26 | func inc(_ key: String) {
27 | if obj[key] != nil {
28 | obj[key,default:0] += 1
29 | } else {
30 | obj[key] = 1
31 | }
32 | }
33 |
34 | /** Decrements an existing key by 1. If Key's value is 1, remove it from the data structure. */
35 | func dec(_ key: String) {
36 | if obj[key] != nil {
37 | obj[key,default:0] -= 1
38 | }
39 | if obj[key] == 0 {
40 | obj[key] = nil
41 | }
42 | }
43 |
44 | /** Returns one of the keys with maximal value. */
45 | func getMaxKey() -> String {
46 | var tmpvalue: Int = -1
47 | var tmpkey:String = ""
48 | for (key, value) in obj {
49 | if value > tmpvalue {
50 | tmpvalue = value
51 | tmpkey = key
52 | }
53 | }
54 | return tmpkey
55 | }
56 |
57 | /** Returns one of the keys with Minimal value. */
58 | func getMinKey() -> String {
59 | var tmpvalue: Int = Int.max
60 | var tmpkey:String = ""
61 | for (key, value) in obj {
62 | if value < tmpvalue {
63 | tmpvalue = value
64 | tmpkey = key
65 | }
66 | }
67 | return tmpkey
68 | }
69 | }
70 |
71 |
72 |
--------------------------------------------------------------------------------
/LeetCode/432. 全 O(1) 的数据结构.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/432. 全 O(1) 的数据结构.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/432. 全 O(1) 的数据结构.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/432. 全 O(1) 的数据结构.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/432. 全 O(1) 的数据结构.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/438. 找到字符串中所有字母异位词.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/438. 找到字符串中所有字母异位词.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/438. 找到字符串中所有字母异位词.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/438. 找到字符串中所有字母异位词.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/438. 找到字符串中所有字母异位词.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/438. 找到字符串中所有字母异位词.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/48. 旋转图像.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 48. 旋转图像
4 | 给定一个 n × n 的二维矩阵 matrix 表示一个图像。请你将图像顺时针旋转 90 度。
5 |
6 | 你必须在 原地 旋转图像,这意味着你需要直接修改输入的二维矩阵。请不要 使用另一个矩阵来旋转图像。
7 |
8 |
9 |
10 | 示例 1:
11 |
12 |
13 | 输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
14 | 输出:[[7,4,1],[8,5,2],[9,6,3]]
15 | 示例 2:
16 |
17 |
18 | 输入:matrix = [[5,1,9,11],[2,4,8,10],[13,3,6,7],[15,14,12,16]]
19 | 输出:[[15,13,2,5],[14,3,4,1],[12,6,8,9],[16,7,10,11]]
20 | */
21 |
22 | class Solution {
23 | func rotate(_ matrix: inout [[Int]]) {
24 | var n = matrix.count
25 | for i in 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/48. 旋转图像.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/48. 旋转图像.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/48. 旋转图像.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/48. 旋转图像.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/48. 旋转图像.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/53. 最大子序和.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。
4 |
5 | 示例:
6 |
7 | 输入: [-2,1,-3,4,-1,2,1,-5,4],
8 | 输出: 6
9 | 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。
10 |
11 | 来源:力扣(LeetCode)
12 | 链接:https://leetcode-cn.com/problems/maximum-subarray
13 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
14 | */
15 |
16 |
17 | class Solution {
18 | func maxSubArray(_ nums: [Int]) -> Int {
19 | var sum = 0
20 | var res = nums[0]
21 | for i in nums {
22 | sum = sum > 0 ? sum + i : i
23 | if res < sum {
24 | res = sum
25 | }
26 | }
27 | return res
28 | }
29 | }
30 | //MARK: - 思路
31 | /*
32 | 动态递归:主要是利用逐步求解,以连续数组结束位置为每一步的解,sum其实就是记录了上一步骤的解,在这一步骤进行对比,如果上一步骤的解<0则舍弃。最终得到这一步骤解,与之前步骤解的最大值res进行比较,保存当前的最优解。
33 | */
34 |
35 |
--------------------------------------------------------------------------------
/LeetCode/53. 最大子序和.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/53. 最大子序和.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/53. 最大子序和.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/53. 最大子序和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/53. 最大子序和.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/547. 朋友圈.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 班上有 N 名学生。其中有些人是朋友,有些则不是。他们的友谊具有是传递性。如果已知 A 是 B 的朋友,B 是 C 的朋友,那么我们可以认为 A 也是 C 的朋友。所谓的朋友圈,是指所有朋友的集合。
4 |
5 | 给定一个 N * N 的矩阵 M,表示班级中学生之间的朋友关系。如果M[i][j] = 1,表示已知第 i 个和 j 个学生互为朋友关系,否则为不知道。你必须输出所有学生中的已知的朋友圈总数。
6 |
7 | 示例 1:
8 |
9 | 输入:
10 | [[1,1,0],
11 | [1,1,0],
12 | [0,0,1]]
13 | 输出: 2
14 | 说明:已知学生0和学生1互为朋友,他们在一个朋友圈。
15 | 第2个学生自己在一个朋友圈。所以返回2。
16 |
17 | 示例 2:
18 |
19 | 输入:
20 | [[1,1,0],
21 | [1,1,1],
22 | [0,1,1]]
23 | 输出: 1
24 | 说明:已知学生0和学生1互为朋友,学生1和学生2互为朋友,所以学生0和学生2也是朋友,所以他们三个在一个朋友圈,返回1。
25 |
26 | 注意:
27 |
28 | N 在[1,200]的范围内。
29 | 对于所有学生,有M[i][i] = 1。
30 | 如果有M[i][j] = 1,则有M[j][i] = 1。
31 |
32 | 来源:力扣(LeetCode)
33 | 链接:https://leetcode-cn.com/problems/friend-circles
34 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
35 | */
36 |
37 |
38 | class Solution {
39 | func findCircleNum(_ M: [[Int]]) -> Int {
40 | //二维数组长度,即所有人的个数
41 | let length = M.count
42 | //统计朋友圈个数
43 | var count = 0
44 | //访问标志
45 | var flag = Array(repeating: false, count: length)
46 | //对于每个人
47 | for i in 0...(length - 1) {
48 | //如果未被访问
49 | if flag[i] == false {
50 | //深度优先搜索,访问
51 | dfs(i, m: M, flag: &flag)
52 | count += 1
53 | }
54 | }
55 | return count
56 | }
57 |
58 | func dfs(_ i: Int, m: [[Int]], flag: inout [Bool]) {
59 | for j in 0...(m.count - 1) {
60 | if flag[j] == false && m[i][j] == 1 {
61 | flag[j] = true
62 | dfs(j, m: m, flag: &flag)
63 | }
64 | }
65 | }
66 | }
67 |
68 | //MARK: - 思路
69 | /*
70 | 1、定义朋友圈 count
71 | 2、定义数组 flag = Array(repeating: false, count: length) 标记当前位置是否被访问,默认没有被访问
72 | 3、for 循环 0..
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/547. 朋友圈.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/547. 朋友圈.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/547. 朋友圈.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/547. 朋友圈.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/56. 合并区间.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给出一个区间的集合,请合并所有重叠的区间。
4 |
5 | 示例 1:
6 |
7 | 输入: [[1,3],[2,6],[8,10],[15,18]]
8 | 输出: [[1,6],[8,10],[15,18]]
9 | 解释: 区间 [1,3] 和 [2,6] 重叠, 将它们合并为 [1,6].
10 |
11 | 示例 2:
12 |
13 | 输入: [[1,4],[4,5]]
14 | 输出: [[1,5]]
15 | 解释: 区间 [1,4] 和 [4,5] 可被视为重叠区间。
16 |
17 | 来源:力扣(LeetCode)
18 | 链接:https://leetcode-cn.com/problems/merge-intervals
19 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
20 | */
21 |
22 |
23 | class Solution {
24 | func merge(_ intervals: [[Int]]) -> [[Int]] {
25 | var res: [[Int]] = []
26 | if intervals.count == 0 {
27 | return []
28 | }
29 | var intervals = intervals.sorted { (a, b) -> Bool in
30 | return a[0] < b[0]
31 | }
32 | var i = 0
33 | while i < intervals.count {
34 | let left = intervals[i][0]
35 | var right = intervals[i][1]
36 | while (i < intervals.count - 1) && intervals[i + 1][0] <= right {
37 | i += 1
38 | right = max(right, intervals[i][1])
39 | }
40 | res.append([left, right])
41 | i += 1
42 | }
43 | return res
44 | }
45 | }
46 |
47 | //MARK: - 思路
48 | /*
49 | 先按首位置进行排序;
50 | 接下来,判断两个区间是否重叠,当a[1] >= b[0]说明两个区间有重叠.
51 | 左边位置一定是确定,就是a[0],而右边位置是max(a[1], b[1])
52 | 所以,我们就能找出整个区间为:[1,4]
53 | */
54 |
55 | //MARK: - Test
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
--------------------------------------------------------------------------------
/LeetCode/56. 合并区间.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/56. 合并区间.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/56. 合并区间.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/56. 合并区间.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/56. 合并区间.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/567. 字符串的排列.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列。
4 | 换句话说,第一个字符串的排列之一是第二个字符串的子串。
5 | 示例1:
6 | 输入: s1 = "ab" s2 = "eidbaooo"
7 | 输出: True
8 | 解释: s2 包含 s1 的排列之一 ("ba").
9 | 示例2:
10 | 输入: s1= "ab" s2 = "eidboaoo"
11 | 输出: False
12 | 注意:
13 | 输入的字符串只包含小写字母
14 | 两个字符串的长度都在 [1, 10,000] 之间
15 | 来源:力扣(LeetCode)
16 | 链接:https://leetcode-cn.com/problems/permutation-in-string
17 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
18 | */
19 |
20 | class Solution {
21 | func checkInclusion(_ s1: String, _ s2: String) -> Bool {
22 | let s = Array(s2), t = Array(s1)
23 | var needs: [Character: Int] = [:], window: [Character: Int] = [:]
24 | for c in t {
25 | if let count = needs[c] {
26 | needs[c] = count + 1
27 | } else {
28 | needs[c] = 1
29 | }
30 | }
31 | var left = 0, right = 0, valid = 0
32 | // 记录最小覆盖子串的起始索引和长度
33 | while right < s.count {
34 | // c 是将移入窗口的字符
35 | let c = s[right]
36 | right += 1
37 | if needs.keys.contains(c) {
38 | if let count = window[c] {
39 | window[c] = count + 1
40 | } else {
41 | window[c] = 1
42 | }
43 | if window[c] == needs[c] {
44 | valid += 1
45 | }
46 | }
47 | // 判断左侧窗口是否要收缩
48 | while (right - left) >= t.count {
49 | //在这里更新最小覆盖子串
50 | if valid == needs.count {
51 | return true
52 | }
53 | // d 是将要移除窗口的字符
54 | let d = s[left]
55 | //缩小窗口
56 | left += 1
57 | //进行窗口内数据的一系列更新
58 | if needs.keys.contains(d) {
59 | if window[d] == needs[d] {
60 | valid -= 1
61 | }
62 | if let count = window[d] {
63 | window[d] = count - 1
64 | }
65 | }
66 | }
67 | }
68 | return false
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/LeetCode/567. 字符串的排列.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/567. 字符串的排列.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/567. 字符串的排列.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/567. 字符串的排列.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/567. 字符串的排列.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/567. 字符串的排列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/567. 字符串的排列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/60. 第k个排列.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给出集合 [1,2,3,…,n],其所有元素共有 n! 种排列。
4 | 按大小顺序列出所有排列情况,并一一标记,当 n = 3 时, 所有排列如下:
5 |
6 | "123"
7 | "132"
8 | "213"
9 | "231"
10 | "312"
11 | "321"
12 |
13 | 给定 n 和 k,返回第 k 个排列。
14 |
15 | 说明:
16 |
17 | 给定 n 的范围是 [1, 9]。
18 | 给定 k 的范围是[1, n!]。
19 |
20 | 示例 1:
21 |
22 | 输入: n = 3, k = 3
23 | 输出: "213"
24 |
25 | 示例 2:
26 |
27 | 输入: n = 4, k = 9
28 | 输出: "2314"
29 |
30 |
31 | 来源:力扣(LeetCode)
32 | 链接:https://leetcode-cn.com/problems/permutation-sequence
33 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
34 | */
35 |
36 | class Solution {
37 | func getPermutation(_ num: Int, _ kth: Int) -> String {
38 | guard num > 1 else {
39 | return "1"
40 | }
41 | //1、算出阶乘表,存储到数组,存储 n 的范围 [1, 9] 到数组
42 | var vals: [Int] = Array(repeating: 1, count: 9)
43 | var nums: [Int] = Array(repeating: 1, count: 9)
44 | var kth = kth - 1
45 | for idx in 0...8 {
46 | vals[idx] = idx == 0 ? 1 : vals[idx - 1] * (idx + 1)
47 | nums[idx] = idx + 1
48 | }
49 | var result = ""
50 | //2、逆序,按照位数用 k 对对应位数的阶乘取商,把得到的商,添加到 result
51 | //3、在 nums中移除用到的数字
52 | for idx in (0...num - 2).reversed() {
53 | let iidx = kth / vals[idx]
54 | result.append(String(nums[iidx]))
55 | nums.remove(at: iidx)
56 | kth %= vals[idx]
57 | }
58 | result.append(String(nums[0]))
59 | return result
60 | }
61 | }
62 |
63 | let k = Solution()
64 | k.getPermutation(4, 9)
65 | //MARK: - 思路
66 | /*
67 | 1、假如n=4,k=9,那么每个数字开头的有(4-1)!=6种排列(如1234,1243,1324...),我们可以用k对6取余,j=k/6=1,将k更新为余数k=3,看第k个排列的第一个数字是num[1]=2,将其从num提取出来保存,并更新num=[1,3,4];
68 | 确定好第一个数字后确定第二个数字,第二个数字的排列有(4-2)!=2种,我们再用k对2取余,j=k/2=1,将k更新为余数k=1,从剩余的num中提取第二个数num[2]=3;
69 | 因为(4-3)!=1比较特殊(即循环中第一个if),进行到次步说明只剩下两个数没有确度了,所以此时先将第三个数num[0]=1从num提取出来,更新num,再将num中最后一个数添加到re中,即得到最后结果。
70 | */
71 |
72 |
73 |
--------------------------------------------------------------------------------
/LeetCode/60. 第k个排列.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/60. 第k个排列.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/60. 第k个排列.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/60. 第k个排列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/60. 第k个排列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/674. 最长连续递增序列.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个未经排序的整数数组,找到最长且连续的的递增序列。
4 |
5 | 示例 1:
6 |
7 | 输入: [1,3,5,4,7]
8 | 输出: 3
9 | 解释: 最长连续递增序列是 [1,3,5], 长度为3。
10 | 尽管 [1,3,5,7] 也是升序的子序列, 但它不是连续的,因为5和7在原数组里被4隔开。
11 |
12 | 示例 2:
13 |
14 | 输入: [2,2,2,2,2]
15 | 输出: 1
16 | 解释: 最长连续递增序列是 [2], 长度为1。
17 |
18 | 来源:力扣(LeetCode)
19 | 链接:https://leetcode-cn.com/problems/longest-continuous-increasing-subsequence
20 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
21 | */
22 |
23 |
24 | class Solution {
25 | func findLengthOfLCIS(_ nums: [Int]) -> Int {
26 | if nums.count == 0 {
27 | return 0
28 | }
29 | var result = 1
30 | var maxValue = 1
31 | for i in 1.. nums[i-1] {
33 | maxValue += 1
34 | } else {
35 | result = max(result, maxValue)
36 | maxValue = 1
37 | }
38 | }
39 | return max(result, maxValue)
40 | }
41 | }
42 | let kk = [1,3,5,4,7]
43 | let s = Solution()
44 | s.findLengthOfLCIS(kk)
45 |
46 | //MARK: - 思路
47 | /*
48 | 过程: 1、count 为当前元素峰值,ans为最大峰值
49 | 2、初始化 count = 1
50 | 3、从 0 位置开始遍历,遍历时根据前后元素状态判断是否递增,递增则 count++,递减则 count=1
51 | 4、如果 count>ans,则更新 ans,直到循环结束
52 | 时间复杂度:O(N)
53 | */
54 |
55 |
--------------------------------------------------------------------------------
/LeetCode/674. 最长连续递增序列.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/674. 最长连续递增序列.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/674. 最长连续递增序列.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/674. 最长连续递增序列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/674. 最长连续递增序列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/69. x 的平方根.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 实现 int sqrt(int x) 函数。
4 |
5 | 计算并返回 x 的平方根,其中 x 是非负整数。
6 |
7 | 由于返回类型是整数,结果只保留整数的部分,小数部分将被舍去。
8 |
9 | 示例 1:
10 |
11 | 输入: 4
12 | 输出: 2
13 |
14 | 示例 2:
15 |
16 | 输入: 8
17 | 输出: 2
18 | 说明: 8 的平方根是 2.82842...,
19 | 由于返回类型是整数,小数部分将被舍去。
20 |
21 | 来源:力扣(LeetCode)
22 | 链接:https://leetcode-cn.com/problems/sqrtx
23 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
24 | */
25 |
26 |
27 | class Solution {
28 | func mySqrt(_ a: Int) -> Int {
29 | var x:Int = a
30 | while x * x > a {
31 | x = (x + a/x)/2
32 | }
33 | return x
34 | }
35 | func mySqrtTwo(_ x: Int) -> Int {
36 | if x == 0 {
37 | return 0
38 | }
39 | var left = 1
40 | var right = x / 2
41 | while left < right {
42 | let mid = (left + right + 1) >> 1
43 | let square = mid * mid
44 | if square > x {
45 | right = mid - 1
46 | } else {
47 | left = mid
48 | }
49 | }
50 | return left
51 | }
52 |
53 |
54 | func mySqrtThree(_ x: Int) -> Int {
55 | if x == 0 {
56 | return 0
57 | }
58 | var left = 1
59 | var right = x / 2
60 | while left < right {
61 | let mid = (left + right + 1) >> 1
62 | let square = mid * mid
63 | if square > x {
64 | right = mid - 1
65 | } else {
66 | left = mid
67 | }
68 | }
69 | return left
70 | }
71 | }
72 |
73 |
74 | /*
75 | 复杂度分析:
76 | 时间复杂度:O(logN),二分法的时间复杂度是对数级别的。
77 | 空间复杂度:O(1),使用了常数个数的辅助空间用于存储和比较。
78 | */
79 |
--------------------------------------------------------------------------------
/LeetCode/69. x 的平方根.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/69. x 的平方根.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/69. x 的平方根.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/69. x 的平方根.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/69. x 的平方根.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/695. 岛屿的最大面积.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个包含了一些 0 和 1的非空二维数组 grid , 一个 岛屿 是由四个方向 (水平或垂直) 的 1 (代表土地) 构成的组合。你可以假设二维矩阵的四个边缘都被水包围着。
4 | 找到给定的二维数组中最大的岛屿面积。(如果没有岛屿,则返回面积为0。)
5 | 示例 1:
6 | [[0,0,1,0,0,0,0,1,0,0,0,0,0],
7 | [0,0,0,0,0,0,0,1,1,1,0,0,0],
8 | [0,1,1,0,1,0,0,0,0,0,0,0,0],
9 | [0,1,0,0,1,1,0,0,1,0,1,0,0],
10 | [0,1,0,0,1,1,0,0,1,1,1,0,0],
11 | [0,0,0,0,0,0,0,0,0,0,1,0,0],
12 | [0,0,0,0,0,0,0,1,1,1,0,0,0],
13 | [0,0,0,0,0,0,0,1,1,0,0,0,0]]
14 | 对于上面这个给定矩阵应返回 6。注意答案不应该是11,因为岛屿只能包含水平或垂直的四个方向的‘1’。
15 | 示例 2:
16 | [[0,0,0,0,0,0,0,0]]
17 | 对于上面这个给定的矩阵, 返回 0。
18 | 注意: 给定的矩阵grid 的长度和宽度都不超过 50。
19 | 来源:力扣(LeetCode)
20 | 链接:https://leetcode-cn.com/problems/max-area-of-island
21 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
22 | */
23 |
24 |
25 | class Solution {
26 | func maxAreaOfIsland(_ grid: [[Int]]) -> Int {
27 | var array = grid
28 | var maxArea = 0
29 | for i in 0.. Int {
41 | //判断是否越界和数字是否为0,这个是递归结束的条件
42 | if i < 0 || i >= grid.count || j < 0 || j >= grid[0].count || grid[i][j] == 0 {
43 | return 0
44 | }
45 | var temMaxArea = 1
46 | grid[i][j] = 0 //遍历后的置为 0
47 | //递归查询上下左右四个
48 | temMaxArea = temMaxArea + dfs(&grid, i: i - 1, j: j)
49 | + dfs(&grid, i: i + 1, j: j)
50 | + dfs(&grid, i: i, j: j - 1)
51 | + dfs(&grid, i: i, j: j + 1)
52 | return temMaxArea
53 | }
54 | }
55 |
56 | //MARK: - 思路
57 | /*
58 | 利用DFS求解
59 | */
60 |
--------------------------------------------------------------------------------
/LeetCode/695. 岛屿的最大面积.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/695. 岛屿的最大面积.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/695. 岛屿的最大面积.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/695. 岛屿的最大面积.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/695. 岛屿的最大面积.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/70. 爬楼梯.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 假设你正在爬楼梯。需要 n 阶你才能到达楼顶。
4 |
5 | 每次你可以爬 1 或 2 个台阶。你有多少种不同的方法可以爬到楼顶呢?
6 |
7 | 示例 1:
8 |
9 | 输入:n = 2
10 | 输出:2
11 | 解释:有两种方法可以爬到楼顶。
12 | 1. 1 阶 + 1 阶
13 | 2. 2 阶
14 | 示例 2:
15 |
16 | 输入:n = 3
17 | 输出:3
18 | 解释:有三种方法可以爬到楼顶。
19 | 1. 1 阶 + 1 阶 + 1 阶
20 | 2. 1 阶 + 2 阶
21 | 3. 2 阶 + 1 阶
22 |
23 | 来源:力扣(LeetCode)
24 | 链接:https://leetcode.cn/problems/climbing-stairs
25 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
26 | */
27 |
28 |
29 | class Solution {
30 | /*
31 | 复杂度分析:
32 | - 时间复杂度:`O(n)`,单循环到 n。
33 | - 空间复杂度:`O(n)`,dpdp 数组用了 n 的空间。
34 | */
35 |
36 | func climbStairs(_ n: Int) -> Int {
37 | if n == 1 {
38 | return 1
39 | }
40 | var temp = Array(repeating: 0, count: n + 1)
41 | temp[1] = 1
42 | temp[2] = 2
43 | for i in 3.. Int {
58 | if n == 1 {
59 | return 1
60 | }
61 | var first = 1
62 | var second = 2
63 | for i in 3...n {
64 | let thrid = first + second
65 | first = second
66 | second = thrid
67 | }
68 | return second
69 | }
70 | }
71 |
72 |
73 |
74 |
--------------------------------------------------------------------------------
/LeetCode/70. 爬楼梯.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/70. 爬楼梯.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/70. 爬楼梯.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/70. 爬楼梯.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/70. 爬楼梯.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/70. 爬楼梯.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/71. 简化路径.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 以 Unix 风格给出一个文件的绝对路径,你需要简化它。或者换句话说,将其转换为规范路径。
4 | 在 Unix 风格的文件系统中,一个点(.)表示当前目录本身;此外,两个点 (..) 表示将目录切换到上一级(指向父目录);两者都可以是复杂相对路径的组成部分。更多信息请参阅:Linux / Unix中的绝对路径 vs 相对路径
5 | 请注意,返回的规范路径必须始终以斜杠 / 开头,并且两个目录名之间必须只有一个斜杠 /。最后一个目录名(如果存在)不能以 / 结尾。此外,规范路径必须是表示绝对路径的最短字符串。
6 | 示例 1:
7 | 输入:"/home/"
8 | 输出:"/home"
9 | 解释:注意,最后一个目录名后面没有斜杠。
10 | 示例 2:
11 | 输入:"/../"
12 | 输出:"/"
13 | 解释:从根目录向上一级是不可行的,因为根是你可以到达的最高级。
14 | 示例 3:
15 | 输入:"/home//foo/"
16 | 输出:"/home/foo"
17 | 解释:在规范路径中,多个连续斜杠需要用一个斜杠替换。
18 | 示例 4:
19 | 输入:"/a/./b/../../c/"
20 | 输出:"/c"
21 | 示例 5:
22 | 输入:"/a/../../b/../c//.//"
23 | 输出:"/c"
24 | 示例 6:
25 | 输入:"/a//b////c/d//././/.."
26 | 输出:"/a/b/c"
27 | 来源:力扣(LeetCode)
28 | 链接:https://leetcode-cn.com/problems/simplify-path
29 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
30 | */
31 |
32 |
33 | class Solution {
34 | func simplifyPath(_ path: String) -> String {
35 | var stack = [String]()
36 | let array = path.split(separator: "/")
37 | print("array :\(array)")
38 | for item in array {
39 | if item.count == 0 || item == "." {
40 | continue
41 | } else if item == ".." {
42 | if stack.count > 0 {
43 | stack.removeLast()
44 | }
45 | } else if item != "" {
46 | stack.append(String(item))
47 | }
48 | }
49 | var result:String = ""
50 | for item in stack {
51 | result.append("/" + item)
52 | }
53 | return result.isEmpty ? "/" : result
54 | }
55 |
56 | }
57 |
58 |
59 | //MARK: - 思路
60 | /*
61 | 栈解决,把当前目录压入栈中,遇到..弹出栈顶,最后返回栈中元素.
62 | */
63 |
64 |
--------------------------------------------------------------------------------
/LeetCode/71. 简化路径.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/71. 简化路径.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/71. 简化路径.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/71. 简化路径.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/71. 简化路径.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/76. 最小覆盖子串.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/76. 最小覆盖子串.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/76. 最小覆盖子串.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/76. 最小覆盖子串.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/76. 最小覆盖子串.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/76. 最小覆盖子串.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/83. 删除排序链表中的重复元素.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个排序链表,删除所有重复的元素,使得每个元素只出现一次。
4 |
5 | 示例 1:
6 |
7 | 输入: 1->1->2
8 | 输出: 1->2
9 |
10 | 示例 2:
11 |
12 | 输入: 1->1->2->3->3
13 | 输出: 1->2->3
14 |
15 | 来源:力扣(LeetCode)
16 | 链接:https://leetcode-cn.com/problems/remove-duplicates-from-sorted-list
17 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
18 | */
19 |
20 |
21 | public class ListNode {
22 | public var val: Int
23 | public var next: ListNode?
24 | public init(_ val: Int) {
25 | self.val = val
26 | self.next = nil
27 | }
28 | }
29 | class Solution {
30 | func deleteDuplicates(_ head: ListNode?) -> ListNode? {
31 | var tmp = head
32 | while tmp != nil && tmp?.next != nil {
33 | if tmp?.val == tmp?.next?.val {
34 | tmp?.next = tmp?.next?.next
35 | } else {
36 | tmp = tmp?.next
37 | }
38 | }
39 | return head
40 | }
41 | }
42 |
43 |
--------------------------------------------------------------------------------
/LeetCode/83. 删除排序链表中的重复元素.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/83. 删除排序链表中的重复元素.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/83. 删除排序链表中的重复元素.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/83. 删除排序链表中的重复元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/83. 删除排序链表中的重复元素.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/83. 删除排序链表中的重复元素.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 83. 删除排序链表中的重复元素 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/93. 复原IP地址.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/93. 复原IP地址.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/93. 复原IP地址.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/93. 复原IP地址.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/93. 复原IP地址.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode.xcodeproj/project.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode.xcodeproj/project.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/LeetCode/LeetCode.xcodeproj/project.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode.xcodeproj/xcuserdata/sunkaifeng.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | LeetCode.xcscheme_^#shared#^_
8 |
9 | orderHint
10 | 0
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode/AppDelegate.swift:
--------------------------------------------------------------------------------
1 | //
2 | // AppDelegate.swift
3 | // LeetCode
4 | //
5 | // Created by 孙凯峰 on 2019/7/8.
6 | // Copyright © 2019 孙凯峰. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | @UIApplicationMain
12 | class AppDelegate: UIResponder, UIApplicationDelegate {
13 |
14 | var window: UIWindow?
15 |
16 |
17 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
18 | // Override point for customization after application launch.
19 | return true
20 | }
21 |
22 | func applicationWillResignActive(_ application: UIApplication) {
23 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25 | }
26 |
27 | func applicationDidEnterBackground(_ application: UIApplication) {
28 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30 | }
31 |
32 | func applicationWillEnterForeground(_ application: UIApplication) {
33 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34 | }
35 |
36 | func applicationDidBecomeActive(_ application: UIApplication) {
37 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38 | }
39 |
40 | func applicationWillTerminate(_ application: UIApplication) {
41 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42 | }
43 |
44 |
45 | }
46 |
47 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode/Assets.xcassets/AppIcon.appiconset/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "images" : [
3 | {
4 | "idiom" : "iphone",
5 | "size" : "20x20",
6 | "scale" : "2x"
7 | },
8 | {
9 | "idiom" : "iphone",
10 | "size" : "20x20",
11 | "scale" : "3x"
12 | },
13 | {
14 | "idiom" : "iphone",
15 | "size" : "29x29",
16 | "scale" : "2x"
17 | },
18 | {
19 | "idiom" : "iphone",
20 | "size" : "29x29",
21 | "scale" : "3x"
22 | },
23 | {
24 | "idiom" : "iphone",
25 | "size" : "40x40",
26 | "scale" : "2x"
27 | },
28 | {
29 | "idiom" : "iphone",
30 | "size" : "40x40",
31 | "scale" : "3x"
32 | },
33 | {
34 | "idiom" : "iphone",
35 | "size" : "60x60",
36 | "scale" : "2x"
37 | },
38 | {
39 | "idiom" : "iphone",
40 | "size" : "60x60",
41 | "scale" : "3x"
42 | },
43 | {
44 | "idiom" : "ipad",
45 | "size" : "20x20",
46 | "scale" : "1x"
47 | },
48 | {
49 | "idiom" : "ipad",
50 | "size" : "20x20",
51 | "scale" : "2x"
52 | },
53 | {
54 | "idiom" : "ipad",
55 | "size" : "29x29",
56 | "scale" : "1x"
57 | },
58 | {
59 | "idiom" : "ipad",
60 | "size" : "29x29",
61 | "scale" : "2x"
62 | },
63 | {
64 | "idiom" : "ipad",
65 | "size" : "40x40",
66 | "scale" : "1x"
67 | },
68 | {
69 | "idiom" : "ipad",
70 | "size" : "40x40",
71 | "scale" : "2x"
72 | },
73 | {
74 | "idiom" : "ipad",
75 | "size" : "76x76",
76 | "scale" : "1x"
77 | },
78 | {
79 | "idiom" : "ipad",
80 | "size" : "76x76",
81 | "scale" : "2x"
82 | },
83 | {
84 | "idiom" : "ipad",
85 | "size" : "83.5x83.5",
86 | "scale" : "2x"
87 | },
88 | {
89 | "idiom" : "ios-marketing",
90 | "size" : "1024x1024",
91 | "scale" : "1x"
92 | }
93 | ],
94 | "info" : {
95 | "version" : 1,
96 | "author" : "xcode"
97 | }
98 | }
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode/Assets.xcassets/Contents.json:
--------------------------------------------------------------------------------
1 | {
2 | "info" : {
3 | "version" : 1,
4 | "author" : "xcode"
5 | }
6 | }
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode/Base.lproj/LaunchScreen.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode/Base.lproj/Main.storyboard:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | APPL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 | LSRequiresIPhoneOS
22 |
23 | UILaunchStoryboardName
24 | LaunchScreen
25 | UIMainStoryboardFile
26 | Main
27 | UIRequiredDeviceCapabilities
28 |
29 | armv7
30 |
31 | UISupportedInterfaceOrientations
32 |
33 | UIInterfaceOrientationPortrait
34 | UIInterfaceOrientationLandscapeLeft
35 | UIInterfaceOrientationLandscapeRight
36 |
37 | UISupportedInterfaceOrientations~ipad
38 |
39 | UIInterfaceOrientationPortrait
40 | UIInterfaceOrientationPortraitUpsideDown
41 | UIInterfaceOrientationLandscapeLeft
42 | UIInterfaceOrientationLandscapeRight
43 |
44 |
45 |
46 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCode/ViewController.swift:
--------------------------------------------------------------------------------
1 | //
2 | // ViewController.swift
3 | // LeetCode
4 | //
5 | // Created by 孙凯峰 on 2019/7/8.
6 | // Copyright © 2019 孙凯峰. All rights reserved.
7 | //
8 |
9 | import UIKit
10 |
11 | class ViewController: UIViewController {
12 |
13 | override func viewDidLoad() {
14 | super.viewDidLoad()
15 | // Do any additional setup after loading the view.
16 | }
17 |
18 |
19 | }
20 |
21 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCodeTests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCodeTests/LeetCodeTests.swift:
--------------------------------------------------------------------------------
1 | //
2 | // LeetCodeTests.swift
3 | // LeetCodeTests
4 | //
5 | // Created by 孙凯峰 on 2019/7/8.
6 | // Copyright © 2019 孙凯峰. All rights reserved.
7 | //
8 |
9 | import XCTest
10 | @testable import LeetCode
11 |
12 | class LeetCodeTests: XCTestCase {
13 |
14 | override func setUp() {
15 | // Put setup code here. This method is called before the invocation of each test method in the class.
16 | }
17 |
18 | override func tearDown() {
19 | // Put teardown code here. This method is called after the invocation of each test method in the class.
20 | }
21 |
22 | func testExample() {
23 | // This is an example of a functional test case.
24 | // Use XCTAssert and related functions to verify your tests produce the correct results.
25 | }
26 |
27 | func testPerformanceExample() {
28 | // This is an example of a performance test case.
29 | self.measure {
30 | // Put the code you want to measure the time of here.
31 | }
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCodeUITests/Info.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | CFBundleDevelopmentRegion
6 | $(DEVELOPMENT_LANGUAGE)
7 | CFBundleExecutable
8 | $(EXECUTABLE_NAME)
9 | CFBundleIdentifier
10 | $(PRODUCT_BUNDLE_IDENTIFIER)
11 | CFBundleInfoDictionaryVersion
12 | 6.0
13 | CFBundleName
14 | $(PRODUCT_NAME)
15 | CFBundlePackageType
16 | BNDL
17 | CFBundleShortVersionString
18 | 1.0
19 | CFBundleVersion
20 | 1
21 |
22 |
23 |
--------------------------------------------------------------------------------
/LeetCode/LeetCode/LeetCodeUITests/LeetCodeUITests.swift:
--------------------------------------------------------------------------------
1 | //
2 | // LeetCodeUITests.swift
3 | // LeetCodeUITests
4 | //
5 | // Created by 孙凯峰 on 2019/7/8.
6 | // Copyright © 2019 孙凯峰. All rights reserved.
7 | //
8 |
9 | import XCTest
10 |
11 | class LeetCodeUITests: XCTestCase {
12 |
13 | override func setUp() {
14 | // Put setup code here. This method is called before the invocation of each test method in the class.
15 |
16 | // In UI tests it is usually best to stop immediately when a failure occurs.
17 | continueAfterFailure = false
18 |
19 | // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
20 | XCUIApplication().launch()
21 |
22 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
23 | }
24 |
25 | override func tearDown() {
26 | // Put teardown code here. This method is called after the invocation of each test method in the class.
27 | }
28 |
29 | func testExample() {
30 | // Use recording to get started writing UI tests.
31 | // Use XCTAssert and related functions to verify your tests produce the correct results.
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/LeetCode/gen.python:
--------------------------------------------------------------------------------
1 | import glob
2 | import re
3 |
4 | # 打开 readme.md 文件
5 | with open("readme.md", "w") as f:
6 | # 输出表格头部
7 | f.write("| **Leetcode** | Code \n| :--- | :---: |\n")
8 |
9 | # 扫描当前文件夹中 .playground 文件,并截取不包含 .playground 的文件名
10 | names = [re.sub(r"\.playground$", "", file) for file in glob.glob("*.playground")]
11 |
12 | # 扫描文件名中的数字,并按照数字大小排序
13 | sorted_names = sorted(names, key=lambda x: int(re.search(r"\d+", x).group()))
14 |
15 | # 遍历数组的元素,以表格的形式输出到 readme.md,每行文件名后面都要加上换行
16 | for file in sorted_names:
17 | f.write(f"| {file} |[Swift](LeetCode/) \n")
18 |
19 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 剑指 Offer II 022. 链表中环的入口节点
4 | 给定一个链表,返回链表开始入环的第一个节点。 从链表的头节点开始沿着 next 指针进入环的第一个节点为环的入口节点。如果链表无环,则返回 null。
5 |
6 | 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。注意,pos 仅仅是用于标识环的情况,并不会作为参数传递到函数中。
7 |
8 | 说明:不允许修改给定的链表。
9 | */
10 |
11 | class Solution {
12 | func detectCycle(_ head: ListNode?) -> ListNode? {
13 | var fast = head, slow = head
14 | while fast != nil || fast?.next != nil {
15 | fast = fast?.next?.next
16 | slow = slow?.next
17 | if fast === slow {
18 | break
19 | }
20 | }
21 | if fast == nil || fast?.next == nil {
22 | return nil
23 | }
24 | slow = head
25 | while fast !== slow {
26 | fast = fast?.next
27 | slow = slow?.next
28 | }
29 | return slow
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 剑指 Offer II 022. 链表中环的入口节点 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 给定一个正整数数组 nums和整数 k ,请找出该数组内乘积小于 k 的连续的子数组的个数。
4 |
5 |
6 |
7 | 示例 1:
8 |
9 | 输入: nums = [10,5,2,6], k = 100
10 | 输出: 8
11 | 解释: 8 个乘积小于 100 的子数组分别为: [10], [5], [2], [6], [10,5], [5,2], [2,6], [5,2,6]。
12 | 需要注意的是 [10,5,2] 并不是乘积小于100的子数组。
13 | 示例 2:
14 |
15 | 输入: nums = [1,2,3], k = 0
16 | 输出: 0
17 |
18 |
19 | 提示:
20 |
21 | 1 <= nums.length <= 3 * 104
22 | 1 <= nums[i] <= 1000
23 | 0 <= k <= 106
24 |
25 |
26 | 来源:力扣(LeetCode)
27 | 链接:https://leetcode.cn/problems/ZVAVXX
28 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
29 | */
30 |
31 | class Solution {
32 | func numSubarrayProductLessThanK(_ nums: [Int], _ k: Int) -> Int {
33 | var s = nums
34 | var right = 0, left = 0, res = 0
35 | var tmp = 1
36 | while right < s.count {
37 | let c = s[right]
38 | tmp = tmp * c
39 | right += 1
40 | while left < right && tmp >= k {
41 | tmp = tmp / nums[left]
42 | left += 1
43 | }
44 | res = res + right - left
45 | }
46 | return res
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 022. 链表中环的入口节点的副本.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 剑指 Offer II 022. 链表中环的入口节点的副本 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 078. 合并排序链表.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 078. 合并排序链表.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 078. 合并排序链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/剑指 Offer II 078. 合并排序链表.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 078. 合并排序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/剑指 Offer II 078. 合并排序链表.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/剑指 Offer II 078. 合并排序链表.playground/xcuserdata/kent.sun.xcuserdatad/xcschemes/xcschememanagement.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | SchemeUserState
6 |
7 | 剑指 Offer II 078. 合并排序链表 (Playground).xcscheme
8 |
9 | isShown
10 |
11 | orderHint
12 | 0
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.01. 判定字符是否唯一.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | 实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
4 |
5 | 示例 1:
6 |
7 | 输入: s = "leetcode"
8 | 输出: false
9 | 示例 2:
10 |
11 | 输入: s = "abc"
12 | 输出: true
13 | 限制:
14 |
15 | 0 <= len(s) <= 100
16 | s[i]仅包含小写字母
17 | 如果你不使用额外的数据结构,会很加分。
18 | 通过次数137,855提交次数193,477
19 |
20 | 来源:力扣(LeetCode)
21 | 链接:https://leetcode.cn/problems/is-unique-lcci
22 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23 | */
24 |
25 |
26 | class Solution {
27 | func isUnique(_ astr: String) -> Bool {
28 | var s = astr
29 | for i in astr {
30 | s.removeFirst()
31 | if s.contains(i) {
32 | return false
33 | }
34 | }
35 | return true
36 | }
37 | }
38 |
39 | let str = "leetcode"
40 | let str1 = "abc"
41 |
42 | let solution = Solution()
43 | solution.isUnique(str)
44 | solution.isUnique(str1)
45 |
46 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.01. 判定字符是否唯一.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.01. 判定字符是否唯一.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.01. 判定字符是否唯一.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.01. 判定字符是否唯一.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.01. 判定字符是否唯一.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.01. 判定字符是否唯一.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.02. 判定是否互为字符重排.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.02. 判定是否互为字符重排.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.02. 判定是否互为字符重排.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.02. 判定是否互为字符重排.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.02. 判定是否互为字符重排.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.02. 判定是否互为字符重排.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.03. URL化.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | URL化。编写一种方法,将字符串中的空格全部替换为%20。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。(注:用Java实现的话,请使用字符数组实现,以便直接在数组上操作。)
4 |
5 | 示例 1:
6 |
7 | 输入:"Mr John Smith ", 13
8 | 输出:"Mr%20John%20Smith"
9 | 示例 2:
10 |
11 | 输入:" ", 5
12 | 输出:"%20%20%20%20%20"
13 |
14 |
15 | 提示:
16 |
17 | 字符串长度在 [0, 500000] 范围内。
18 |
19 |
20 | 来源:力扣(LeetCode)
21 | 链接:https://leetcode.cn/problems/string-to-url-lcci
22 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23 | */
24 |
25 |
26 | class Solution {
27 | func replaceSpaces(_ S: String, _ length: Int) -> String {
28 | var value = S.prefix(length)
29 | return value.replacingOccurrences(of: " ", with: "%20")
30 | }
31 | }
32 | let str = "leetcode"
33 | let str1 = "abc"
34 |
35 | let solution = Solution()
36 |
37 |
38 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.03. URL化.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.03. URL化.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.03. URL化.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.03. URL化.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.03. URL化.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.03. URL化.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.04. 回文排列.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | //MARK: - 题目
2 | /*
3 | URL化。编写一种方法,将字符串中的空格全部替换为%20。假定该字符串尾部有足够的空间存放新增字符,并且知道字符串的“真实”长度。(注:用Java实现的话,请使用字符数组实现,以便直接在数组上操作。)
4 |
5 | 示例 1:
6 |
7 | 输入:"Mr John Smith ", 13
8 | 输出:"Mr%20John%20Smith"
9 | 示例 2:
10 |
11 | 输入:" ", 5
12 | 输出:"%20%20%20%20%20"
13 |
14 |
15 | 提示:
16 |
17 | 字符串长度在 [0, 500000] 范围内。
18 |
19 |
20 | 来源:力扣(LeetCode)
21 | 链接:https://leetcode.cn/problems/string-to-url-lcci
22 | 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
23 | */
24 |
25 | /*
26 | 统计字符数量,如果字符数量为奇数的字符数只有一个,就可以排成回文。
27 |
28 |
29 | */
30 | class Solution {
31 | func canPermutePalindrome(_ s: String) -> Bool {
32 | var record = [Character: Int]()
33 | for c in s {
34 | record[c, default: 0] += 1
35 | }
36 | var oddCount = 0
37 | for value in record.values {
38 | oddCount += value % 2
39 | }
40 | return oddCount <= 1
41 | }
42 | }
43 | let str = "leetcode"
44 | let str1 = "abc"
45 |
46 | let solution = Solution()
47 |
48 |
49 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.04. 回文排列.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.04. 回文排列.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.04. 回文排列.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.04. 回文排列.playground/playground.xcworkspace/xcuserdata/kent.sun.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/LeetCode/面试金典/面试题 01.04. 回文排列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/LeetCode/面试金典/面试题 01.04. 回文排列.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/test.sh:
--------------------------------------------------------------------------------
1 | # 扫描文件夹中的 playground 文件
2 | playground_files=$(find . -name "*.playground")
3 |
4 | # 创建 readme.md 文件
5 | touch readme.md
6 |
7 | # 在 readme.md 文件中输出 markdown excel 格式的文件列表
8 | echo "| Name | Link |" >> readme.md
9 | echo "|------|------|" >> readme.md
10 |
11 | for file in $playground_files
12 | do
13 | # 使用超链接形式输出文件名
14 | echo "| $file | [Link]($file) |" >> readme.md
15 | done
16 |
17 |
--------------------------------------------------------------------------------
/恋上数据结构/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/.DS_Store
--------------------------------------------------------------------------------
/恋上数据结构/day1/斐波那契/fibonacciNumber.playground/Contents.swift:
--------------------------------------------------------------------------------
1 | /*
2 | 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列。该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和。也就是:
3 |
4 | F(0) = 0, F(1) = 1
5 | F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
6 |
7 | 给定 N,计算 F(N)
8 | */
9 |
10 | func fib(_ N: Int) -> Int {
11 | if N == 0 {
12 | return 0
13 | }
14 | if N == 1 {
15 | return 1
16 | }
17 | var first = 0
18 | var second = 1
19 | for _ in 1...(N - 1) {
20 | second += first
21 | first = second - first
22 | }
23 | return second
24 | }
25 |
26 | print(fib(20))
27 |
--------------------------------------------------------------------------------
/恋上数据结构/day1/斐波那契/fibonacciNumber.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/恋上数据结构/day1/斐波那契/fibonacciNumber.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/恋上数据结构/day1/斐波那契/fibonacciNumber.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/恋上数据结构/day1/斐波那契/fibonacciNumber.playground/playground.xcworkspace/xcuserdata/May.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day1/斐波那契/fibonacciNumber.playground/playground.xcworkspace/xcuserdata/May.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/恋上数据结构/day2/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day2/.DS_Store
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day2/链表/.DS_Store
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day2/链表/LinkedList.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList2.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList2.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList2.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/恋上数据结构/day2/链表/LinkedList2.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day2/链表/LinkedList2.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------
/恋上数据结构/day3/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day3/.DS_Store
--------------------------------------------------------------------------------
/恋上数据结构/day3/双向链表/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day3/双向链表/.DS_Store
--------------------------------------------------------------------------------
/恋上数据结构/day3/双向链表/LinkedList.playground/contents.xcplayground:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/恋上数据结构/day3/双向链表/LinkedList.playground/playground.xcworkspace/contents.xcworkspacedata:
--------------------------------------------------------------------------------
1 |
2 |
4 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/恋上数据结构/day3/双向链表/LinkedList.playground/playground.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | IDEDidComputeMac32BitWarning
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/恋上数据结构/day3/双向链表/LinkedList.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/wubianxiaoxian/Algorithm-swift/39a507c84fa4e481a8741c2f73e810dbe0570727/恋上数据结构/day3/双向链表/LinkedList.playground/playground.xcworkspace/xcuserdata/sunkaifeng.xcuserdatad/UserInterfaceState.xcuserstate
--------------------------------------------------------------------------------