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 ...
随机推荐
- 51Nod:1265 四点共面
计算几何 修改隐藏话题 1265 四点共面 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出三维空间上的四个点(点与点的位置均不相同),判断这4个点 ...
- spring boot下mybatis遇到No operations allowed after connection closed.
在配置文件中添加 # for initial,min,maxspring.datasource.initialSize=5spring.datasource.minIdle=5spring.datas ...
- CTF-练习平台-Social之 密码?
一.密码? 看到题目提示是“张三”“生日”,再联系到我们设置密码时一般是名字的拼音首字母加生日,所以猜测是:zs19970315尝试后正确.
- 《DSP using MATLAB》Problem 4.14
代码: %% ---------------------------------------------------------------------------- %% Output Info a ...
- 虚拟机CentOS的NAT模式联网和SecureCRT远程登录管理工具
Cenos7 发生了很大的变化,不过也是直接配置网络,从启网卡,从启机器,crt链接 https://blog.csdn.net/gebitan505/article/details/54584213 ...
- 剑指offer-特定二维数组中查找一个元素是否存在-二分搜索-二维数组
int [][] array ={ {1,2,8,9}, {2,4,9,12}, {4,7,10,13}, {6,8,11,19} }; 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都 ...
- MySQL--修改MySQL账号密码
##使用mysqladmin进行修改 mysqladmin -u username -h hostname password 'new password'; ##使用set命令进行修改 SET PAS ...
- Python包管理工具easy_install使用
easy_install是python的包管理工具,可以方便的下载.安装.更新python包,并可以自动处理相关依赖.类似于ruby的gem,java的maven,nodejs的npm. 安装方法有两 ...
- xhan/qqbot试用
xhan/qqbot试用 啥是qqbot?基于WebQQ协议的QQ机器人.命令行工具,Hubot支持! 怎样安装?首先要安装CoffeeScript windows:安装nodejs:主页上就有下载C ...
- JVM 之:Class 类文件结构
类文件结构 Class 文件是一组以8位字节为基础单位的二进制流,各个数据项目严格按照顺序紧凑地排列在 Class 文件中,中间没有添加任何分隔符,这使得整个 Class 文件中存储的内容几乎全部都是 ...