找出 int 数组的平衡点 树算法 二叉树 / 平衡二叉树 / 满二叉树 / 完全二叉树 / 二叉查找树 balanced binary tree



找出 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

  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;
}
}


树算法

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

题目

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

balanced binary tree

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

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

剑指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 不用加减乘除做加法


​​