博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Algorithm] Reverse array of Chars by word
阅读量:5052 次
发布时间:2019-06-12

本文共 2809 字,大约阅读时间需要 9 分钟。

For example we have:

["p", "r", "e", "f", "e", "t", " ", "m", "a", "k", "e", " ", "p", "r", "a", "t", "i", " c", "e"]

We want to get:

["p", "r", "a", "t", "i", " c", "e", " ", "m", "a", "k", "e", " ", "p", "r", "e", "f", "e", "t"]

 

Requirement:

  You can only do in array swap, you cannot create a new array.

 

The way to do it:

  1. Reverse whole string to get:

["e", " c", "i", "t", "a", "r", "p", " ", "e", "k", "a", "m", " ", "t", "e", "f", "e", "r", "p"]

  2. Then we reverse each word to get final result:

["p", "r", "a", "t", "i", " c", "e", " ", "m", "a", "k", "e", " ", "p", "r", "e", "f", "e", "t"]

 

So the main function should looks like this:

let data = [  "p","r","e","f","e","t"," ",  "m","a","k","e"," ",  "p","r","a","t","i","c","e"];function main(_data) {  let data = _data.slice();  reverseWholeString(data);  reverseWords(data, 0);  return data;}console.log(main(data));

 

The reverseWholeString function would be:

function reverseWholeString(data) {  let start = 0,    end = data.length - 1;  reverseChars(data, start, end);}function reverseChars(data, start, end) {  while (start < end) {    [data[start], data[end]] = [data[end], data[start]];    start++;    end--;  }}

 

reverseWords function would be:

function reverseWords(data, start) {  let index = findEmptyIndex(data, start);  let end = index - 1;  while (index !== -1) {    reverseChars(data, start, end);    start = index + 1;    index = findEmptyIndex(data, start);    end = index - 1;  }  reverseChars(data, start, data.length - 1);}function findEmptyIndex(data, start) {  let index;  for (let i = start; i < data.length; i++) {    if (data[i] === " ") {      index = i;      break;    } else {      index = -1;    }  }  return index;}

 

------------

Full code:

let data = [  "p",  "r",  "e",  "f",  "e",  "t",  " ",  "m",  "a",  "k",  "e",  " ",  "p",  "r",  "a",  "t",  "i",  " c",  "e"];function reverseWholeString(data) {  let start = 0,    end = data.length - 1;  reverseChars(data, start, end);}function reverseChars(data, start, end) {  while (start < end) {    [data[start], data[end]] = [data[end], data[start]];    start++;    end--;  }}function findEmptyIndex(data, start) {  let index;  for (let i = start; i < data.length; i++) {    if (data[i] === " ") {      index = i;      break;    } else {      index = -1;    }  }  return index;}function reverseWords(data, start) {  let index = findEmptyIndex(data, start);  let end = index - 1;  while (index !== -1) {    reverseChars(data, start, end);    start = index + 1;    index = findEmptyIndex(data, start);    end = index - 1;  }  reverseChars(data, start, data.length - 1);}function main(_data) {  let data = _data.slice();  reverseWholeString(data);  reverseWords(data, 0);  return data;}console.log(main(data));

  

转载于:https://www.cnblogs.com/Answer1215/p/10385636.html

你可能感兴趣的文章
MVC Razor
查看>>
软件目录结构规范
查看>>
Windbg调试Sql Server 进程
查看>>
linux调度器系列
查看>>
mysqladmin
查看>>
解决 No Entity Framework provider found for the ADO.NET provider
查看>>
SVN服务器搭建和使用(三)(转载)
查看>>
Android 自定义View (三) 圆环交替 等待效果
查看>>
设置虚拟机虚拟机中fedora上网配置-bridge连接方式(图解)
查看>>
HEVC播放器出炉,迅雷看看支持H.265
查看>>
[置顶] Android仿人人客户端(v5.7.1)——人人授权访问界面
查看>>
Eclipse 调试的时候Tomcat报错启动不了
查看>>
【安卓5】高级控件——拖动条SeekBar
查看>>
ES6内置方法find 和 filter的区别在哪
查看>>
Android入门之文件系统操作(二)文件操作相关指令
查看>>
Android实现 ScrollView + ListView无滚动条滚动
查看>>
Swift 中的指针使用
查看>>
Swift - 使用闭包筛选过滤数据元素
查看>>
alue of type java.lang.String cannot be converted to JSONObject
查看>>
搜索引擎选择: Elasticsearch与Solr
查看>>