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. ip sevices

    http://www.ip138.com/ip2city.asp http://www.bliao.com/ip.phtml http://www.whereismyip.com/ http://ww ...

  2. jquery rotate

    网上发现一个很有意思的jQuery旋转插件,支持Internet Explorer 6.0+ .Firefox 2.0 .Safari 3 .Opera 9 .Google Chrome,高级浏览器下 ...

  3. jQuery对象与JS原生dom对象之间的转换

    jQuery就是JS的一个扩展库,工具库,提供很多方便快捷的方法,所以将JS对象转换为jQuery对象后,能更方便地操作这个对象.但是jQuery对象也不是万能的,有一些JS对象有的能,jQuery对 ...

  4. background:transparent的作用

    background的属性值 background : background-color | background-image | background-repeat | background-att ...

  5. [LeetCode]题解(python):093 Restore IP Addresses

    题目来源 https://leetcode.com/problems/restore-ip-addresses/ Given a string containing only digits, rest ...

  6. js判断手机访问web网站跳转到手机版

    第一种:直接JS脚本 <script type="text/javascript">try {var urlhash = window.location.hash;if ...

  7. 《Maven实战》阅读笔记

    java -versionmvn -vmvn help:system m2eclipse maven->install MAVEN_OPTS: -Xms128m -Xmx512mmvn clea ...

  8. 转帖:解决jquery.js在myeclipse中报错的问题

    转载地址:http://tieba.baidu.com/p/1993753087 从官方下载的jquery.js在myeclipse始终用个大大的红叉,看着很不爽,如何解决呢:jquery.js在my ...

  9. break , continue , exit

    break , continue , exit 例一:#!/bin/bash . /etc/init.d/functions `;do ];then #continue #没有数字3 break #e ...

  10. 选择年份 php的写法要比js简洁一些

    所以遇到下拉框默认选择的情况,用php写比较方便一些 <select type="text" class="form-control_2" name=&q ...