剑指编程(11)

一、
在一个长度为n的数组里的所有数字都在0到n-1的范围内。
数组中某些数字是重复的,但不知道有几个数字是重复的。
也不知道每个数字重复几次。请找出数组中任意一个重复的数字。
例如,如果输入长度为7的数组{2,3,1,0,2,5,3},那么对应的输出是第一个重复的数字2。

public class Solution {
    // Parameters:
    //    numbers:     an array of integers
    //    length:      the length of array numbers
    //    duplication: (Output) the duplicated number in the array number,length of duplication array is 1,so using duplication[0] = ? in implementation;
    //                  Here duplication like pointor in C/C++, duplication[0] equal *duplication in C/C++
    //    这里要特别注意~返回任意重复的一个,赋值duplication[0]
    // Return value:       true if the input is valid, and there are some duplications in the array number
    //                     otherwise false
    public boolean duplicate(int numbers[],int length,int [] duplication) {
        //桶法
        boolean[] bucket = new boolean[length];
        for(int i = 0; i < length; i++) {
            if(bucket[numbers[i]]) {
                duplication[0] = numbers[i];
                return true;
            }
            bucket[numbers[i]] = true;
        }
        return false;
    }
}

二、
给定一个数组A[0,1,…,n-1],
请构建一个数组B[0,1,…,n-1],
其中B中的元素B[i]=A[0]A[1]A[i-1]*A[i+1]…*A[n-1]。
不能使用除法。

public class Solution {
    //B[i]除了A[i],A数组所有其他元素相乘,即除了对角线以外
    public int[] multiply(int[] A) {
        int[] B = new int[A.length];
        if(A == null || A.length == 0) {
            return B;
        }
        //计算下三角
        B[0] = 1;
        for(int i = 1; i < A.length; i++) {
            B[i] = B[i - 1] * A[i - 1];
        }
        //计算上三角,因为需要用原来的B[i],需要借助临时变量
        int tmp = 1;
        for(int i = A.length - 2; i >= 0; i--) {
            tmp *= A[i + 1];
            B[i] *= tmp;
        }
        return B;
    }
}

三、
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。
例如,字符串”+100”,”5e2”,”-123”,”3.1416”和”-1E-16”都表示数值。
但是”12e”,”1a3.14”,”1.2.3”,”+-5”和”12e+4.3”都不是。

public class Solution {
    public boolean isNumeric(char[] str) {
        boolean flagE = false;
        boolean flagDot = false;
        for(int i = 0; i < str.length; i++) {
            //e或E只可能出现一次,e后必须有数字
            if(str[i] == 'e' || str[i] == 'E') {
                if(flagE || i == str.length - 1) {
                    return false;
                }
                flagE = true;
            }else if(str[i] == '+' || str[i] == '-') {
                //正负号只可能在开头或E后面,且各只有一个
                if(i != 0 && str[i - 1] != 'e' && str[i - 1] != 'E') {
                    return false;
                }
            }else if(str[i] == '.'){
                //.只能出现一次,且不能在E后
                if(flagE || flagDot) {
                    return false;
                }
                flagDot = true;
            }else if(str[i] < '0' || str[i] > '9'){
                return false;
            }
        }
        return true;
    }
}

四、
一个链表中包含环,请找出该链表的环的入口结点。

 public class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    //公式推导直接得结论:一个指针从头结点开始,一个从相遇点开始,再一次相遇在入口结点
    public ListNode EntryNodeOfLoop(ListNode pHead) {
        ListNode slow = pHead;
        ListNode fast = pHead;
        while(slow != null && fast.next != null && slow != fast) {
            slow = slow.next;
            fast = fast.next.next;
        }
        if(slow == null || fast.next == null) {
            return null;
        }
        slow = pHead;
        while(slow != fast) {
            slow = slow.next;
            fast = fast.next;
        }
        return slow;
    }
}

五、
在一个排序的链表中,存在重复的结点,
请删除该链表中重复的结点,重复的结点不保留,返回链表头指针。
例如,链表1->2->3->3->4->4->5 处理后为 1->2->5

class ListNode {
    int val;
    ListNode next = null;
    ListNode(int val) {
        this.val = val;
    }
}
public class Solution {
    //注意点是重复节点自身也要删除
    public ListNode deleteDuplication(ListNode pHead) {
        //因为头结点也有可能删除
        ListNode tmp = new ListNode(-1);
        tmp.next = pHead;
        ListNode pre = tmp;
        ListNode cur = pHead;
        while(cur != null && cur.next != null) {
            if(cur.val == cur.next.val) {
                int value = cur.val;
                while(cur != null && cur.val == value) {
                    cur = cur.next;
                }
                pre.next = cur;
            }else {
                pre = cur;
                cur = cur.next;
            }
        }
        return tmp.next;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值