Sum All Numbers in a Range(两数之间数字总和)
题目:我们会传递给你一个包含两个数字的数组。返回这两个数字和它们之间所有数字的和。
最小的数字并非总在最前面。
/*方法一: 公式法 (首+末)*项数/2 */
/*两个数比较大小的函数*/
function compare(value1,value2){
if(value1 < value2){
return -1;
}else if(value1 > value2){
return 1;
}else{
return 0;
}
}
function sumAll(arr) {
arr.sort(compare);
var sum= (arr[0] + arr[1])*(arr[1]-arr[0]+1)/2;
return sum;
/*return (arr[0] + arr[1])*(arr[1]-arr[0]+1)/2;*/
}
sumAll([1, 4]); /*方法一: 公式法 (首+末)*项数/2 */
/*Math.abs() 取两数运算绝对值*/
function sumAll(arr) {
return (arr[0] + arr[1])*(Math.abs(arr[0] - arr[1]) + 1)/2;
} /*方法二:。。。 */
Sum All Numbers in a Range(两数之间数字总和)的更多相关文章
- [LeetCode] 167. Two Sum II - Input array is sorted 两数和 II - 输入是有序的数组
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- [LeetCode] 653. Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- LeetCode 170. Two Sum III - Data structure design (两数之和之三 - 数据结构设计)$
Design and implement a TwoSum class. It should support the following operations: add and find. add - ...
- [LeetCode] Two Sum IV - Input is a BST 两数之和之四 - 输入是二叉搜索树
Given a Binary Search Tree and a target number, return true if there exist two elements in the BST s ...
- 167. Two Sum II - Input array is sorted两数之和
1. 原始题目 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数. 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2. 说明 ...
- [LeetCode] Two Sum II - Input array is sorted 两数之和之二 - 输入数组有序
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- 167 Two Sum II - Input array is sorted 两数之和 II - 输入有序数组
给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数.函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2.请注意,返回的下标值(i ...
- Sum All Numbers in a Range
我们会传递给你一个包含两个数字的数组.返回这两个数字和它们之间所有数字的和. 最小的数字并非总在最前面. 这是一些对你有帮助的资源: Math.max() Math.min() Array.reduc ...
- 002 Add Two Numbers 链表上的两数相加
You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...
随机推荐
- 20155316 2016-2017-2 《Java程序设计》第5周学习总结
教材学习内容总结 这周总结 try catch语法 异常继承结构 throw finally AutoCloseable接口 Collection Map Lambda表达式 上周总结 三个关键 类与 ...
- MVC 模型 视图, 控制器 写 三级联动
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title>& ...
- 《DSP using MATLAB》Problem 4.23
代码: %% ------------------------------------------------------------------------ %% Output Info about ...
- MySQL--Alter Table注意事项
======================================================================== ALTER TABLE 和FLUSH TABLE导致的 ...
- KindEditor 上传文件 在Asp.net中的使用
以前一直用FCK编辑器,因为配置比较简单,但是发现Kindeditor这个编辑器更加好看,更加灵活,就用了下. 但是发现在上传文件的时候,出现了大问题,弄了我好久的时间,为了记录下,或许能帮助到您,共 ...
- 容器、应用服务器和web服务器的区别
什么是容器 servlet没main()方法.它们受控于另一个java应用,这个java应用称为容器(Container)Tomcat是这样的容器,Web服务器应用(Apache)得到一个指向serv ...
- mysql 官方docker镜像使用教程
首先是pull image,这里我拉取的是5.x版本最新版: docker pull mysql:5 拉下来以后大可以按照官方的说明无脑启动,但是外部无法访问,所以绑定端口:docker run -- ...
- Vue 介绍
1. 条件 效果图. 如果seen为false,文字将消失 2. 循环 script里定义数据 效果 3. 事件处理 效果如下图, hello world被逆转了
- 更新上篇文章 调用三级目录文章内容 dede频道页实现三级栏目嵌套调用文章
原文:http://www.wuaie.com/?p=66 源码改写 $typeid = $row['id']; if((class_exists('PartView'))) { $pv = new ...
- 淘宝用户api 如何获得App Key和API Secret
下面我们通过截图的方式详细说明申请淘宝应用的步骤. 一.访问淘宝开放平台http://open.taobao.com/ 申请成为合作伙伴 二.填写个人信息申请入住 三.点击创建应用 四.填写应用名称, ...