算法学习之剑指offer(十)

题目描述

请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+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) {
        String string = String.valueOf(str);
        return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?");
    }
}

题目描述

请实现一个函数用来找出字符流中第一个只出现一次的字符。例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g"。当从该字符流中读出前六个字符“google"时,第一个只出现一次的字符是"l"。

输出描述:


import java.util.*;
public class Solution {
    
    private Map<Character,Integer> map = new LinkedHashMap<Character,Integer>();
    
    //Insert one char from stringstream
    public void Insert(char ch)
    {
        if(map.containsKey(ch))
            map.put(ch,2);
        else
            map.put(ch,1);
    }
  //return the first appearence once char in current stringstream
    public char FirstAppearingOnce()
    {
        for(Character cc:map.keySet()){
            if(map.get(cc).intValue()==1)
                return cc;
        }
        return '#';
    }
}

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

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {

    public ListNode EntryNodeOfLoop(ListNode pHead)
    {
        if(pHead==null)
            return null;
        ListNode meetNode = getMeetNode(pHead);
        if(meetNode==null)
            return null;
        int count=1;//计算节点
        ListNode node=meetNode.next;
        while(meetNode!=node){
            node=node.next;
            count++;
        }
        ListNode slowNode=pHead,fastNode=pHead;
        for(int i=0;i<count;i++)
            fastNode=fastNode.next;
         while(slowNode!=null&&fastNode!=null){
             if(slowNode==fastNode)
                return fastNode;
             slowNode = slowNode.next;
             fastNode = fastNode.next;
         }
        return null;
    }
    
    public ListNode getMeetNode(ListNode pHead){
        ListNode slowNode=pHead,fastNode=pHead;
        while(slowNode!=null&&fastNode!=null){
            slowNode = slowNode.next;
            fastNode = fastNode.next;
            if(fastNode!=null)
                fastNode = fastNode.next;
            if(slowNode==fastNode)
                return fastNode;
        }
        return null;
    }
}

题目描述

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

/*
 public class ListNode {
    int val;
    ListNode next = null;

    ListNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public ListNode deleteDuplication(ListNode pHead)
    {
        ListNode node = pHead;
        ListNode LastNode = null;
        while(node!=null){
            if(node.next==null)
                break;
            if(node.val==node.next.val){
                if(LastNode == null){
                    pHead=deleteSame(node);
                    node = pHead;
                }
                else{
                    node = deleteSame(node);
                    LastNode.next=node;
                }
            }else{
                LastNode=node;
                node=node.next;
            }
        }
        return pHead;
    }
     public ListNode deleteSame(ListNode node)
     {
         int sameValue = node.val;
         while(node!=null&&node.val==sameValue)
             node=node.next;
         return node;
     }
}

题目描述

给定一个二叉树和其中的一个结点,请找出中序遍历顺序的下一个结点并且返回。注意,树中的结点不仅包含左右子结点,同时包含指向父结点的指针。

/*
public class TreeLinkNode {
    int val;
    TreeLinkNode left = null;
    TreeLinkNode right = null;
    TreeLinkNode next = null;

    TreeLinkNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public TreeLinkNode GetNext(TreeLinkNode pNode)
    {
        if(pNode==null)
            return null;
        if(pNode.right!=null){
            pNode=pNode.right;
            while(pNode.left!=null)
                pNode=pNode.left;
            return pNode;
        }
        while(pNode.next!=null){
            if(pNode.next.left==pNode)
                return pNode.next;
            pNode=pNode.next;
        }
        return null;
    }
}

题目描述

请实现一个函数,用来判断一颗二叉树是不是对称的。注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的。

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    boolean isSymmetrical(TreeNode pRoot)
    {
        if(pRoot==null)
            return true;
        return is2Same(pRoot.left,pRoot.right);
    }
    boolean is2Same(TreeNode pLeft,TreeNode pRight)
    {
        if(pLeft==null&&pRight==null)
            return true;
        if(pLeft!=null&&pRight!=null){
            return pLeft.val==pRight.val&&is2Same(pLeft.left,pRight.right)&&is2Same(pLeft.right,pRight.left);
        }
        return false;
        
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值