【LeetCode在线编程记录-1】字符串按单词反转

来源:互联网 发布:触摸屏编程软件下载 编辑:程序博客网 时间:2024/05/17 02:13

写在前面


   LeetCode(地址:https://oj.leetcode.com/)是一个在线编程网站,题目经典,测试用例完备,共计157道算法类的题目。之后我会记录我的一些练习题目,有些答案是我自己原创的(说是原创,也很可能是之前在别的地方看到的而已),有些是从讨论区看到的,我都会明确标注出处。


Reverse Words in a String

Given an input string, reverse the string word by word.

For example,
Given s = "
the sky is blue",
return "
blue is sky the".

click to show clarification.

Clarification:

  • What constitutes a word?
    A sequence of non-space characters constitutes a word.
  • Could the input string contain leading or trailing spaces?
    Yes. However, your reversed string should not contain leading or trailing spaces.
  • How about multiple spaces between two words?
    Reduce them to a single space in the reversed string.

【我的分析】原来希望通过字符串全部反转然后按单词进行反转即可,但是遇到的困难是多个空格如何处理的问题。

【分析1-非原创】需要考虑的情况是单词与单词之间可能是多个空格。从前往后扫描得到字符temp,最后返回的是字符串result

1)如果不是空格,word+=temp

2)如果是空格,将word附加到result前面。

参考:https://oj.leetcode.com/discuss/10888/my-java-solution-few-lines

   

 /* 方法1:二级缓存的思想,word来存储字符,遇到空格,如果word不是空格,将word添加到字符串的newStr前面,同时置为"" */    public static String reverseWords4(String s) {        String temp = "";        String result = "";        for (int i = 0; i < s.length(); i++) {            char c = s.charAt(i);            if (c == ' ') {                if (temp != "" && result != "") {                    result = temp + " " + result;                }                if (temp != "" && result == "") {                    result = temp;                }                temp = "";             } else {                temp += c;            }        }        /* 最后一次添加 */        if (temp != "" && result != "") {            result = temp + " " + result;        }        if (temp != "" && result == "") {            result = temp;        }        return result;    }

【分析2-非原创】利用正则表达式将字符串分隔成字符串数组。这要用到系统函数。

参考:https://oj.leetcode.com/discuss/9142/my-accepted-java-solution

方案一:系统函数trim()split()

/* 正则表达\s+表示任意多个空白字符 */    public static String reverseWords3(String s) {        String[] parts = s.trim().split("\\s+");        String out = "";        if (parts.length > 0) {            for (int i = parts.length - 1; i > 0; i--) {                out += parts[i] + " ";            }            out += parts[0];        }        return out;    }


方案二:系统类Scanner默认构造函数会将字符串按空白字符分隔

   

 public String reverseWords(String s) {        Scanner parts = new Scanner(s);         String result = "";         while(parts.hasNext()){            result = parts.next() + " " + result;        }         return result.trim();    }

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 电动车被交警拖走了怎么办 电动车车被城管拖走了怎么办 12123地理反编码失败怎么办 苹果手机地理反编码失败怎么办 城管执法过程被打怎么办 老婆看不起老公不让碰怎么办 老婆总不让碰该怎么办 机动车扣满12分怎么办 吊车吊运货物失控应该怎么办 车辆违章扣6分怎么办 最新交通法扣满12分怎么办 违章停车单丢了怎么办 违停告知单掉了怎么办 违章停车扣3分怎么办 驾驶证被扣12分怎么办 被贴条了条丢了怎么办 车停路边连续几天被贴条怎么办 车停在路边限号怎么办 违停的罚单丢了怎么办 借道左转红灯了 怎么办 道路上有锯齿线标志怎么办 被领导臭骂了一顿怎么办 酒驾撞了人逃跑怎么办处理 荣耀9home键掉了怎么办 今天开车把老太婆撞了怎么办 交通责任认定书不合理怎么办 交通事故救济金用了 没钱还怎么办 车脏了洗不干净怎么办 衣服上贴花掉了怎么办 衣服上的画掉了怎么办 衣服上沾了胶怎么办 补鞋胶弄衣服上怎么办 摩托车大灯里面掉漆怎么办 行驶中轮胎爆胎怎么办 左拐车道直行了怎么办 踏板摩托车淋雨打不着火怎么办 购房小蓝本丢了怎么办 3d下载模型没颜色怎么办 车辆被扣30分怎么办 驾驶证被扣15分怎么办 驾驶本被扣分了怎么办