首页 web前端 js教程 JavaScript 面试备忘单 - 第 2 部分

JavaScript 面试备忘单 - 第 2 部分

Dec 15, 2024 am 07:32 AM

JavaScript Interview Cheat Sheet - Part 2

常见 LeetCode 模式

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

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

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

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253

254

255

256

257

258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

// Two Pointers - In-place array modification

const modifyArray = (arr) => {

    let writePointer = 0;

    for (let readPointer = 0; readPointer < arr.length; readPointer++) {

        if (/* condition */) {

            [arr[writePointer], arr[readPointer]] = [arr[readPointer], arr[writePointer]];

            writePointer++;

        }

    }

    return writePointer; // Often returns new length or modified position

};

 

// Fast and Slow Pointers (Floyd's Cycle Detection)

const hasCycle = (head) => {

    let slow = head, fast = head;

    while (fast && fast.next) {

        slow = slow.next;

        fast = fast.next.next;

        if (slow === fast) return true;

    }

    return false;

};

 

// Sliding Window - Fixed Size

const fixedSlidingWindow = (arr, k) => {

    let sum = 0;

    // Initialize first window

    for (let i = 0; i < k; i++) {

        sum += arr[i];

    }

 

    let maxSum = sum;

    // Slide window

    for (let i = k; i < arr.length; i++) {

        sum = sum - arr[i - k] + arr[i];

        maxSum = Math.max(maxSum, sum);

    }

    return maxSum;

};

 

// Sliding Window - Variable Size

const varSlidingWindow = (arr, target) => {

    let start = 0, sum = 0, minLen = Infinity;

 

    for (let end = 0; end < arr.length; end++) {

        sum += arr[end];

        while (sum >= target) {

            minLen = Math.min(minLen, end - start + 1);

            sum -= arr[start];

            start++;

        }

    }

 

    return minLen === Infinity ? 0 : minLen;

};

 

// BFS - Level Order Traversal

const levelOrder = (root) => {

    if (!root) return [];

    const result = [];

    const queue = [root];

 

    while (queue.length) {

        const levelSize = queue.length;

        const currentLevel = [];

 

        for (let i = 0; i < levelSize; i++) {

            const node = queue.shift();

            currentLevel.push(node.val);

 

            if (node.left) queue.push(node.left);

            if (node.right) queue.push(node.right);

        }

        result.push(currentLevel);

    }

    return result;

};

 

// DFS - Recursive Template

const dfs = (root) => {

    const result = [];

 

    const traverse = (node) => {

        if (!node) return;

 

        // Pre-order

        result.push(node.val);

 

        traverse(node.left);

        // In-order would be here

        traverse(node.right);

        // Post-order would be here

    };

 

    traverse(root);

    return result;

};

 

// Backtracking Template

const backtrack = (nums) => {

    const result = [];

 

    const bt = (path, choices) => {

        if (/* ending condition */) {

            result.push([...path]);

            return;

        }

 

        for (let i = 0; i < choices.length; i++) {

            // Make choice

            path.push(choices[i]);

            // Recurse

            bt(path, /* remaining choices */);

            // Undo choice

            path.pop();

        }

    };

 

    bt([], nums);

    return result;

};

 

// Dynamic Programming - Bottom Up Template

const dpBottomUp = (n) => {

    const dp = new Array(n + 1).fill(0);

    dp[0] = 1; // Base case

 

    for (let i = 1; i <= n; i++) {

        for (let j = 0; j < i; j++) {

            dp[i] += dp[j] * /* some calculation */;

        }

    }

 

    return dp[n];

};

 

// Dynamic Programming - Top Down Template

const dpTopDown = (n) => {

    const memo = new Map();

 

    const dp = (n) => {

        if (n <= 1) return 1;

        if (memo.has(n)) return memo.get(n);

 

        let result = 0;

        for (let i = 0; i < n; i++) {

            result += dp(i) * /* some calculation */;

        }

 

        memo.set(n, result);

        return result;

    };

 

    return dp(n);

};

 

// Monotonic Stack Template

const monotonicStack = (arr) => {

    const stack = []; // [index, value]

    const result = new Array(arr.length).fill(-1);

 

    for (let i = 0; i < arr.length; i++) {

        while (stack.length && stack[stack.length - 1][1] > arr[i]) {

            const [prevIndex, _] = stack.pop();

            result[prevIndex] = i - prevIndex;

        }

        stack.push([i, arr[i]]);

    }

    return result;

};

 

// Prefix Sum

const prefixSum = (arr) => {

    const prefix = [0];

    for (let i = 0; i < arr.length; i++) {

        prefix.push(prefix[prefix.length - 1] + arr[i]);

    }

    // Sum of range [i, j] = prefix[j + 1] - prefix[i]

    return prefix;

};

 

// Binary Search Variations

const binarySearchLeftmost = (arr, target) => {

    let left = 0, right = arr.length;

    while (left < right) {

        const mid = Math.floor((left + right) / 2);

        if (arr[mid] < target) left = mid + 1;

        else right = mid;

    }

    return left;

};

 

const binarySearchRightmost = (arr, target) => {

    let left = 0, right = arr.length;

    while (left < right) {

        const mid = Math.floor((left + right) / 2);

        if (arr[mid] <= target) left = mid + 1;

        else right = mid;

    }

    return left - 1;

};

 

// Trie Operations

class TrieNode {

    constructor() {

        this.children = new Map();

        this.isEndOfWord = false;

    }

}

 

class Trie {

    constructor() {

        this.root = new TrieNode();

    }

 

    insert(word) {

        let node = this.root;

        for (const char of word) {

            if (!node.children.has(char)) {

                node.children.set(char, new TrieNode());

            }

            node = node.children.get(char);

        }

        node.isEndOfWord = true;

    }

 

    search(word) {

        let node = this.root;

        for (const char of word) {

            if (!node.children.has(char)) return false;

            node = node.children.get(char);

        }

        return node.isEndOfWord;

    }

 

    startsWith(prefix) {

        let node = this.root;

        for (const char of prefix) {

            if (!node.children.has(char)) return false;

            node = node.children.get(char);

        }

        return true;

    }

}

 

// Union Find (Disjoint Set)

class UnionFind {

    constructor(n) {

        this.parent = Array.from({length: n}, (_, i) => i);

        this.rank = new Array(n).fill(0);

    }

 

    find(x) {

        if (this.parent[x] !== x) {

            this.parent[x] = this.find(this.parent[x]); // Path compression

        }

        return this.parent[x];

    }

 

    union(x, y) {

        let rootX = this.find(x);

        let rootY = this.find(y);

 

        if (rootX !== rootY) {

            if (this.rank[rootX] < this.rank[rootY]) {

                [rootX, rootY] = [rootY, rootX];

            }

            this.parent[rootY] = rootX;

            if (this.rank[rootX] === this.rank[rootY]) {

                this.rank[rootX]++;

            }

        }

    }

}

登录后复制

常见的时间/空间复杂度模式

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

// O(1) - Constant

Array.push(), Array.pop(), Map.set(), Map.get()

 

// O(log n) - Logarithmic

Binary Search, Balanced BST operations

 

// O(n) - Linear

Array traversal, Linear Search

 

// O(n log n) - Linearithmic

Efficient sorting (Array.sort())

 

// O(n²) - Quadratic

Nested loops, Simple sorting algorithms

 

// O(2ⁿ) - Exponential

Recursive solutions without memoization

登录后复制

以上是JavaScript 面试备忘单 - 第 2 部分的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

在JavaScript中替换字符串字符 在JavaScript中替换字符串字符 Mar 11, 2025 am 12:07 AM

JavaScript字符串替换方法详解及常见问题解答 本文将探讨两种在JavaScript中替换字符串字符的方法:在JavaScript代码内部替换和在网页HTML内部替换。 在JavaScript代码内部替换字符串 最直接的方法是使用replace()方法: str = str.replace("find","replace"); 该方法仅替换第一个匹配项。要替换所有匹配项,需使用正则表达式并添加全局标志g: str = str.replace(/fi

自定义Google搜索API设置教程 自定义Google搜索API设置教程 Mar 04, 2025 am 01:06 AM

本教程向您展示了如何将自定义的Google搜索API集成到您的博客或网站中,提供了比标准WordPress主题搜索功能更精致的搜索体验。 令人惊讶的是简单!您将能够将搜索限制为Y

构建您自己的Ajax Web应用程序 构建您自己的Ajax Web应用程序 Mar 09, 2025 am 12:11 AM

因此,在这里,您准备好了解所有称为Ajax的东西。但是,到底是什么? AJAX一词是指用于创建动态,交互式Web内容的一系列宽松的技术。 Ajax一词,最初由Jesse J创造

示例颜色json文件 示例颜色json文件 Mar 03, 2025 am 12:35 AM

本文系列在2017年中期进行了最新信息和新示例。 在此JSON示例中,我们将研究如何使用JSON格式将简单值存储在文件中。 使用键值对符号,我们可以存储任何类型的

10个jQuery语法荧光笔 10个jQuery语法荧光笔 Mar 02, 2025 am 12:32 AM

增强您的代码演示:开发人员的10个语法荧光笔 在您的网站或博客上共享代码片段是开发人员的常见实践。 选择合适的语法荧光笔可以显着提高可读性和视觉吸引力。 t

8令人惊叹的jQuery页面布局插件 8令人惊叹的jQuery页面布局插件 Mar 06, 2025 am 12:48 AM

利用轻松的网页布局:8个基本插件 jQuery大大简化了网页布局。 本文重点介绍了简化该过程的八个功能强大的JQuery插件,对于手动网站创建特别有用

10 JavaScript和JQuery MVC教程 10 JavaScript和JQuery MVC教程 Mar 02, 2025 am 01:16 AM

本文介绍了关于JavaScript和JQuery模型视图控制器(MVC)框架的10多个教程的精选选择,非常适合在新的一年中提高您的网络开发技能。 这些教程涵盖了来自Foundatio的一系列主题

什么是这个&#x27;在JavaScript? 什么是这个&#x27;在JavaScript? Mar 04, 2025 am 01:15 AM

核心要点 JavaScript 中的 this 通常指代“拥有”该方法的对象,但具体取决于函数的调用方式。 没有当前对象时,this 指代全局对象。在 Web 浏览器中,它由 window 表示。 调用函数时,this 保持全局对象;但调用对象构造函数或其任何方法时,this 指代对象的实例。 可以使用 call()、apply() 和 bind() 等方法更改 this 的上下文。这些方法使用给定的 this 值和参数调用函数。 JavaScript 是一门优秀的编程语言。几年前,这句话可

See all articles