The definition of integer average is the highest smaller integer if average is floating point number. Also the condition if that they can not use any typecasting or any datatype other than int.

Example: a = 4, b = 5, avg = 4 a = 4, b = 6, avg = 5 a = -4, b = -6, avg = -5 a = 4, b = -5, avg = -1 a = -4, b = -5, avg = -5

-9%2 == -1 不是1, 所以负数%2==1不是奇数判定标准。但是偶数标准仍是%2 == 0

 package FindIntegerAverage;

 public class Solution {
public int aver(int a, int b) {
if ((a+b)%2 == 0)return (int)(a+b)/2;
else return (int)(a+b-1)/2;
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
int res = sol.aver(-4, -5);
System.out.print(res);
} }

M面经Prepare: Find integer Average of 2 integers.的更多相关文章

  1. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  2. Moving Average from Data Stream

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  3. LeetCode Moving Average from Data Stream

    原题链接在这里:https://leetcode.com/problems/moving-average-from-data-stream/ 题目: Given a stream of integer ...

  4. LeetCode 346. Moving Average from Data Stream (数据流动中的移动平均值)$

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  5. Moving Average from Data Stream LT346

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  6. [leetcode]346. Moving Average from Data Stream滑动窗口平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

  7. MyBatis3.4.0以上的分页插件错误:Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named prepare. Cause: java.lang.NoSuchMethodException: org.apache.ibatis.executor.stateme

    错误: Could not find method on interface org.apache.ibatis.executor.statement.StatementHandler named p ...

  8. 346. Moving Average from Data Stream数据窗口流中位数的数据结构设计

    [抄题]: Given a stream of integers and a window size, calculate the moving average of all integers in ...

  9. [LeetCode] 346. Moving Average from Data Stream 从数据流中移动平均值

    Given a stream of integers and a window size, calculate the moving average of all integers in the sl ...

随机推荐

  1. 如何解决php 生成验证码图片不显示问题

    最近遇到一个问题,就是验证码在别人的电脑上可以显示,但是我自己的电脑上去不能.原因找了好久,哈哈,终于找到了!现在给大家分享一下: 程序: <?php $w = 80; //设置图片宽和高 $h ...

  2. 如何删除docker images/containers

    docker images往往不知不觉就占满了硬盘空间,为了清理冗余的image,可采用以下方法: 1.进入root权限 sudo su 2.停止所有的container,这样才能够删除其中的imag ...

  3. coursera-miniproject Pang任务总结

    Mini_project开发过程 1.通过添加代码画出一个在乒乓球台移动的球.我们提醒你给乒乓台添加位置信息给draw handler像在”Motion"那节课第二部分介绍的那样 2.添加代 ...

  4. hiho42 : 骨牌覆盖问题·二

    描述 上一周我们研究了2xN的骨牌问题,这一周我们不妨加大一下难度,研究一下3xN的骨牌问题?所以我们的题目是:对于3xN的棋盘,使用1x2的骨牌去覆盖一共有多少种不同的覆盖方法呢?首先我们可以肯定, ...

  5. Running Kafka At Scale

    https://engineering.linkedin.com/kafka/running-kafka-scale If data is the lifeblood of high technolo ...

  6. 关于jQuery学习

    ≡[..]≡≡[..]≡ 所有的实例都位于document.ready里面--为了防止文档在未完全加载之前就运行函数导致操作失败. $(document).ready(function(){ --- ...

  7. nginx生产配置

    user www www; worker_processes 8; error_log /data/logs/nginx_error.log crit; pid /usr/local/webserve ...

  8. 【转】简单的 Laravel 5 REST API

    Introduction Almost all successful internet based companies have APIs. API is an acronym for Applica ...

  9. java类加载过程

    类(型)的生命周期--装载.连接.初始化.卸载 Java虚拟机通过装载.连接和初始化一个Java类型,使该类型可以被正在运行的Java程序所使用. 1.       装载 装载阶段包括三个基本动作: ...

  10. 【Android开发学习笔记】【第四课】基础控件的学习

    通过一个简单的例子来学习下面几种控件: 1.TextView:简单的文本显示控件 2.EditText:可以编辑的文本框 3.Button:按钮 4.Menu:这里指的是系统的Menu 5.Toast ...