• leetcode


    class Solution {
        public int strStr(String haystack, String needle) {
            int i = 0, j = 0;
            int a = haystack.length();
            int b = needle.length();
            if(a<b) return -1;
            else if(b==0 || haystack.equals(needle)) return 0;
            else {
                while(i < a && j < b) {
                    if(needle.charAt(j) != haystack.charAt(i)) {
                        i=i-j+1;
                        j=0;
                    }
                    else {
                        i++;
                        j++;
                    }
                }
                if(j==b) return i-j;
                else return -1;
            }
        }
    }
    双指针
    class Solution {
        public int strStr(String haystack, String needle) {
                    if(haystack.length() == 0&&needle.length() != 0) return -1;
                    int l1=haystack.length();
                    int l2=needle.length();
                    boolean flag;
                    for(int i = 0 ; i <= l1-l2; i++){
                        flag = true;
                        for(int j = 0 ; j < needle.length() ; j ++) {
                            if(haystack.charAt(i+j) != needle.charAt(j)) {
                                flag = false;
                                break;
                            }
                        }
                        if(flag) return i;
                    }
                    return -1;
                }
        }
    简洁
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    /*
    class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
                if(root == null) return res;
                //定义一个队列q
                Queue<TreeNode> q = new LinkedList<>();
                q.offer(root);//把根加入队列
                while(!q.isEmpty()) {
                    int sz = q.size();
                    ArrayList<Integer> list = new ArrayList<>();
                    for(int i=0; i<sz; i++) { //队列大小
                        TreeNode node = q.poll(); //pop
                        list.add(node.val); //push
                        if(node.left != null) q.offer(node.left);
                        if(node.right != null) q.offer(node.right);
                    }
                    res.add(list);
                }
                return res;
        }
    }*/
    class Solution {
         /*
          * 队列:Queue<TreeNode> queue=new LinkedList<>();
          * 入队:offer
          * 出队:poll
          * 得到队头:peek
          * 大小:size
          * 空:isEmpty
          */
          public List<List<Integer>> levelOrderBottom(TreeNode root) {
            // write your code here
            List<List<Integer>> res = new ArrayList<>();
            List<Integer> list = new ArrayList<>();
            if(root==null){
                return res;
            }
            Queue<TreeNode> queue = new LinkedList<>();
            TreeNode curLast = root;
            TreeNode nextLast = root;
            queue.offer(root);
            while (!queue.isEmpty()){
                TreeNode node = queue.poll();
                list.add(node.val);
                if(node.left!=null){
                    queue.offer(node.left);
                    nextLast = node.left;
                }
                if(node.right!=null){
                    queue.offer(node.right);
                    nextLast = node.right;
                }
                if(node==curLast){
                    curLast = nextLast;
                    res.add(new ArrayList<>(list));
                    list = new ArrayList<>();
                }
            }
            Collections.reverse(res);
            return res;
        }
    }
    107
  • 相关阅读:
    平衡二叉树
    二叉搜索树的最近公共祖先
    U-Boot> help, 命令集
    sprintf_s函数用法
    用keil编写的 C51错误 *** WARNING L1: UNRESOLVED EXTERNAL SYMBOL SYMBOL: ?C_START
    GPS时间系统概述和世界时系统
    浅析gcc、arm-linux-gcc和arm-elf-gcc关系
    如何删除电脑中使用过的COM端口
    飞鸽传书 绑定指定网卡
    UE 高亮 一个或多个关键字的方法
  • 原文地址:https://www.cnblogs.com/Roni-i/p/9746350.html
Copyright © 2020-2023  润新知