LeetCode java day11

LeetCode java day11

LCR 181. 字符串中的单词反转

你在与一位习惯从右往左阅读的朋友发消息,他发出的文字顺序都与正常相反但单词内容正确,为了和他顺利交流你决定写一个转换程序,把他所发的消息 message 转换为正常语序。

注意:输入字符串 message 中可能会存在前导空格、尾随空格或者单词间的多个空格。返回的结果字符串中,单词间应当仅用单个空格分隔,且不包含任何额外的空格。

示例 1:

输入: message = "the sky is blue"
输出: "blue is sky the"

示例 2:

输入: message = "  hello world!  "
输出: "world! hello"
解释: 输入字符串可以在前面或者后面包含多余的空格,但是反转后的字符不能包括。

示例 3:

输入: message = "a good   example"
输出: "example good a"
解释: 如果两个单词间有多余的空格,将反转后单词间的空格减少到只含一个。

本人可谓是基础very poor

感觉被字符串随便拿捏。。。。

代码虽然对了,但是写得巨丑,而且复杂度极高。可谓是暴力都算不上,太流氓了。

class Solution {
    public String reverseMessage(String message) {
           message=message.trim();
           ArrayList<String>temp=new ArrayList<>();
           int count=0;
           for (int i = 0; i < message.length(); i++) {

            if(message.charAt(i)!=' '){
                  count++;
              }
              if(count!=0&&(message.charAt(i)==' ')){
                  temp.add(message.substring(i-count,i));
                  count=0;
              }
              if(count!=0&&i==message.length()-1){
                  temp.add(message.substring(i-count+1,i+1));
                  count=0;
              }

           }
            StringBuffer curr=new StringBuffer();
           for (int i = temp.size()-1; i >=0 ; i--) {
               if(i!= 0){
                   curr.append(temp.get(i)+" ");
               }
               else curr.append(temp.get(i));
           }
           return String.valueOf(curr);
        }
    }
  class Solution {
        public String reverseMessage(String message) {
           String[]temp=message.split(" ");
           ArrayList<String>list=new ArrayList<>();
           for (String s : temp) {
               if(s.length()!=0){
               list.add(s);}
           }
           String curr="";
           for (int i = list.size()-1; i>=0; i--) {
               if(i!=0){
                   curr =curr+ list.get(i)+" ";
               }else
                   curr =curr+ list.get(i);
           }
           return curr;
        }
    }

代码下来了,但是。。。喵的时间上去了

 class Solution {
        public String reverseMessage(String message) {
           String[]temp=message.split(" ");

           String curr="";
           for (int i = temp.length-1; i >=0; i--) {
               if(temp[i].length()!=0){
                   if(i!=0){
                       curr+=temp[i]+" ";
                   }
                   else curr+=temp[i];
               }
           }
           return curr.trim();
        }
    }

额,泪目了。

 class Solution {
  public String reverseWords(String s) {
            String[]temp=s.trim().split("\\s+");

            String curr="";
            for (int i = temp.length-1; i >=0; i--) {
                curr+=temp[i];
                if(i>0){
                    curr+=" ";
                }
            }
            return curr;
        }
    }

先到这里。。。

嘛,对字符串一些常用函数确实不熟悉,然后就是之前学Java8特性的时候也没怎么想过还能这样用来刷算法。

嘛,最近多线程学习感觉有点累,主要是有点迷茫,也感觉容易捡芝麻丢西瓜。

┭┮﹏┭┮

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值