需要注意overflow,特别是Integer.MIN_VALUE这个数字。

需要掌握二分法。

不用除法的除法,分而治之的乘方

2. Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

基本等于合并数组,记得要最后加上carry位。

不需要考虑overflow,但是记得当输入两个空链表的时候输出0而不是空链表。

29. Divide Two Integers

Divide two integers without using multiplication, division and mod operator.

If it is overflow, return MAX_INT.

不能使用除法的除法,让除数不停地乘以二直到会大于被除数为止。

核心代码如下:

while (x >= y) {
int a = 0;
while (x >= (y << a)) {
a++;
}
a--;
result += (long)1 << a;
x -= y << a;
}

其中,x是被除数,y是除数,这段代码里,还需注意x,y都是long。1要被强制转换为long

这道题有很多corner case,具体见

[leetcode] 29. divide two integers

43. Multiply Strings

Given two numbers represented as strings, return multiplication of the numbers as a string.

Note:

  • The numbers can be arbitrarily large and are non-negative.
  • Converting the input string to integer is NOT allowed.
  • You should NOT use internal library such as BigInteger.
  • 按照我目前的解法没有什么corner case需要考虑,只要把输出数字前面多余的0去掉就可以了。但是现在的解法比较慢。有空的时候再改进吧。

50. Pow(x, n)

Implement pow(xn).

1. 0的0次方是1

2. 判断x是否为0,n是否为0,x是否是1

3. 判断x的正负性,判断n的正负性。

4. 注意n是Integer.MIN_VALUE的情况,这种情况下取绝对值的时候会overflow

BTW,Double.MIN_VALUE定义的是大于0的最小正数哦~

leetcode的testcase没有涉及到double overflow的情况,double overflow的时候直接返回MAX_VALUE了?

69. Sqrt(x)

Implement int sqrt(int x).

Compute and return the square root of x.

用二分法做,因为测试 mid * mid 时得到的结果可能超过int的范围,所以,这一步需要用long来做。

int相乘overflow的时候,得到的结果有可能是一个正整数,无法判别overflow(得到的结果甚至可能大于mid真的无法判别啊)。

62. Unique Paths

A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).

The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).

How many possible unique paths are there?

Above is a 3 x 7 grid. How many possible unique paths are there?

Note: m and n will be at most 100.

这道题其实是求组合数。Cm-1m+n-1

注意由于起点和终点一个在1一个在m,所以两者之间需要的步数是m - 1而不是m。

372. Super Pow

Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array.

Example1:

a = 2
b = [3] Result: 8

Example2:

a = 2
b = [1,0] Result: 1024

Credits:
Special thanks to @Stomach_ache for adding this problem and creating all test cases.

public class Solution {
private static final int N = 1337;
public int superPow(int a, int[] b) {
int size = b.length;
int result = 1;
for (int i = 0; i < size; i++) {
result = helper(result, 10) * helper (a, b[i]) % N;
}
return result;
}
private int helper(int a, int b) {
if (b == 0) {
return 1;
} else if (b == 1) {
return a % N;
}
return helper (a, b / 2) * helper(a, b - b / 2) % N;
}
}

[leetcode] 题型整理之数字加减乘除乘方开根号组合数计算取余的更多相关文章

  1. 用C语言将一个数开根号后再取倒数的方法

    在上学的时候,曾经看过有人写过这样的算法,就是将一个数开根号后再取倒数的算法,我本人也觉得十分巧妙,于是就将它积累了下来,让我们来看看是怎么回事: #include <stdio.h> # ...

  2. [leetcode] 题型整理之排列组合

    一般用dfs来做 最简单的一种: 17. Letter Combinations of a Phone Number Given a digit string, return all possible ...

  3. [leetcode] 题型整理之二叉树

    94. Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' va ...

  4. [leetcode] 题型整理之查找

    1. 普通的二分法查找查找等于target的数字 2. 还可以查找小于target的数字中最小的数字和大于target的数字中最大的数字 由于新的查找结果总是比旧的查找结果更接近于target,因此只 ...

  5. [leetcode] 题型整理之动态规划

    动态规划属于技巧性比较强的题目,如果看到过原题的话,对解题很有帮助 55. Jump Game Given an array of non-negative integers, you are ini ...

  6. [leetcode] 题型整理之cycle

    找到环的起点. 一快一慢相遇初,从头再走再相逢.

  7. [leetcode]题型整理之用bit统计个数

    137. Single Number II Given an array of integers, every element appears three times except for one. ...

  8. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  9. [leetcode] 题型整理之排序

    75. Sort Colors Given an array with n objects colored red, white or blue, sort them so that objects ...

随机推荐

  1. firefox屏蔽广告真是太好了

    在ubuntu上使用firefox有些页面的广告很多,很讨厌. 突然想到是否可以把这些广告屏蔽掉.在网上搜索了一下,发现有个 adblock plus插件,安装上发现广告没有了,很干净. 开源软件就是 ...

  2. Zabbix监控disk performance

    概述 zabbix获取/sys里面的磁盘信息并分析来监控disk performance sysfs是Linux内核中设计较新的一种虚拟的基于内存的文件系统,它的作用与 proc 有些类似(默认挂载在 ...

  3. cookie 和session 的区别详解

    这些都是基础知识,不过有必要做深入了解.先简单介绍一下. 二者的定义: 当你在浏览网站的时候,WEB 服务器会先送一小小资料放在你的计算机上,Cookie 会帮你在网站上所打的文字或是一些选择, 都纪 ...

  4. SQLAlchemy文档翻译

    想记录一下SQLAlchemy的入门学习,然后突发奇想觉得:为什么不直接翻译一下文档呢?于是顺手查了查怎么使用Gitbook,2333 于是就在Github开了项目,然后导入了Gitbook,开始写. ...

  5. <<< Java提取网页源码

    package com.sevennight; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io ...

  6. github提交代码时,报permission denied publickey

    在像github提交代码时,报permission denied publickey. 查找了一下,可能是因为github的key失效了. 按照以下步骤,重新生成key. ssh-keygen 一路默 ...

  7. tyvj1034 尼克的任务

    描述 尼克每天上班之前都连接上英特网,接收他的上司发来的邮件,这些邮件包含了尼克主管的部门当天要完成的全部任务,每个任务由一个开始时刻与一个持续时间构成.尼克的一个工作日为N分钟,从第一分钟开始到第N ...

  8. 开放数据库互联ODBC配置(odbcconf)

    开放数据库互连(ODBC)是微软引进的一种早期数据库接口技术,通过ODBC驱动程序可访问数据库数据:使用ODBC管理器可以完成对数据库的链接操作.笔者利用ODBC接口,将WINDOWS计数器信息写入到 ...

  9. Coursera系列-R Programming第三周-词法作用域

    完成R Programming第三周 这周作业有点绕,更多地是通过一个缓存逆矩阵的案例,向我们示范[词法作用域 Lexical Scopping]的功效.但是作业里给出的函数有点绕口,花费了我们蛮多心 ...

  10. Coursera系列-R Programming-Final Week-Assignment3 & 总结

    博客总目录,记录学习R与数据分析的一切:http://www.cnblogs.com/weibaar/p/4507801.html  ------- 经过周末一个半天的努力,终于把这次的Assignm ...