shannonchenchn / algorithm-and-data-structure Goto Github PK
View Code? Open in Web Editor NEWAlgorithms + Data Structures = Programs
Algorithms + Data Structures = Programs
A priority queue is a queue where the most important element is always at the front.
The queue can be a max-priority queue (largest element first) or a min-priority queue (smallest element first).
参考:
struct Stack<Element> {
private var storage: [Element] = []
mutating func push(_ object: Element) {
storage.append(object)
}
@discardableResult
mutating func pop() -> Element? {
storage.popLast()
}
func peek() -> Element? {
return storage.last
}
func isEmpty() -> Bool {
return storage.isEmpty
}
}
extension Stack: CustomStringConvertible {
var description: String {
return storage.description
}
}
extension Stack: ExpressibleByArrayLiteral {
init(arrayLiteral elements: Element...) {
self.init()
for element in elements {
self.push(element)
}
}
}
3). 还有一类题型是因为正好可以利用栈“后进先出”的特性来解决问题的(但并不一定是最优解),比如回删操作、后退操作这类问题,相关题目有 844. Backspace String Compare、19. 删除链表倒数第 n 个结点 和 682. Baseball Game 。
Stack
类,但是可以用数组模拟栈的操作。popLast()
相当于 pop 操作,append()
相当于 push 操作O(n²)
的排序
O(nlog(n))
的排序
O(n)
的排序(线性排序)
partition
函数的实现题库来源:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x6w3ds/
Character
的常用 API
asciiValue
isNumber
isLetter
isWhitespace
isNewline
wholeNumberValue
lowercased()
uppercased()
String
中的字符,我们可以将一个 String
类型的变量转成 Array
,比如 let str = "abc"
可以转成 let charArray = Array(str)
Int.max
的逆操作,比如判断某数乘 10 是否会溢出,那么就把该数和 Int.max
除 10 进行比较:num > Int.max / 10 || (num == Int.max / 10 && curChar.wholeNumberValue > Int.max % 10)
i
j
递增,但是有些特殊情况也要具体分析,比如 5. 最长回文子串 这一题dp[i][j]
表示前 i
, j
个字符串的状态,但是在遍历字符串时一定要记得前 i
个字符串的最后一个字符是第 i-1
位而不是第 i
位。比如 10. 正则表达式匹配在一个二维数组中,每一行都按照从左到右递增的顺序排列,每一列都按照从上到下递增的顺序排列。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。
例如,从下面的数组中查找 7 返回 true,查找 5 返回 false。
1 2 8 9
2 4 9 12
4 7 10 13
6 8 11 15
Given two strings s and t, write a function to determine if t is an anagram of s.
For example,
s = "anagram", t = "nagaram", return true. s = "rat", t = "car", return false.
You may assume the string contains only lowercase alphabets.
方案1:为s和t分别建立一个HashMap,统计每个字母出现的次数,比较两个HashMap是否相等。时间复杂度O(n),空间复杂度O(n),n为字符串长度。
方案2:题目中的字符串只包含小写的字母,意味着只有从a-z的26个字母,于是我们可以为s和t开一个长度为26的整数数组来代替哈希表,此时额外空间为固定长度的两个数组,因此为O(1)。
#define ALPHABET_SIZE 26
BOOL isAnagram(NSString *s, NSString *t) {
if (s.length != t.length) {
return NO;
}
int map[ALPHABET_SIZE] = {}; // C 数组必须要初始化,才能保证所有元素初始值为 0
for (int i = 0; i < s.length; i++) {
int indexForS = [s characterAtIndex:i] - 'a';
map[indexForS] = map[indexForS] + 1;
int indexForT = [t characterAtIndex:i] - 'a';
map[indexForT] = map[indexForT] - 1;
}
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (map[i] != 0) {
return NO;
}
}
return YES;
}
课程地址:https://time.geekbang.org/course/detail/100019701-41518
ppt 地址:https://github.com/ShannonChenCHN/algorithm-1
这是由 LeetCode 官方推出的的经典面试题目清单,我们将题目重新整理规划,从而为大家提供更好的练习体验和帮助大家找到理想的工作。 我们将题目分为以下三个部分:
这一系列 LeetBook 将帮助您掌握算法及数据结构,并提高您的编程能力。
编程能力就像任何其他技能一样,也是一个可以通过 刻意练习 大大提高的。
大多数经典面试题目都有多种解决方案。 为了达到最佳的练习效果,我们强烈建议您至少将此清单里的题目练习两遍,如果可以的话,三遍会更好。
在第二遍练习时,你可能会发现一些新的技巧或新的方法。 到第三遍的时候,你会发现你的代码要比第一次提交时更加简洁。 如果你达到了这样的效果,那么恭喜你,你已经掌握了正确的练习方法!
记住:刻意练习并不意味着寻找答案并记住它,这种练习方法不是长久之计。 在没有参考答案情况下,越能自主解决问题,才越能提高自身能力。
作者:力扣 (LeetCode)
链接:https://leetcode-cn.com/leetbook/read/top-interview-questions-easy/x6w3ds/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
从一个长度为 n,且所有元素大小都在 0~n-1 范围内的数组中,找出任意重复的数字。比如,输入一个长度为 7 的数组 {2, 3, 1, 0, 2, 5, 3},那么结果应该是重复数字 2 或者 3。
&
,或 |
,非 ~
,异或 ^
,左移 <<
,右移 >>
(x & 1) == 1
等价于 (x % 2 == 1)
(x & 1) == 0
等价于 (x % 2 == 0)
x / 2
等价于 x >> 1
x &= (x - 1)
把x最低位的二进制1给去掉x & -x
只保留最低位的1,而把其他位的1都去掉x & ~x
得到 0x^0 = x
x ^ x = 0
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.