您的位置:首页 > 职场人生

剑指offer面试题 java解答11-15

2016-08-22 16:11 155 查看
面试题11:数值的整数次方

public class Test11 {
public double Power(double base,int exponent) throws Exception{
Double baseValue=new Double(base);
//利用java Double对象的equals方法判断两个double类型是否相等 ,注意+0.0与-0.0调用此方法返回false
if (baseValue.equals(new Double(+0.0))||baseValue.equals(new Double(-0.0)))
{
if (exponent<0)
{
throw new Exception("invalid input");
}
}
int absExponent=0;
if (exponent<0)
{
absExponent=-exponent;
}else if (exponent>0)
{
absExponent=exponent;
}else if (exponent==0)
{
return 1.0;
}
double result=PowerWithUnsignedExponent(base,absExponent);
if (exponent<0)
{
result=1.0/result;
}
return result;
}
private double PowerWithUnsignedExponent(double base,int absExponent) {
double result=1.0;
for (int i = 0; i <absExponent; i++) {
result*=base;
}
return result;
}
public static void main(String[] args) throws Exception {
Test11 t=new Test11();
System.out.println(t.Power(0.2,-2));
}
}


面试题12:打印1到最大的n位数

public class Test12 {
private void Print1ToMaxOfDigits_1(String s,int len){
if (len==0) {
System.out.println(s);
return;
}
for (int i = 0; i < 10; i++) {
Print1ToMaxOfDigits_1(s+i, len-1);
}
}
private void Print1ToMaxOfDigits(int n) {
Print1ToMaxOfDigits_1("", n);
}
public static void main(String[] args) {
Test12 t=new Test12();
t.Print1ToMaxOfDigits(4);
}
}


面试题13:在O(1)时间删除链表结点

public class Test13 {
private class Node<T>{
private T value;
private Node<T> next = null;

Node(T obj) {
this.value = obj;
}
}
public Node DeleteNode(Node head,Node nodeToBeDeleted) throws Exception {
if (head==null||nodeToBeDeleted==null)
{
throw new Exception("invalid");
}
//不是尾结点
if (nodeToBeDeleted.next!=null)
{
Node nextNode=nodeToBeDeleted.next;
nodeToBeDeleted.value=nextNode.value;
nodeToBeDeleted.next=nextNode.next;
nextNode=null;
}
//为头结点
else if (head==nodeToBeDeleted)
{
nodeToBeDeleted=null;
head=null;
}
//多个结点 删除尾结点
else
{
Node pNode=head;
while (pNode.next!=nodeToBeDeleted)
{
pNode=pNode.next;
}
pNode.next=null;
nodeToBeDeleted=null;
}
return head;
}
}


面试题14:调整数组顺序使奇位位于偶位前面

public class Test14 {
public void Reorder(int[] a) throws Exception{
if (a==null&&a.length==0)
{
throw new Exception("Invalid");
}
int left=0;
int right=a.length-1;
while (left<right) {
while (left<right&&(a[right]&1)==0) {
right--;
}
while (left<right&&(a[left]&1)==1) {
left++;
}
int tmp=a[left];
a[left]=a[right];
a[right]=tmp;
}
}
public static void main(String[] args) throws Exception {
int[] a={1,2,3,4,5,6,7};
Test14 t=new Test14();
t.Reorder(a);
for (int i = 0; i < a.length; i++) {
System.out.println(a[i]);
}
}
}


面试题15:链表中倒数第K个结点

public class Test15 {
private class Node<T>{
private T obj;
private Node<T> next = null;
Node(T obj) {
this.obj = obj;
}
}
private Node first = null;
public Node getHeadNode(){
return first;
}
public <T> void insertFirst(T obj){
Node node = new Node(obj);
node.next = first;
first = node;
}
public Node FindKthToTail(Node head,int k) throws Exception{
if (head==null||k<1) {
throw new Exception("invalid");
}
Node listHead=head;
Node node;
for (int i = 0; i < k-1; i++)
{
if (head.next!=null)
{
head=head.next;
}else {
throw new Exception("链表长度小于K");
}
}
node=listHead;
while (head.next!=null) {
head=head.next;
node=node.next;
}
return node;
}
public static void main(String[] args) throws Exception {
int[] a={6,5,4,3,2,1};
Test15 t=new Test15();
for (int i = 0; i < a.length; i++)
{
t.insertFirst(a[i]);//头插法
}
//链表为1->2->3->4->5->6
Node node=t.FindKthToTail(t.getHeadNode(), 3);
System.out.println(node.obj);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 面试题