一个程序在创建等差数列时出了Bug,等差数列中某一项被丢失了,其他项好完好,请找出这个丢失项。
比如:1,3,5,9,11,这里的丢失项很明显是7。
你得写一个函数,findMissing (list) ,它接收一个等差数列列表,列表至少有3个元素,丢失项不会是第一个或最后一个。
来个例子:
findMissing ([1,3,5,9,11]); // 7
PS:这是Facebook在interviewstreet上的工程师挑战例题,大家拿张白纸和笔,写写划划应该很快能解出来。
好了,咋们来看看解题思路:
我个人感觉,对付此类问题,也就是说,至少3个元素的题目,它往往暗示了你,三个元素内要“决出胜负”。
对于这个题目,三个元素内要推算出等差数列的公差。
先来看前三个元素的例子:
7,5,1
大家一眼就可以看出,第一个元素和第二个元素相差-2,第二个元素和第三个元素相差-4,那么公差是-2。
所以,对于递减数列这种情况,公差是:
Math.max(list[1]-list[0],list[2]-list[1]);
再看另一个倒过来的例子:
1,5,7
第一个元素和第二个元素相差4,第二个元素和第三个元素相差2,那么公差是2。
对于递增数列的情况,公差就是:
Math.min(list[1]-list[0],list[2]-list[1]);
既然算出来了公差,那剩下的事儿就好办了,从第一个数依次用公差,一一比对列表中的数,遇到不相等的,那就是Miss的项了。
var findMissing = function (list) { var diff1$0 = list[1]-list[0]; var diff2$1 = list[2]-list[1]; var diff = diff1$0 < 0 ? Math.max(diff1$0,diff2$1) : Math.min(diff1$0,diff2$1); for(var i=list[0],j=0;j<=list.length;i+=diff,j++){ if(i !== list[j]){ return i; } } }
以上就是JavaScript趣题:出Bug的等差数列 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Detailed explanation of JavaScript string replacement method and FAQ This article will explore two ways to replace string characters in JavaScript: internal JavaScript code and internal HTML for web pages. Replace string inside JavaScript code The most direct way is to use the replace() method: str = str.replace("find","replace"); This method replaces only the first match. To replace all matches, use a regular expression and add the global flag g: str = str.replace(/fi

So here you are, ready to learn all about this thing called AJAX. But, what exactly is it? The term AJAX refers to a loose grouping of technologies that are used to create dynamic, interactive web content. The term AJAX, originally coined by Jesse J

10 fun jQuery game plugins to make your website more attractive and enhance user stickiness! While Flash is still the best software for developing casual web games, jQuery can also create surprising effects, and while not comparable to pure action Flash games, in some cases you can also have unexpected fun in your browser. jQuery tic toe game The "Hello world" of game programming now has a jQuery version. Source code jQuery Crazy Word Composition Game This is a fill-in-the-blank game, and it can produce some weird results due to not knowing the context of the word. Source code jQuery mine sweeping game

Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.

This tutorial demonstrates how to create a captivating parallax background effect using jQuery. We'll build a header banner with layered images that create a stunning visual depth. The updated plugin works with jQuery 1.6.4 and later. Download the

The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.

Matter.js is a 2D rigid body physics engine written in JavaScript. This library can help you easily simulate 2D physics in your browser. It provides many features, such as the ability to create rigid bodies and assign physical properties such as mass, area, or density. You can also simulate different types of collisions and forces, such as gravity friction. Matter.js supports all mainstream browsers. Additionally, it is suitable for mobile devices as it detects touches and is responsive. All of these features make it worth your time to learn how to use the engine, as this makes it easy to create a physics-based 2D game or simulation. In this tutorial, I will cover the basics of this library, including its installation and usage, and provide a

This article demonstrates how to automatically refresh a div's content every 5 seconds using jQuery and AJAX. The example fetches and displays the latest blog posts from an RSS feed, along with the last refresh timestamp. A loading image is optiona


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft