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 ...
随机推荐
- 斐波那契数列的5种python实现写法
斐波那契数列的5种python写法 斐波那契数列(Fibonacci sequence),又称黄金分割数列.因数学家列昂纳多·斐波那契(Leonardoda Fibonacci)以兔子繁殖 ...
- 【转】python mysql数据库 'latin-1' codec can't encode character错误问题解决
UnicodeEncodeError: 'latin-1' codec can't encode character "UnicodeEncodeError:'latin-1' code ...
- wpf 客户端【JDAgent桌面助手】开发详解(三) 瀑布流效果实现与UI虚拟化优化大数据显示
目录区域: 业余开发的wpf 客户端终于完工了..晒晒截图 wpf 客户端[JDAgent桌面助手]开发详解-开篇 wpf 客户端[JDAgent桌面助手]详解(一)主窗口 圆形菜单... wpf 客 ...
- 特殊字符搜索网站 http://symbolhound.com/
最近在学习makefile,想搜索一下 $@是啥意思,结果google由于忽略了特殊字符,结果啥也没找到, 后来在stackoverflow上看到了别人同样的问题 http://stackoverfl ...
- vue-progressbar 知识点
使用步骤: 安装 import.Vue.use() 组件里,created() 和 mounted() 复制官方github地址上的代码 官方github地址:https://github.com/h ...
- puppet 工作原理
Puppet的工作细节分成如下几个步骤: 1.客户端puppetd调用facter,facter会探测出这台主机的一些变量如主机名.内存大小.IP地址等.然后puppetd把这些信息发送到服务器端. ...
- 峰Spring4学习(5)bean之间的关系和bean的作用范围
一.bean之间的关系: 1)继承: People.java实体类: package com.cy.entity; public class People { private int id; priv ...
- 2018-2019 20165226 网络对抗 Exp1 PC平台逆向破解
2018-2019 20165226 网络对抗 Exp1 PC平台逆向破解 目录 一.逆向及Bof基础实践说明 二.直接修改程序机器指令,改变程序执行流程 三.通过构造输入参数,造成BOF攻击,改变程 ...
- applicatonContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Bootstrap table的一些简单使用总结
在GitHub上Bootstrap-table的源码地址是:https://github.com/wenzhixin/bootstrap-table Bootstrap-table的文档地址:http ...