• 《剑指offer》算法题第十二天


    今天是《剑指offer》算法题系列的最后一天了,但是这个系列并没有包括书上的所有题目,因为正如第一天所说,这些代码是在牛客网上写并且测试的,但是牛客网上并没有涵盖书上所有的题目。

    今日题目:

    1. 正则表达式匹配
    2. 表示数值的字符
    3. 把字符串转换成整数
    4. 删除连表中重复的节点
    5. 按之字形顺序打印二叉树
    6. 将二叉树打印成多行

    其中第5,6题是比较典型的二叉树层次遍历的题目,比较简单,这边就不在阐述,但是大家对它们还是得非常熟悉。

    1. 正则表达式匹配

    题目描述:
    请实现一个函数用来匹配包括'.'和'*'的正则表达式。模式中的字符'.'表示任意一个字符,而'*'表示它前面的字符可以出现任意次(包含0次)。 在本题中,匹配是指字符串的所有字符匹配整个模式。例如,字符串"aaa"与模式"a.a"和"ab*ac*a"匹配,但是与"aa.a"和"ab*a"均不匹配

    思路:
    这道题目之前在leetcode上有遇到过,思路比较简单,但是对于新手来说是比较难想到的。
    它本质上是一个递归的问题,当前的匹配只关注当前的字符和一下个字符是‘*’的情况,代码比较直观。但在用java实现的时候要时刻注意数组是否越界。

    代码如下:

     1 public class Solution {
     2     public boolean match(char[] str, char[] pattern) {
     3     if (str == null || pattern == null) {
     4         return false;
     5     }
     6     int strIndex = 0;
     7     int patternIndex = 0;
     8     return matchCore(str, strIndex, pattern, patternIndex);
     9 }
    10   
    11 public boolean matchCore(char[] str, int strIndex, char[] pattern, int patternIndex) {
    12     //有效性检验:str到尾,pattern到尾,匹配成功
    13     if (strIndex == str.length && patternIndex == pattern.length) {
    14         return true;
    15     }
    16     //pattern先到尾,匹配失败
    17     if (strIndex != str.length && patternIndex == pattern.length) {
    18         return false;
    19     }
    20     //模式第2个是*,且字符串第1个跟模式第1个匹配,分3种匹配模式;如不匹配,模式后移2位
    21     if (patternIndex + 1 < pattern.length && pattern[patternIndex + 1] == '*') {
    22         if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
    23             return matchCore(str, strIndex, pattern, patternIndex + 2)//模式后移2,视为x*匹配0个字符
    24                     || matchCore(str, strIndex + 1, pattern, patternIndex + 2)//视为模式匹配1个字符
    25                     || matchCore(str, strIndex + 1, pattern, patternIndex);//*匹配1个,再匹配str中的下一个
    26         } else {
    27             return matchCore(str, strIndex, pattern, patternIndex + 2);
    28         }
    29     }
    30     //模式第2个不是*,且字符串第1个跟模式第1个匹配,则都后移1位,否则直接返回false
    31     if ((strIndex != str.length && pattern[patternIndex] == str[strIndex]) || (pattern[patternIndex] == '.' && strIndex != str.length)) {
    32         return matchCore(str, strIndex + 1, pattern, patternIndex + 1);
    33     }
    34     return false;
    35     }
    36 }

    2. 表示数值的字符串

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

    思路:
    参考《剑指》上的讲解,将字符串根据'.'和'E'分成若干段,每一段都有不同的约束条件,根据这个来判断是否符合数字的要求。同样地,要注意java中数组越界的问题。

    代码如下:

     1 public class Solution {
     2     int ind = 0;
     3     public boolean isNumeric(char[] str) {
     4         if(str.length < 1) return false;
     5         boolean num = scanInteger(str);
     6         if(ind < str.length && str[ind] == '.'){
     7             ind++;
     8             num = scanUnsignedInt(str) || num;
     9         }
    10         if(ind < str.length && (str[ind] == 'e' || str[ind] == 'E')){
    11             ind++;
    12             
    13             num = scanInteger(str) && num;
    14         }
    15         
    16         return num && (ind == str.length);
    17     }
    18     
    19     public boolean scanInteger(char[] str){
    20         if(ind < str.length && (str[ind] == '+' || str[ind] == '-'))
    21             ind ++;
    22         return scanUnsignedInt(str);
    23     }
    24     
    25     public boolean scanUnsignedInt(char[] str){
    26         int before = ind;
    27         while(ind < str.length && str[ind] >= '0'
    28               && str[ind] <= '9')
    29             ind ++;
    30         return ind > before;
    31     }
    32 }

    3. 把字符串转换成整数

    题目描述:
    将一个字符串转换成一个整数,要求不能使用字符串转换整数的库函数。 数值为0或者字符串不是一个合法的数值则返回0。

    思路:
    这个题目就是atoi(),是面试中经典得不能再经典的题目了,与之对应的是itoa(),请参考:http://www.cnblogs.com/cobbliu/archive/2012/08/25/2656176.html
    atoi()中,关键是要考虑到各种可能的输入,以及对字符串的操作。

    代码如下:

     1 public class Solution {
     2     public int StrToInt(String str) {
     3         if(str == null || str.length() == 0)
     4             return 0;
     5         int res = 0;
     6         boolean isNeg = false;
     7         if(str.charAt(0) == '-'){
     8             isNeg = true;
     9         }else if(str.charAt(0) != '+'){
    10             res = (str.charAt(0) - '0');
    11         }
    12         for(int i = 1; i < str.length(); i++){
    13             char ch = str.charAt(i);
    14             if(ch >= '0' && ch <= '9')
    15                 res = res*10 + (ch - '0');
    16             else
    17                 return 0;
    18         }
    19         
    20         return isNeg? -res:res;
    21     }
    22 }

    4. 删除链表中重复的节点

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

    思路:
    比较简单,熟悉链表操作的朋友能很快地写出来,关键是一定要熟悉!
    博主这边加入了一个头结点来处理连表中第一个节点是重复节点的情况。

    代码如下:

     1 public class Solution {
     2     public ListNode deleteDuplication(ListNode pHead){
     3         if(pHead == null || pHead.next == null)
     4             return pHead;
     5         ListNode dummy = new ListNode(0);
     6         dummy.next = pHead;
     7         ListNode prev = dummy,cur = pHead;
     8         while(cur != null){
     9             boolean duplicate = false;
    10             while(cur.next != null && cur.val == cur.next.val){
    11                 duplicate = true;
    12                 cur.next = cur.next.next;
    13             }
    14             if(duplicate){
    15                 prev.next = cur.next;
    16                 cur = cur.next;
    17             }else{
    18                 prev = prev.next;
    19                 cur = prev.next;
    20             }
    21         }
    22         return dummy.next;
    23     }
    24 }
  • 相关阅读:
    uva10285 Longest Run on a Snowboard(DP)
    typecho 0.8 营销引擎
    新浪博客营销插件
    忍者X3备份说明
    QQ空间、说说抓取引擎
    yiqicms发布插件的使用
    SHOPEX v4.85 发布插件
    ecshop2.73插件使用帮助
    Destoon V5 发布插件
    Wordpress3.52营销引擎
  • 原文地址:https://www.cnblogs.com/wezheng/p/8433441.html
Copyright © 2020-2023  润新知