找出 int 数组的平衡点

左右两边和相等, 若存在返回平衡点的值(可能由多个); 若不存在返回 -1;

``java

int [] arr = {2,3,4,2,4};


```js
const arr = [2,3,4,2,4];

https://repl.it/@xgqfrms/find-number-array-balance-point

https://repl.it/@xgqfrms/find-Int-array-balance-point

  1. ts
// 找出 Int 数组平衡点

/**
* 整形数组平衡点问题:平衡点指左边的整数和等于右边的整数和,
* 求出平衡点位置,要求输入的数组可能是GB级
*
* 要求找出整型数组的一个平衡点(如果要找出所有平衡点的话,按此方法需要把每一个平衡点都存起来)
*/ const log = console.log; // 'public' modifier cannot appear on a module or namespace element.ts
// public class IntArrayBalancePoint { class IntArrayBalancePoint {
constructor(args: String[]) {
log(`args`, args)
// const a: Object = [- 7 , 1, 5, 2, -5, 1];
// const b: Object = [2, 3, 4, 2, 4];
// const c: Object = [2, 3, 4, 3, 2];
// const a: Number[] = [- 7 , 1, 5, 2, -5, 1];
// const b: Number[] = [2, 3, 4, 2, 4];
// const c: Number[] = [2, 3, 4, 3, 2];
// interface Number
// An object that represents a number of any kind.
// All JavaScript numbers are 64-bit floating-point numbers.
// const a: number[] = [- 7 , 1, 5, 2, -5, 1];
// const b: number[] = [2, 3, 4, 2, 4];
// const c: number[] = [2, 3, 4, 3, 2];
// const t = new IntArrayBalancePoint([]);
// log(t.findBalancePoint(a));
// log(t.findBalancePoint(b));
// log(t.findBalancePoint(c));
}
public findBalancePoint(a: number[]) {
// findBalancePoint(a: number[]) {
const len = a.length || 0;
if (a === null) {
return -1;
}
let sum = 0;
let subSum = 0;
for (let i = 0; i < len; i ++) {
sum += a[i];
}
for (let i = 0; i < len; i++) {
if (subSum === sum - subSum - a[i]) {
// log(a[i]);
return a[i];
} else {
subSum += a[i];
}
}
return -1;
}
} const a: number[] = [- 7 , 1, 5, 2, -5, 1];
const b: number[] = [2, 3, 4, 2, 4];
const c: number[] = [2, 3, 4, 3, 2]; const t = new IntArrayBalancePoint([]);
log(t.findBalancePoint(a));
log(t.findBalancePoint(b));
log(t.findBalancePoint(c)); // args []
// -5
// -1
// 4

refs

https://www.iteye.com/blog/jerryqiu-252422

https://www.cnblogs.com/tomato0906/articles/7417798.html

  1. java
package find_Int_array_balance_point;

// 找出 Int 数组平衡点

/**
* 整形数组平衡点问题:平衡点指左边的整数和等于右边的整数和,
* 求出平衡点位置,要求输入的数组可能是GB级
*
* 要求找出整型数组的一个平衡点(如果要找出所有平衡点的话,按此方法需要把每一个平衡点都存起来)
*/ public class IntArrayBalancePoint {
public static void main(String[] args) {
int[] a = { - 7 , 1, 5, 2, -5, 1} ;
int[] b = {2, 3, 4, 2, 4} ;
int[] c = {2, 3, 4, 3, 2} ;
IntArrayBalancePoint t = new IntArrayBalancePoint();
System.out.println(t.findBalancePoint(a));
System.out.println(t.findBalancePoint(b));
System.out.println(t.findBalancePoint(c));
// t.findBalancePoint(a);
// t.findBalancePoint(b);
// t.findBalancePoint(c);
}
public int findBalancePoint(int[] a) {
if (a == null) {
return -1;
}
long sum = 0l;
long subSum = 0l;
for ( int i = 0 ; i < a.length; i ++ ) {
sum += a[i];
}
for (int i = 0; i < a.length; i ++ ) {
if (subSum == sum - subSum - a[i]) {
// System.out.println(a[i]);
return a[i];
} else {
subSum += a[i];
}
}
return -1;
}
}

树算法

二叉树 / 平衡二叉树 / 满二叉树 / 完全二叉树 / 二叉查找树

https://juejin.im/entry/5afb9fb66fb9a07ab458cc0d

https://zhuanlan.zhihu.com/p/56066942

https://blog.csdn.net/qq_24885695/article/details/75268318

题目

根据一个数组,找出其平衡点,也就是该点左边的和等于右边的和; 一个数组可能有多个平衡点;

注意:数组至少有3个元素,才能有平衡点存在, 不存在返回 -1;

https://www.cnblogs.com/tomato0906/articles/7417798.html

balanced binary tree

https://leetcode-cn.com/problems/balanced-binary-tree/

JavaScript中的数据结构和算法学习

https://juejin.im/post/594dfe795188250d725a220a

剑指Offer笔记

https://xmoyking.github.io/2018/03/27/js-offer-algorithms5/

  1. 问题38 数字在排序数组中出现的次数
  2. 问题39 二叉树的深度

    2.1. 问题39.2 平衡二叉树
  3. 问题40 数组中只出现一次的数字
  4. 问题41 和为S的两个数字

    4.1. 问题41.2 和为S的连续正数序列
  5. 问题42 翻转单词顺序

    5.1. 问题42.2 左旋转字符串
  6. 问题43 n个骰子的点数
  7. 问题44 扑克牌的顺子
  8. 问题45 圆圈中最后剩下的数
  9. 问题46 求1+2+3+…+n
  10. 问题47 不用加减乘除做加法

xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!


找出 int 数组的平衡点 & 二叉树 / 平衡二叉树 / 满二叉树 / 完全二叉树 / 二叉查找树的更多相关文章

  1. 笔试题&amp;面试题:找出一个数组中第m小的值并输出

    题目:找出一个数组中第m小的值并输出. 代码: #include <stdio.h> int findm_min(int a[], int n, int m) //n代表数组长度,m代表找 ...

  2. 找出数字数组中最大的元素(使用Math.max函数)

    从汤姆大叔的博客里看到了6个基础题目:本篇是第1题 - 找出数字数组中最大的元素(使用Match.max函数) 从要求上来看,不能将数组sort.不能遍历.只能使用Math.max,所以只能从java ...

  3. javascript 找出数字数组中最大的数

    找出数字数组中最大的数 var Match = (function(){ var arr = null; var len = 0; return { max:function(arr,len){ ar ...

  4. 找出整数数组中出现次数超过数组长度一半的元素(Java)

    Question:数组中有一个数字出现的次数超过数组长度的一半,请找出这个数字 package com.study.zhipengs.test; import java.util.Arrays; im ...

  5. 【LeetCode】找出所有数组中消失的数字

    [问题] 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次.找到所有在 [1, n] 范围之间没有出现在数组中的数字. ...

  6. 找出如下数组中最大的元素和最小的元素, a[][]={{3,2,6},{6,8,2,10},{5},{12,3,23}}

    int [][]a={{3,2,6},{6,8,2,10},{5},{12,3,23}}; //先对二维数组进行遍历:然后把二维数组合成一个数组 int[] k=new int[11]; int q= ...

  7. 368. Largest Divisible Subset -- 找出一个数组使得数组内的数能够两两整除

    Given a set of distinct positive integers, find the largest subset such that every pair (Si, Sj) of ...

  8. 找出Java数组中不同的值

    public void deltp(PrintWriter out) { try{ PageData pd = new PageData(); pd = this.getPageData(); Str ...

  9. 15.找出如下数组中最大的元素和最小的元素, a[][]={{3,2,6},{6,8,2,10},{5},{12,3,23}}

    package com.bao; public class Erweim { public static void main(String[] args) { int[][]a={{3,2,6},{6 ...

随机推荐

  1. 「一本通 1.3 例 4」Addition Chains

    Addition Chains 题面 对于一个数列 \(a_1,a_2 \dots a_{m-1},a_m\) 且 \(a_1<a_2 \dots a_{m-1}<a_m\). 数列中的一 ...

  2. LOJ10199轻拍牛头

    题目描述 原题来自:USACO 2008 Dec. Silver 今天是 Bessie 的生日,并且现在是聚会的游戏时间.Bessie 让编号为 1~N 的 N 头奶牛围成一个圈坐(所以除了最后一头牛 ...

  3. LOJ10074架设电话线

    USACO 2008 Jan. Silver 在郊区有 N 座通信基站,P 条双向电缆,第 i 条电缆连接基站 Ai​ 和 Bi​.特别地,1 号基站是通信公司的总站,N 号基站位于一座农场中.现在, ...

  4. Java——I/O操作之拷贝文件

    功能描述: 利用文件输入输出流编写一个实现文件拷贝的程序,源文件名和目标文件名通过控制台输入. public static void main(String[] args) { Scanner in= ...

  5. JMeter多个请求按照比例并发的几种方式

    一.需求 在压测的过程中,为了能够压测整个链路,通常需要多个接口进行并发, 每个接口的请求比例不尽相同. 比如此时此刻,我在写博客,很多人在浏览博客,或者点赞.评论博客等等等,这些行为占比是不同的. ...

  6. cassandra权威指南读书笔记--客户端

    DataStax驱动最成熟.默认,驱动程序会使用第一个连接的节点作为支持的版本协议.如果集群存在高低版本的节点(比如升级场景),如果驱动先连接不同不同版本的节点,可能会出现不兼容.驱动支持压缩客户端和 ...

  7. 在.NetCore(C#)中使用ODP.NET Core+Dapper操作Oracle数据库

    前言 虽然一直在说"去IOE化",但是在国企和政府,Oracle的历史包袱实在太重了,甚至很多业务逻辑都是写在Oracle的各种存储过程里面实现的-- 我们的系统主要的技术栈是Dj ...

  8. HDOJ 1028 母函数分析

    #include<iostream>#include<cstring>using namespace std;int main(){    int c1[10000],c2[1 ...

  9. Educational Codeforces Round 88 (Rated for Div. 2) D、Yet Another Yet Another Task

    题意: 给你一个含n个数a1,a2...an的数组,你要找到一个区间[l,r],使得al+a(l+1)+...+a(r-1)+ar减去max(al,a(l+1),...,a(r-1),ar)的值尽可能 ...

  10. Educational Codeforces Round 89 (Rated for Div. 2) C Palindromic Paths

    题目链接:Palindromic Paths 题意: 给你一个n行m列的矩阵,这个矩阵被0或者1所填充,你需要从点(1,1)走到点(n,m).这个时候会有很多路径,每一条路径对应一个01串,你可以改变 ...