美文网首页
2022-03-08 2055. 蜡烛之间的盘子+ II 063

2022-03-08 2055. 蜡烛之间的盘子+ II 063

作者: 16孙一凡通工 | 来源:发表于2022-03-08 10:39 被阅读0次

双指针第一遍超时,优化了一下勉强通过,没看到题目下面的数据量大小。
应该还能再优化一波,不过感觉会很麻烦,懒。。。
java版本:

class Solution {
    public int[] platesBetweenCandles(String s, int[][] queries) {

        // 找最左和最右边的两个蜡烛 ||
        // 然后找里面有几个盘子  *
        // left right
        // 双指针
        int left,right,count;
        int n=queries.length;
        int[] res=new int[n];
        int arr_count=0;
           
            for(int i=0;i<s.length();i++){
                if(s.charAt(i)=='|'){
                       arr_count++;
                }  
            }
            if(arr_count==0){
                return res;
            }
            
             int[] arr=new int[arr_count];
             int index=0;
           for(int i=0;i<s.length();i++){
                if(s.charAt(i)=='|'){
                arr[index]=i;
                index++;
                }  
            }
            int left_index,right_index;


        for(int i=0;i<n;i++){
            count=0;
           left_index=0;
           right_index=arr_count-1;
           left=queries[i][0];
           right=queries[i][1];
         
                while( left_index<right_index && left>arr[left_index]  ){
                    left_index++;
               }
                while(left_index<right_index && right<arr[right_index]){
                 right_index--;
               }
            //    System.out.println(arr[left_index]);
            //        System.out.println(arr[right_index]);
           
          
           count=arr[right_index]-arr[left_index]-(right_index-left_index-1)-1;
           
           res[i]=count;
        }
        return res;

    }
}

前缀树中规中矩,记不清leetcode咋导入需要的包了,import导入Stringutils报错。。。
java版本:

class Solution {

 class Trbie{
    private Trbie[] children;
        private boolean isEnd;
    public Trbie() {
        children=new Trbie[26];
        isEnd=false;
     
    }
 }

    /** Inserts a word into the trie. */
    public  void insert(Trbie root,String word) {
        Trbie node=root;
       
        for(int i=0;i<word.length();i++){
            int index=word.charAt(i)-'a';
            if(node.children[index]==null){
            node.children[index]=new Trbie();
            }
            node=node.children[index];
        }
        node.isEnd=true;
    return ;

    }
         public  int search(Trbie root,String s){
             Trbie node=root;
             int count=0;
              for(int i=0;i<s.length();i++){
             int index=s.charAt(i)-'a';
           if(node.children[index]==null || node.isEnd){
               break;
           }else{
              count++;
           }
           node=node.children[index];
        
         
        }
        // System.out.println(count);
        if(node.isEnd && count!=0){
         return count;
        }else{
            return 0;
        }
       


         }
    
    public String replaceWords(List<String> dictionary, String sentence) {

        // 确定每一个单词是否有其前缀
        Trbie node=new Trbie();
          for(int i=0;i<dictionary.size();i++){
            insert(node,dictionary.get(i));
          }

      String[] str=sentence.split(" ");
      int n=str.length;
      int[] arr=new int[n];
      for(int i=0;i<n;i++){
          int temp=search(node,str[i]);
          if(temp!=0){

              
            str[i]=str[i].substring(0,temp);
          }
      }
      StringBuffer str_buffer = new StringBuffer();
for (int i=0;i<n;i++) {
    str_buffer.append(str[i]);
   if(i!=n-1){
  str_buffer.append(" ");
   }
   
}
    String res=str_buffer.toString();
    return res;
    }
}

相关文章

  • 2022-03-08 2055. 蜡烛之间的盘子+ II 063

    双指针第一遍超时,优化了一下勉强通过,没看到题目下面的数据量大小。应该还能再优化一波,不过感觉会很麻烦,懒。。。j...

  • 科学小实验——会吸水的杯子

    在实验中培养孩子对科学的兴趣,引导孩子去观察,去探索。 准备材料:蜡烛,杯子,盘子,水。 步骤一:将蜡烛放在盘子里...

  • stem科学小实验-气压

    点燃蜡烛放在盘子里,盘子里倒水,用玻璃杯把蜡烛罩住。玻璃杯内的空气燃烧完,玻璃杯中的水面就会上升,玻璃杯中的水面高...

  • JavaScript:获取数组前N位元素

    2022-03-08 周二

  • 科学小实验:力大无穷的杯子

    一个杯子居然能把整个盘子给提起来,这到底是什么情况?我们一起看看吧! 准备材料:水、杯子、蜡烛、盘子(或托盘)一片...

  • 让水上涨

    实验材料: 清水,酱油,一根蜡烛,一个盘子,一个玻璃杯,一个打火机。 实验步骤: 1:先用打火机点好蜡烛(注意用打...

  • 简书周刊063发布—还没谈恋爱就老了

    《简书周刊063》epub下载地址《简书周刊063》豆瓣阅读地址《简书周刊063》多看阅读地址 点击链接即可下载,...

  • 2020-08-04

    【063号】入驻者:11山山 01. 【063号】00087-我听02. 【063号】01078-许愿03. 【0...

  • 橙子的ScalersTalk第六轮新概念朗读持续力训练Day 1

    练习材料: [Day 2721 2022-03-08] Lesson 25-1 The Cutty Sark On...

  • 分盘子

    小子过生日,吹完蜡烛准备吃蛋糕前,先发盘子;向前递“爸爸,给你”,向左递“妈妈,给你”,向右转“哥哥,给你”,伸长...

网友评论

      本文标题:2022-03-08 2055. 蜡烛之间的盘子+ II 063

      本文链接:https://www.haomeiwen.com/subject/hjcarrtx.html