每天进步一点!每天离梦想靠近一点!
to_zoe_yang · 共 109 篇文章
详解java中的抽象类和接口的区别
在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力。abstract class和interface之间在对于抽象类定义的支持方面具有很大的相似性,甚至可以相互替换,因此很多开发者在进 行抽象类定义时对于abstract class和interface的选择显得比较随意。其实,两者之间还是有很大的区别的,对于它们的选择甚至反映出对 于问题领域本质的理解、对于设计意图的理解是否正确、合理。本文将对它们之间的区别进行一番剖析,试图给开发者提供一个在二者之间进行选择的依据。 理解抽象类 …
2011-09-15 15:09 · Java相关 · 评论(0)
在二元树中找出和为某一值的所有路径
题目: 题目:输入一个整数和一棵二元树。 从树的根结点开始往下访问一直到叶结点所经过的所有结点形成一条路径。 打印出和与输入整数相等的所有路径。 例如 输入整数22和如下二元树 10 / \ 5 12 / \ 4 7 则打印出两条路径:10, 12和10, 5, 7。 思路:有两种方法,递归和非递归。其中非递归使用前序遍历,一条路走到底,并且每次访问一个节点,就判断当前访问的节点是否符合要求,符合就进栈保持,不然就退栈,对退出来的节点判断是否有右子树,有就继续往下,否则就继续退栈。 代码: 递归: public void findPath(BiSearchTree node,int sum,…
2011-09-14 20:25 · 面试题 · 评论(2)
判断整数序列是不是二元查找树的后序遍历结果
问题描述: 判断整数序列是不是二元查找树的后序遍历结果 题目:输入一个整数数组,判断该数组是不是某二元查找树的后序遍历的结果。 如果是返回true,否则返回false。 例如输入5、7、6、9、11、10、8,由于这一整数序列是如下树的后序遍历结果: 8 / \ 6 10 / \ / \ 5 7 9 11 因此返回true。 如果输入7、4、6、5,没有哪棵树的后序遍历的结果是这个序列,因此返回false。 import java.util.Arrays; public class Judge { public static boolean judge(int[] Data){ int le…
2011-09-13 18:47 · 面试题 · 评论(0)
把二元查找树转变成排序的双向链表
题目: 输入一棵二元查找树,将该二元查找树转换成一个排序的双向链表。 要求不能创建任何新的结点,只调整指针的指向。 10 / \ 6 14 / \ / \ 4 8 12 16 转换成双向链表 4=6=8=10=12=14=16。 中序遍历,遍历的过程生成双向链表。 先实现双线链表,然后实现二叉查找树。 class Node { private int value; private Node pre = null; private Node next = null; private Node prior = null; public Node(int value) { this.value =…
2011-09-11 20:08 · 面试题 · 评论(0)
寻找丢失的数字
据传说是MS/Google等等IT名企业的面试题: 有一组数字,从1到n,中减少了一个数,顺序也被打乱,放在一个n-1的数组里 请找出丢失的数字,最好能有程序,最好算法比较快 假如有1,2,3,···,10十个数字,并且少了5,那么遍历数组求和得到50,在遍历的过程中得知最大数字为10,那么如果不少5,全部数字和为55,则缺少的数字为55-50=5 如果少了10,那么遍历数组得到的和为45,最大数字为9,和依然为45,45-45=0,则丢失的数字为9+1=10 或者就是申请n-1个空间,遍历标记! import java.util.Arrays; import java.util.Rando…
2011-09-08 14:28 · 面试题 · 评论(0)
Problem 52
问题描述: It can be seen that the number, 125874, and its double, 251748, contain exactly the same digits, but in a different order. Find the smallest positive integer, x , such that 2 x , 3 x , 4 x , 5 x , and 6 x , contain the same digits. 解决问题: package projecteuler; import java.util.Arrays; public c…
2011-09-07 19:37 · 算法 · 评论(0)
Problem 51
问题描述: By replacing the 1st digit of *3, it turns out that six of the nine possible values: 13, 23, 43, 53, 73, and 83, are all prime. By replacing the 3rd and 4th digits of 56**3 with the same digit, this 5-digit number is the first example having seven primes among the ten generated numbers, yield…
2011-09-06 19:46 · 算法 · 评论(0)
【转载】VC++对象模型
前言 了解你所使用的编程语言究竟是如何实现的,对于C++程序员可能特别有意义。首先,它可以去除我们对于所使用语言的神秘感,使我们不至于对于编译器干的 活感到完全不可思议;尤其重要的是,它使我们在Debug和使用语言高级特性的时候,有更多的把握。当需要提高代码效率的时候,这些知识也能够很好地帮助 我们。 本文着重回答这样一些问题: * 类如何布局? * 成员变量如何访问? * 成员函数如何访问? * 所谓的“调整块”(adjuster thunk)是怎么回事? * 使用如下机制时,开销如何: * 单继承、多重继承、虚继承 * 虚函数调用 * 强制转换到基类,或者强制转换到虚基类 * 异常处理 …
2011-09-06 18:15 · 评论(0)
【转载】面向对象三要素
面向对象三要素是封装 继承 多态 封装 封装就是事物抽象为类,把对外接口暴露,将实现和内部数据隐藏。 继承 面向对象编程 (OOP) 语言的一个主要功能就是“继承”。继承是指这样一种能力:它可以使用现有类的所有功能,并在无需重新编写原来的类的情况下对这些功能进行扩展。 通过继承创建的新类称为“子类”或“派生类”。 被继承的类称为“基类”、“父类”或“超类”。 继承的过程,就是从一般到特殊的过程。 要实现继承,可以通过“继承”(Inheritance)和“组合”(Composition)来实现。 在某些 OOP 语言中,一个子类可以继承多个基类。但是一般情况下,一个子类只能有一个基类,要实现多…
2011-09-06 18:14 · Java综合 · 评论(0)
Problem 50
问题描述: The prime 41, can be written as the sum of six consecutive primes: 41 = 2 + 3 + 5 + 7 + 11 + 13 This is the longest sum of consecutive primes that adds to a prime below one-hundred. The longest sum of consecutive primes below one-thousand that adds to a prime, contains 21 terms, and is equal …
Problem 49
问题描述: The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another. There are no arithmetic sequences made up of three 1-, 2-, or 3-digit …
2011-08-28 15:34 · 算法 · 评论(0)
Problem 47
问题描述: The first two consecutive numbers to have two distinct prime factors are: 14 = 2 7 15 = 3 5 The first three consecutive numbers to have three distinct prime factors are: 644 = 2² 7 23 645 = 3 5 43 646 = 2 17 19. Find the first four consecutive integers to have four distinct primes factors. Wh…
2011-08-27 16:10 · 算法 · 评论(0)
Problem 46
问题描述: It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square. 9 = 7 + 2 12 15 = 7 + 2 22 21 = 3 + 2 32 25 = 7 + 2 32 27 = 19 + 2 22 33 = 31 + 2 12 It turns out that the conjecture was false. What is the smallest odd composite th…
2011-08-26 18:08 · 算法 · 评论(0)
<分享>JNDI全攻略
JNDI全攻略之(一) 关键字:JNDI,J2EE,Java,命名和目录接口,Java Naming and Directory Interface 摘要: 本文详细介绍了JNDI的架构与实现,JNDI的工作原理,并给出了具体代码,帮助读者更理解J2EE主要常用技术---JNDI.本文为系列文章的第一篇,其它相关文章会在近期推出。 名词解释 jndi是Java 命名和目录接口(Java Naming and Directory Interface,JNDI)的简称.从一开始就一直是 java 2 平台企业版(JEE)的核心技术之一。在JMS,JMail,JDBC,EJB等技术中,就大量应用的…
2011-08-25 18:25 · Java综合 · 评论(0)
Problem 45
问题描述: Triangle, pentagonal, and hexagonal numbers are generated by the following formulae: Triangle T n = n ( n +1)/2 1, 3, 6, 10, 15, ... Pentagonal P n = n (3 n 1)/2 1, 5, 12, 22, 35, ... Hexagonal H n = n (2 n 1) 1, 6, 15, 28, 45, ... It can be verified that T285 = P165 = H143 = 40755. Find the …
2011-08-25 18:24 · 算法 · 评论(0)