算法与数据结构 03 匹配问题

  1. 括号匹配问题
    假设表达式中允许包含多种括号 {}、<>、() 和 [] ,以任意方式排列,默认() [] <> {} 全部出现并两两匹配未正确格式,出现一种确实一半为不正确格式。
    例如: [ ( { } ) < > ]
    12 3 4 5 6 7 8
    当计算机接受第一个括号 “[”时,它便期待与之匹配的第八个括号"]"出现,然而计算机是按顺序遍历的,下一个接收到的是第二个括号“(”,此时第一个括号“[” 只能暂时放到一边,第二个括号接收后期待与之匹配的第五个括号“)”出现,同样等来的是第三个括号“{”;以此类推,直到第四个括号“}”被接收后,满足了第三个括号“{” 的期待并与之匹配;继续往后接收,与上面一样,遇到相同的就匹配,不相同的就放到一边接着往下,直到最后一个括号被接收。
    这样,上面的例子当最后一个括号被接受完成后,每一个括号都找到了与之相匹配的另一半,这是我们引入栈,遍历过程中将接收到的暂时不匹配的存入栈中,往下遍历遇到与之匹配的就弹栈,最后遍历结束时,看栈是否为空。若不为空,就说明有括号未被匹配;若空,这说明所有括号均能匹配。
    代码实现:

public static void main(String[] args) {
    solution01();
    solution02();
  }

//利用HashMap算法
  private static void solution02() {
    String str = "{()<>[]}";
    HashMap<Character,Character> map = new HashMap<>();
    map.put('[',']');
    map.put('<','>');
    map.put('(',')');
    map.put('{','}');
    ArrayStack<Character> stack = new ArrayStack<>();
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if (stack.isEmpty()) {
        stack.push(c);
      } else {
        char top = stack.peek();
        if (map.containsKey(top) && c == map.get(top)) {
          stack.pop();
        } else {
          stack.push(c);
        }
      }
    }
    System. out.println(stack.isEmpty());
  }
 
 // 利用ACSII编码判断
  private static void solution01() {
    String str = "{()[[()]]<>{}()<>}()";
    ArrayStack<Character> stack = new ArrayStack<>();
    for (int i = 0; i < str.length(); i++) {
      char c = str.charAt(i);
      if (stack.isEmpty()) {
        stack.push(c);
      } else {
        char top = stack.peek();
        if (top - c == -1 || top - c == -2) {
          stack.pop();
        } else {
          stack.push(c);
        }
      }
    }
    System. out.println(stack.isEmpty());
  }
}
//输出结果
true
true
  1. 判断回文
    方法一:与括号问题相同的原理
public class JudgingPalindrome01 {

  public static void main(String[] args) {
    //引入输入的字符串
    Scanner scanner = new Scanner(System.in);
    System. out.println("请输入文本:");
    String text = scanner.nextLine();

    //创建一个栈 存储字符数据
    ArrayStack<Character> stack = new ArrayStack<>();
     //遍历字符串 做出栈和进栈操作
    char c ;
    for (int i = 0; i < text.length(); i++) {
      //判断长度是否为奇数并做处理
      if (text.length() % 2 == 1 && text.length() / 2 == i) {
        continue;
      }
      c = text.charAt(i);
      if (stack.isEmpty()) {
        stack.push(c);
      } else {
        if (c == stack.peek()) {
          stack.pop();
        } else {
          stack.push(c);
        }
      }
    }
    if (stack.isEmpty()) {
      System. out.println(text + " is a palindrome");
    } else {
      System. out.println(text + " is not a palindrome");
    }
  }
}
// 输出结果
请输入文本:
上海自来水来自海上
上海自来水来自海上 is a palindrome

请输入文本:
123321
123321 is a palindrome

请输入文本:
123456
123456 is not a palindrome

方法二:
输入字符串格式,从字符串两端开始扫描对比

public static void main(String[] args) {
    System. out.println(solution02());
  }

  private static boolean solution02() {
    String text = "上海自来水来自海上";
    int i = 0;
    int j = text.length() - 1;
    while (true) {
      if (text.charAt(i) == text.charAt(j)) {
        i++;
        j--;
      } else {
        return  false;
      }
      if (i >= j) {
        return true;
      }
    }
  }
// 输出结果
true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值