[Algorithm] Reverse array of Chars by word
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, ); return data;
} console.log(main(data));
The reverseWholeString function would be:
function reverseWholeString(data) {
let start = ,
end = data.length - ;
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 - ; while (index !== -) {
reverseChars(data, start, end);
start = index + ;
index = findEmptyIndex(data, start);
end = index - ;
} reverseChars(data, start, data.length - );
} function findEmptyIndex(data, start) {
let index;
for (let i = start; i < data.length; i++) {
if (data[i] === " ") {
index = i;
break;
} else {
index = -;
}
} 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));
[Algorithm] Reverse array of Chars by word的更多相关文章
- [Algorithm] Reverse a linked list
It helps to understands how recursive calls works. function Node(val) { return { val, next: null }; ...
- [Algorithm] Chunk Array
// --- Directions// Given an array and chunk size, divide the array into many subarrays// where each ...
- Reverse array
数组颠倒算法 #include <iostream> #include <iterator> using namespace std; void reverse(int* A, ...
- reverse array java
/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import ...
- CUDA学习,使用shared memory实现Reverse Array
- java.lang.String
1.String 是一个类,广泛应用于 Java 程序中,相当于一系列的字符串.在 Java 语言中 strings are objects.创建一个 strings 最直接的方式是 String g ...
- Java基础教程(20)--数字和字符串
一.数字 在用到数字时,大多数情况下我们都会使用基本数据类型.例如: int i = 500; float gpa = 3.65f; byte mask = 0xff; 然而,有时候我们既需要 ...
- [Swift]LeetCode557. 反转字符串中的单词 III | Reverse Words in a String III
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- JavaScript Array 常用函数整理
按字母顺序整理 索引 Array.prototype.concat() Array.prototype.filter() Array.prototype.indexOf() Array.prototy ...
随机推荐
- 消息中间件ActiveMQ使用详解
消息中间件ActiveMQ使用详解 一.消息中间件的介绍 介绍 消息队列 是指利用 高效可靠 的 消息传递机制 进行与平台无关的 数据交流,并基于 数据通信 来进行分布式系统的集成. 特点(作用) ...
- hdu 1253
D - 胜利大逃亡 Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- Yii2 init 与 beforeAction 区别
1.执行顺序 init > beforeAction 2.调用子函数时,两个函数都不会再次执行 3.返回值 init返回false继续执行,beforeAction停止执行 4.执行EXIT,全 ...
- LOJ2758 年轮蛋糕
JOI 君马上要和妹妹 JOI 子和 JOI 美一起吃小吃.今天的小吃是他们三个人都很喜欢的年轮蛋糕. 年轮蛋糕是像下图一样呈圆筒形的蛋糕.为了把蛋糕分给三个人,JOI 君必须沿着半径方向切 3 刀, ...
- 使用Keras开发神经网络
一.使用pip安装好tensorflow 二.使用pip安装好Keras 三.构建过程: 1 导入数据 2 定义模型 3 编译模型 4 训练模型 5 测试模型 6 写出程序 1.导入数据 使用皮马人糖 ...
- mui 图片预览
1. 图片放大滑动预览需要 mui.zoom.js mui.previewimage.js 2. 调用 mui.previewImage(); 3. 滑动过程中禁止图片缩放 注释掉下面代 ...
- 引用计数的cocos2dx对象内存管理和直接new/delete box2d对象内存管理冲突的解决方法
转载请注明: http://blog.csdn.net/herm_lib/article/details/9316601 项目中用到了cocos2dx和box2d,cocos2dx的内存是基于引用计数 ...
- Source Insight 源代码查看工具
在开发的过程中,有时候我们需要研究源代码,查看源码是一个好的习惯,能帮我们学到很多的东西,比如JDK可以帮助我们理解很多设计模式在实际开发中的应用,又或者android开发者,源代码更是必不可少的,当 ...
- iOS-- UIimageView详解
原文地址: http://blog.csdn.net/djxiaoyu_haha/article/details/40348377 // (1)创建 UIImageView *imageView = ...
- ExtJs4.0日期控件只显示年月按年月格式会跳月的解决办法
如果是Ext.form.panel的话,只要设置一下属性就可以.如下代码: { text : '期间', width : 80, sortable : true, dataIndex : 'accou ...