zoukankan      html  css  js  c++  java
  • Math Three

    (1)Arranging Coins

    解题思路一:这个想法是关于二次方程,得到算术和的公式是sum =(x + 1)* x / 2

    所以对于这个问题,如果我们知道和,那么我们可以知道x =(-1 + sqrt(8 * n + 1))/ 2 向下取整。

    代码如下:

    1 public class Solution {
    2     public int arrangeCoins(int n) {
    3         return (int)((-1 + Math.sqrt(1 + 8 * (long)n)) / 2);
    4     }
    5 }
    View Code

    解题思路二:利用给定的n不停的减去1,2…,直至n小于要减去的数字。

    代码如下:

     1 public class Solution {
     2     public int arrangeCoins(int n) {
     3         int result = 0;
     4         int add = 1;
     5         while (n >= add) {
     6             n -= add;
     7             add++;
     8             result++;
     9         }
    10         return result;
    11     }
    12 }
    View Code

    (2)Factorial Trailing Zeroes

    解题思路:

    因为所有的trailing zero来自因子5 * 2。

    但有时一个数字可能有几个5因子,例如,25有两个5因子,125有三个5因子。 在n! 操作,因子2总是充足。 所以我们只计算从1到n的所有数字中的5个因子。

    计算5的个数时, 最简单的方法是 SUM(N/5^1,  N/5^2, N/5^3...)

    代码一:

    1 public class Solution {
    2     public int trailingZeroes(int n) {
    3        return n == 0 ? 0 : n / 5 + trailingZeroes(n / 5);
    4     }
    5 }
    View Code

    代码二:

     1 public class Solution {
     2     public int trailingZeroes(int n) {
     3         if (n < 1) {
     4             return 0;
     5         }
     6         int number = 0;
     7         while (n / 5 != 0) {
     8             n /= 5;
     9             number += n;
    10         }
    11         return number;
    12     }
    13 }
    View Code

    (3)Palindrome Number

    解题思路:判断一个数字是否是回文数字,不能像字符串那样一个字符一个字符比较!!!所以将数字反转如果与原数值相同就是回文数字。

    代码如下:

     1 public class Solution {
     2     public boolean isPalindrome(int x) {
     3         if (x < 0) {
     4             return false;
     5         }
     6         return x == reverse(x);
     7     }
     8     public int reverse(int x) {
     9         int rst = 0;
    10         while (x != 0) {
    11             rst = rst * 10 + x % 10;
    12             x /= 10;
    13         }
    14         return rst;
    15     }
    16 }
    View Code

    (4)Rectangle Area

    解题思路:

    找到重复部分矩形的坐标表示,两个大矩形面积相加减去重叠部分面积即可。

    代码如下:

    1 public class Solution {
    2     public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) {
    3         int left = Math.max(A,E), right = Math.max(Math.min(C,G), left);
    4         int bottom = Math.max(B,F), top = Math.max(Math.min(D,H), bottom);
    5         return (C-A)*(D-B) - (right-left)*(top-bottom) + (G-E)*(H-F);       
    6     }
    7 }
    View Code

     (5)Reverse Integer

    解题思路简单明了。

    代码如下:

     1 public class Solution {
     2     public int reverse(int x) {
     3         int result = 0;
     4         while (x != 0) {
     5             int tail = x % 10;
     6             int newResult = result * 10 + tail;
     7             if ((newResult - tail) / 10 != result) { 
     8                 return 0; 
     9             }//判断是否溢出
    10             result = newResult;
    11             x /= 10;
    12         }
    13         return result;
    14     }
    15 }
    View Code
  • 相关阅读:
    《RTC — RTC相关操作以及如何同步系统时间》
    《关闭服务器,再次启动服务器提示bind:address already in use》
    《海思中内存分配和优化》
    《开发板 — 查看共享内存情况》
    《通过himm读取指定引脚并保存在共享内存中》
    《开发板 — 格式化TF卡》
    《网络编程 — UDP》
    《网络编程 — 服务器中bind的ip地址是什么》
    《使用gdb中core dump》
    《 Linux套接字编程中的5个隐患 》
  • 原文地址:https://www.cnblogs.com/struggleli/p/6209464.html
Copyright © 2011-2022 走看看