本文涉及到一些JVM原理和Java的字节码指令,推荐感兴趣的读者阅读一本有关JVM的经典书籍<深入Java虚拟机(第2版)>,将它与我在<.NET 4.0面向对象编程漫谈>中介绍的CLR原理与IL汇编指令作个对比,相信读者会有一定的启发.而仔细对比两个类似事物的异同,是很有效的学习方法之一. 1 奇特的程序输出 前段时间,一个学生给我看了一段“非常诡异”的Java代码: public class testInteger { public static void main(…
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought through this! If the integer'…
Given a positive integer n and you can do operations as follow: If n is even, replace n with n/2. If n is odd, you can replace n with either n + 1 or n - 1. What is the minimum number of replacements needed for n to become 1? Example 1: Input: 8 Outp…
Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get. For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 +…
Convert a non-negative integer to its english words representation. Given input is guaranteed to be less than 231 - 1. For example, 123 -> "One Hundred Twenty Three" 12345 -> "Twelve Thousand Three Hundred Forty Five" 1234567 -&g…
Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 罗马数转化成数字问题,我们需要对于罗马数字很熟悉才能完成转换.以下截自百度百科: 罗马数字是最早的数字表示方式,比阿拉伯数字早2000多年,起源于罗马. 如今我们最常见的罗马数字就是钟表的表盘符号:Ⅰ,Ⅱ,Ⅲ,Ⅳ(IIII),Ⅴ,Ⅵ,Ⅶ,Ⅷ,Ⅸ,Ⅹ,Ⅺ,Ⅻ…… 对应阿拉伯数字(就是现…
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999. 之前那篇文章写的是罗马数字转化成整数(http://www.cnblogs.com/grandyang/p/4120857.html), 这次变成了整数转化成罗马数字,基本算法还是一样.由于题目中限定了输入数字的范围(1 - 3999), 使得题目变得简单了不少. 基本字符 I V…
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases. Notes: It is intended for this problem to be spe…
Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 click to show spoilers. Have you thought about this? Here are some good questions to ask before coding. Bonus points for you if you have already thought throu…
Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 public class Solution { public int reverse(int x) { if(x==Integer.MIN_VALUE) return 0; long result = 0; int i = 0; int temp = (x>0)?1:0; x = Math.abs(x); while…
简述:int与Integer的区别: 对于它们,我们可能只是知道简单的区别.Integer是int的一个封装类,int的初始值为0,而Integer的初始值为null.但是他们之间真的仅仅只有这些区别吗?我觉得答案是否定的,于是我决定深入到jdk源码中一探究竟.看看Integer与int到底有什么区别. 执行代码: public class IntegerTest { public static void main(String[] args) { // TODO Auto-generated…
题目描述: Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999. 解题思路: 首先我们要了解罗马数字怎么写的 个位数举例 I, 1 ]II, 2] III, 3] IV, 4 ]V, 5 ]VI, 6] VII, 7] VIII,8 ]IX, 9 ·十位数举例 X, 10] XI, 11 ]XII, 12] XIII, 13] XIV,…