# \#algorithm

**URL:** https://www.isharkfly.com/tag/algorithm/32.md

[Latest](https://www.isharkfly.com/latest.md) · [Categories](https://www.isharkfly.com/categories.md) · [Tags](https://www.isharkfly.com/tags.md)

---

## [构建城堡（Build Castles）](https://www.isharkfly.com/t/build-castles/315)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:38pm UTC](https://www.isharkfly.com/t/build-castles/315 "2020-07-24T19:38:09Z")

</div>

中文标题【构建城堡】 Charlemagne, the King of Frankie, 英文描述 请参考图片中的说明。 中文描述 根据给出的数组确定能够盖多少城堡。 思路和点评 我不能确定我的思路是正确的，也欢迎大家参与讨论。 根据给出的数组，因为有重复的值，我首先想到的是将给出的数组进行一次过滤和处理，去掉重复的值。 例如，给出的数组为：int A = { 2, 2, 3, 4, 3, 3, 2, 2, 1, 1, …

---

## [二叉树的序列化和反序列化（Serialize and Deserialize Binary Tree）](https://www.isharkfly.com/t/serialize-and-deserialize-binary-tree/15125)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 2:03pm UTC](https://www.isharkfly.com/t/serialize-and-deserialize-binary-tree/15125 "2023-11-09T14:03:04Z")

</div>

中文标题【二叉树的序列化和反序列化】 描述 设计一个算法，并编写代码来序列化和反序列化二叉树。将树写入一个文件被称为“序列化”，读取文件后重建同样的二叉树被称为“反序列化”。 如何反序列化或序列化二叉树是没有限制的，你只需要确保可以将二叉树序列化为一个字符串，并且可以将字符串反序列化为原来的树结构。 对二进制树进行反序列化或序列化的方式没有限制，LintCode将您的serialize输出作为deserialize的输入，它不会检…

---

## [机器人移动（Robot Movement）](https://www.isharkfly.com/t/robot-movement/312)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:34pm UTC](https://www.isharkfly.com/t/robot-movement/312 "2020-07-24T19:34:47Z")

</div>

中文标题【机器人移动】 这个题目是 Kayak 发布的代码挑战题目。 我认为题目本身描述的不是十分清楚，方法需要返回结果，但是结果没有说明是机器人最后的坐标位置，还是最后的坐标位置距离原点的距离。同时，机器人的初始化方向等都没有十分明确的定义。 根据测试数据，机器人应该是从下往上（初始化的方向）。因为如果初始化的方向不确定的话，最后的坐标值可能会不一致。 我这里假设程序应该返回的是机器人的最后坐标位置，并且初始化的位置为 (0,…

---

## [岛屿个数（Number of Islands）](https://www.isharkfly.com/t/number-of-islands/15124)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 1:53pm UTC](https://www.isharkfly.com/t/number-of-islands/15124 "2023-11-09T13:53:53Z")

</div>

描述 给一个01矩阵，求不同的岛屿的个数。 0代表海，1代表岛，如果两个1相邻，那么这两个1属于同一个岛。我们只考虑上下左右为相邻。 样例 在矩阵： \[ \[1, 1, 0, 0, 0\], \[0, 1, 0, 0, 1\], \[0, 0, 0, 1, 1\], \[0, 0, 0, 0, 0\], \[0, 0, 0, 0, 1\] \] 中有 3 个岛。 代码 GitHub 的源代码，请访问下面的链接： 点评 本质是…

---

## [合并 2 个有序链表（Merge Two Sorted Lists）](https://www.isharkfly.com/t/2-merge-two-sorted-lists/15123)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 1:50pm UTC](https://www.isharkfly.com/t/2-merge-two-sorted-lists/15123 "2023-11-09T13:50:18Z")

</div>

描述 将两个排序链表合并为一个新的排序链表。 样例 给出 1-\>3-\>8-\>11-\>15-\>null，2-\>null， 返回 1-\>2-\>3-\>8-\>11-\>15-\>null。 代码 GitHub 的源代码，请访问下面的链接： 点评 本题主要涉及到链表的遍历和比较，基本上不会太难。 可能在写法上有点绕。

---

## [带环链表（Linked List Cycle）](https://www.isharkfly.com/t/linked-list-cycle/15122)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 1:36pm UTC](https://www.isharkfly.com/t/linked-list-cycle/15122 "2023-11-09T13:36:34Z")

</div>

描述 给定一个链表，判断它是否有环。 样例 给出 -21-\>10-\>4-\>5, tail connects to node index 1，返回 true。 这里解释下，题目的意思，在英文原题中，tail connects to node index 1 表示的是节点 5 还要链接回索引号 为 1 的节点。 一个典型的带环链表如下： 挑战 不要使用额外的空间 代码 GitHub 的源代码，请访问下面的链接： 点评 链…

---

## [电话号码的字母组合（Letter Combinations of a Phone Number）](https://www.isharkfly.com/t/letter-combinations-of-a-phone-number/15121)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 1:29pm UTC](https://www.isharkfly.com/t/letter-combinations-of-a-phone-number/15121 "2023-11-09T13:29:37Z")

</div>

描述 给定一个字符串，字符串中不包含 01。返回这个字符串中所有可能的字母组合返回结果。 数字和字母的的映射图片，请参考下面的电话拨号盘。 样例 给定 "23" 返回 \["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"\] 代码 GitHub 的源代码，请访问下面的链接： 点评 本题目主要考察你对递归调用的熟练使用程度。 有关递归调用的方法，请自行脑补，或者 G…

---

## [找到最小数量的硬币（Minimum Coins）](https://www.isharkfly.com/t/minimum-coins/318)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 8:21pm UTC](https://www.isharkfly.com/t/minimum-coins/318 "2020-07-24T20:21:46Z")

</div>

中文标题【找到最小数量的硬币】 题目的要求比较简单，要求找到最小数量的硬币。 给定的硬币数量是 1，3， 5 英文描述 英文题目的要求请参考下图： 中文描述 主要要求是你手上已经有 1,3,5 面值的硬币。 在给定金额情况下，找到最少需要多少个硬币能够等于给定的价值。 思路和点评 这个算法的主要目的是利用你已有的面值，主要考察你对除法中的除数和余数的理解和如何利用这 2 个数值进行计算。 源代码 源代码和有关代码的更新…

---

## [格雷编码（Gray Code）](https://www.isharkfly.com/t/gray-code/15120)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 2:38am UTC](https://www.isharkfly.com/t/gray-code/15120 "2023-11-09T02:38:18Z")

</div>

描述 格雷编码是一个二进制数字系统，在该系统中，两个连续的数值仅有一个二进制的差异。 给定一个非负整数 n ，表示该代码中所有二进制的总数，请找出其格雷编码顺序。一个格雷编码顺序必须以 0 开始，并覆盖所有的 2n 个整数。 对于给定的 n，其格雷编码顺序并不唯一。 根据以上定义， \[0,2,3,1\] 也是一个有效的格雷编码顺序。 样例 给定 n = 2， 返回 \[0,1,3,2\]。其格雷编码顺序为： 00 - 0 01 - …

---

## [展平嵌套数组（Flatten Nested Arrays）](https://www.isharkfly.com/t/flatten-nested-arrays/314)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:36pm UTC](https://www.isharkfly.com/t/flatten-nested-arrays/314 "2020-07-24T19:36:31Z")

</div>

这个题目是在 PillPack 现场面谈的时候的一个题目。 中文描述 题目要求比较简单：\[1,2,\[3\],\[\[4\]\],5,6\] → \[1,2,3,4,5,6\] 就是数组中嵌套数组，考察一个数组\[1,2,\[3\],\[\[4\]\],5,6\]。 你怎么能够输出 1,2,3,4,5,6（并不要求按照顺序输出）。 这里是一个嵌套数组，你需要将这个数组中的值全部取出来。 思路和点评 不清楚其他语言中这个数据结构怎么存储，我假设的是在 Java 中…

---

## [点积（Dot Product）](https://www.isharkfly.com/t/dot-product/15119)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 2:27am UTC](https://www.isharkfly.com/t/dot-product/15119 "2023-11-09T02:27:33Z")

</div>

描述 给出两个数组，求它们的点积。(Wikipedia) 如果没有点积则返回-1 样例 Input:A = \[1,1,1\] B = \[2,2,2\] Output:6 代码 请单击下面链接在 GitHub 上查看最新的源代码： 点评 这个问题的关键是要了解点积是如何进行计算的。 对输入的数组还需要进行 NULL 的校验和计算。 摘录点积的计算方法如下： 向量是由n个实数组成的一个n行1列（n1）或一个1行n列（1n）的有…

---

## [上下计数（Count Up Down）](https://www.isharkfly.com/t/count-up-down/313)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:35pm UTC](https://www.isharkfly.com/t/count-up-down/313 "2020-07-24T19:35:47Z")

</div>

这个题目是 Kayak 发布的代码挑战题目。 最简单的描述就是不使用循环，输出 0 到 5，然后同样不是会用循环的方式再次输出 5 到 0。 英文描述 Part 1 Write a program that counts in sequential order when given a start and end value - without using any iterative programing loops, i.e. w…

---

## [二叉树的层次遍历（Binary Tree Level Order Traversal）](https://www.isharkfly.com/t/binary-tree-level-order-traversal/15118)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 2:16am UTC](https://www.isharkfly.com/t/binary-tree-level-order-traversal/15118 "2023-11-09T02:16:02Z")

</div>

中文描述 给出一棵二叉树，返回其节点值的层次遍历（逐层从左往右访问） 样例 给一棵二叉树 {3,9,20,#,#,15,7} ： 3 / \\ 9 20 / \\ 15 7 返回他的分层遍历结果： \[ \[3\], \[9,20\], \[15,7\] \] 挑战 挑战1：只使用一个队列去实现它 挑战2：用BFS算法来做 思路和点评 这个程序可以使用队列的广度优先算法来进行遍历。 需要注意的是，因为在输出结…

---

## [Lambda Evens（Lambda 偶数）](https://www.isharkfly.com/t/lambda-evens-lambda/317)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:44pm UTC](https://www.isharkfly.com/t/lambda-evens-lambda/317 "2020-07-24T19:44:27Z")

</div>

英文题目 题目的英文表述成参考： 中文描述 题目要求比较简单，使用 Lambda 表达式写一个函数，找到给出字符串中的偶数。 思路点评 你需要对 Lambda 表达式比较熟悉，需要知道 Lambda 是什么。在 Java 世界中，Lambda 是在 Java 8 中引进的一个表达式。属于函数式。 近来也用得越来越多，最好对 Lambda 有所了解。 同时，你还要有基本的Java 字符拆分 API 的了解。很多题目可能不能…

---

## [二叉树（Binary Tree）](https://www.isharkfly.com/t/binary-tree/15117)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 9, 2023, 1:31am UTC](https://www.isharkfly.com/t/binary-tree/15117 "2023-11-09T01:31:31Z")

</div>

二叉树是一种非常重要的数据结构，很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树，有深度遍历和广度遍历，深度遍历有前序、中序以及后序三种遍历方法，广度遍历即我们平常所说的层次遍历。 因为树的定义本身就是递归定义，因此采用递归的方法去实现树的三种遍历不仅容易理解而且代码很简洁，而对于广度遍历来说，需要其他数据结构的支撑，比如队列。 遍历 二叉树的遍历你可能会在各种面试和算法题目中遇到。 这里主要讨论下面几种种遍历算法。在遍…

---

## [Binary Gap（二进制空白）](https://www.isharkfly.com/t/binary-gap/311)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:33pm UTC](https://www.isharkfly.com/t/binary-gap/311 "2020-07-24T19:33:06Z")

</div>

英文描述 A binary gap within a positive integer N is any maximal sequence of consecutive zeros that is surrounded by ones at both ends in the binary representation of N. For example, number 9 has binary representation 1001…

---

## [下一个斐波拉契数（Next Fibonacci Number）](https://www.isharkfly.com/t/next-fibonacci-number/15116)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 8, 2023, 7:04pm UTC](https://www.isharkfly.com/t/next-fibonacci-number/15116 "2023-11-08T19:04:28Z")

</div>

中文标题【下一个斐波拉契数】 Write a program that takes input of integer N, followed by N more integers. For each integer, output the next fibonacci number after it. Fibonacci number: Any number that belongs to the fibonacci series…

---

## [Java 计算阿姆斯特朗数](https://www.isharkfly.com/t/java/15114)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [November 8, 2023, 12:39am UTC](https://www.isharkfly.com/t/java/15114 "2023-11-08T00:39:48Z")

</div>

什么是阿姆斯特朗数？ 如果一个n位正整数等于其各位数字的n次方之和，则称该数为阿姆斯特朗数。 例如1^3 + 5^3 + 3^3 = 153 当n=3时，又称水仙花数，特指一种三位数，其各个数之立方和等于该数。 水仙花数共有4个，分别为：153、370、371、407。 如果是 4 位的阿姆斯特朗数的计算方法。 Java 实现 可以用下面的 Java 代码来进行实现，实现的过程也非常简单，只需要对数字进行拆分就好…

---

## [如何用 Java 判断一个给定的数是不是素数](https://www.isharkfly.com/t/java/13749)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [September 23, 2021, 7:46pm UTC](https://www.isharkfly.com/t/java/13749 "2021-09-23T19:46:39Z")

</div>

有关素数的定义：质数又称素数。一个大于1的自然数，除了1和它自身外，不能被其他自然数整除的数叫做质数；否则称为合数（规定1既不是质数也不是合数）。 生成素数的算法 在我们论坛中我们给出了一个有关素数生成算法。 这个是一个公司的面试题目，请参考 Prime numbers from 1 to 100 (打印 100 以内的素数) 页面中的内容。 如何判断一个数是不是素数 为什么要判断一个数是不是素数？因为质数 非常重要，随之数字越来…

---

## [A "word-wrap" functionality（一个字符串包裹函数）](https://www.isharkfly.com/t/a-word-wrap-functionality/13452)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [April 29, 2021, 2:43pm UTC](https://www.isharkfly.com/t/a-word-wrap-functionality/13452 "2021-04-29T14:43:14Z")

</div>

问题描述 下面是有关这个问题的描述部分。 英文 We are building a word processor and we would like to implement a “word-wrap” functionality. Given a list of words followed by a maximum number of characters in a line, return a collection of st…

---

## [First Unique Character in a String (找到一个字符串中第一个不重复的字符)](https://www.isharkfly.com/t/first-unique-character-in-a-string/13448)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 1\
**Last updated:** [April 29, 2021, 4:03pm UTC](https://www.isharkfly.com/t/first-unique-character-in-a-string/13448 "2021-04-29T16:03:19Z")

</div>

问题描述 下面是有关这个问题的描述部分。 英文 Given a string s, return the first non-repeating character in it and return its index. If it does not exist, return -1. 中文 针对给定的一个字符串 s，你需要写一个算法，返回给定字符串中不重复字符的位置（index），如果所有的字符在给定的字符串中都有重复的话，那么你…

---

## [Prime numbers from 1 to 100 (打印 100 以内的素数)](https://www.isharkfly.com/t/prime-numbers-from-1-to-100-100/13450)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [April 29, 2021, 3:24am UTC](https://www.isharkfly.com/t/prime-numbers-from-1-to-100-100/13450 "2021-04-29T03:24:40Z")

</div>

问题描述 下面是有关这个问题的描述部分。 中文 题目要求非常简单，就是将 100 以内的素数打印出来就可以了。 在做题之前，需要了解的就是有关素数的定义：质数又称素数。一个大于1的自然数，除了1和它自身外，不能被其他自然数整除的数叫做质数；否则称为合数（规定1既不是质数也不是合数）。 质数被利用在密码学上，所谓的公钥就是将想要传递的信息在编码时加入质数，编码之后传送给收信人，任何人收到此信息后，若没有此收信人所拥有的密钥，则解密的…

---

## [哈希算法](https://www.isharkfly.com/t/topic/13442)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [April 24, 2021, 4:21pm UTC](https://www.isharkfly.com/t/topic/13442 "2021-04-24T16:21:25Z")

</div>

哈希算法是什么。 为什么要哈希算法 我们的哈希算法是怎么实现的

---

## [Binomial Coefficient（二项式系数）](https://www.isharkfly.com/t/binomial-coefficient/316)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:39pm UTC](https://www.isharkfly.com/t/binomial-coefficient/316 "2020-07-24T19:39:42Z")

</div>

In mathematics, any of the positive integers that occurs as a coefficient in the binomial theorem is a binomial coefficient. Commonly, a binomial coefficient is indexed by a pair of integers n ≥ k ≥ 0 and is written {\\di…

---

## [Two Sum](https://www.isharkfly.com/t/two-sum/310)

<div class="topic-metadata">

**Author:** [@honeymoose](https://www.isharkfly.com/u/honeymoose)\
**Replies:** 0\
**Last updated:** [July 24, 2020, 7:31pm UTC](https://www.isharkfly.com/t/two-sum/310 "2020-07-24T19:31:22Z")

</div>

英文来源：https://leetcode.com/problems/two-sum/description/ Given an array of integers, return indices of the two numbers such that they add up to a specific target. You may assume that each input would have exactly one so…
