国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Leetcode: Reverse Words in a String II

2019-11-14 23:06:30
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
Leetcode: Reverse Words in a String II
Given an input string, reverse the string word by word. A word is defined as a sequence of non-space characters.The input string does not contain leading or trailing spaces and the words are always separated by a single space.For example,Given s = "the sky is blue",return "blue is sky the".Could you do it in-place without allocating extra space?

這道題要求in-place做法,不能使用extra space, 那么,做法跟Rotate Array那道題非常相似

(1)reverse the whole array

(2)reverse each subarray seperated by ' '

 1 public class Solution { 2     public void reverseWords(char[] s) { 3         if (s.length == 0) return; 4         reverse(s, 0, s.length-1); 5         int last = 0; 6         for (int i=0; i<s.length; i++) { 7             if (s[i] == ' ') { 8                 reverse(s, last, i-1); 9                 last = i + 1;10             }11         }12     }13     14     public void reverse(char[] s, int l, int r) {15         while (l <= r) {16             int temp = s[l];17             s[l] = s[r];18             s[r] = temp;19             l++;20             r--;21         }22     }23 }


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 三门县| 光泽县| 县级市| 将乐县| 南川市| 望奎县| 历史| 临猗县| 双城市| 石棉县| 衡南县| 敦化市| 大名县| 普兰县| 安庆市| 通海县| 扎兰屯市| 富川| 万盛区| 津市市| 当雄县| 德江县| 阿克| 平塘县| 布拖县| 齐河县| 武威市| 星座| 满城县| 陈巴尔虎旗| 鹰潭市| 古田县| 二手房| 乐安县| 景宁| 松阳县| 旅游| 铜陵市| 睢宁县| 梨树县| 白沙|