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 ...
随机推荐
- nmap扫描时的2个小经验
http://pnig0s1992.blog.51cto.com/393390/367558/ 1.我肉鸡的环境是Windows XP sp3,在使用nmap扫描外网的时候,提示我 pcap_open ...
- Xcode7( linker command failed with exit code 1)
”Build Settings”->”Enable Bitcode”设置为NO ,因为有些SDK不支持Bitcode
- CTF-练习平台-WEB之 计算题
四.计算题 打开连接 输入后发现只能输入一个数字,在火狐浏览器中按F12,打开查看器 ,如图所示修改最大长度 输入答案后验证,当当当~~flag出现
- pycharm PYTHONPATH
Hi brandenju! I believe os.chdir doesn't affect PYTHONPATH so changing your working directory at run ...
- leetcode:Reverse Words in a String【Python版】
class Solution: # @param s, a string # @return a string def reverseWords(self, s): ss = s.split(&quo ...
- streamsets k8s 部署试用
使用k8s 进行 streamsets的部署(没有使用持久化存储) k8s deploy yaml 文件 deploy.yaml apiVersion: extensions/v1beta1 kind ...
- nodejs express 学习
nodejs的大名好多人应该是听过的,而作为nodejs web 开发的框架express 大家也应该比较熟悉. 记录一下关于express API 的文档: express() 创建express ...
- Entity Framework常用的查询方式
Entity Framework支持的查询方式有三种 LINQ to Entities Entity SQL Native SQL [LINQ to Entities] LINQ(语言集成查询)是从V ...
- zmediaboard-Hi3518参数及配置
1.12_13.uboot的烧写和flash分区1_21.12.1.裸机烧录uboot(1)什么叫裸机烧录?设备是空白的,未经烧录的,就叫裸机.(2)裸机烧录一个设备有2种方案:1是用外部烧录器来烧录 ...
- 【转】每天一个linux命令(30): chown命令
原文网址:http://www.cnblogs.com/peida/archive/2012/12/04/2800684.html chown将指定文件的拥有者改为指定的用户或组,用户可以是用户名或者 ...