查看“︁JavaScript/String/面试题”︁的源代码
来自康健生活
←
JavaScript/String/面试题
因为以下原因,您没有权限编辑该页面:
您请求的操作仅限属于该用户组的用户执行:
用户
您可以查看和复制此页面的源代码。
== 输出字符串 == <!-- 之前微软小冰创始团队的一个大佬面试笔试,题目如下: <syntaxhighlight lang="javascript"> /** Problem: Longest continous valid substring Your task is to find the longest continous substrings consists of only "valid" characters for each line and output the substring as a separate line to output. Here "valid" is defined as English letters (upper and lower cases) and space. Here is an example of two lines of input stream: ^%oA i923diuo (989872347897*&&:":}{?> And the ouput stream should also be two lines: oA i Please note that the output lines should be in the exact same order as their input lines and the number of input lines and output lines should be the same. InStream and OutStream are two already implemented interfaces for input and output respectively. You should use them for I/O and they have only the methods defined below. Don't read their implementations, which are not relevant to this interview. TWO TASKs: 1. add enough (according to you) test cases in line 52. 2. implement LongestValidSubstrings() of Processor class ADDITIONAL REQUIREMENTS: Do not use regular expression. Use as little memory as you can. You may search on the web freely when you code. If any questions please contact me. */ /** * The interface for input * * Only sequential access allowed. Cannot get length information. * * Note: * Ask question if anything is not clear. * The implementations are just mock. Your code should not depend on them. */ class InStream { constructor() { // TAKS 1: PLEASE ADD UNITTEST CASES HERE. this.UnittestCases = "^%oA i923diuo\n(989872347897*&oA it&:":}{?>"; this.ptr = 0; } readChar() { // 返回下标的单个字符串 if (!this.hasEnded()) { return this.UnittestCases[this.ptr++]; } else { return null; } } readLine() { if (!this.hasEnded()) { var start = this.ptr; var end = this.UnittestCases.indexOf('\n', this.ptr); // 检验 回车位置 if (end === -1) { end = this.UnittestCases.length; } this.ptr = end+1; return this.UnittestCases.substring(start, end); } else { return null; } } readBuffer(size) { if (!this.hasEnded()) { var start = this.ptr; var end = Math.min(this.ptr + size, this.UnittestCases.length) this.ptr = end; return this.UnittestCases.substring(start, end); } else { return null; } } // 计数器判断 hasEnded() { return this.UnittestCases.length <= this.ptr; } } /** * The interface for output * * Note: * Ask question if anything is not clear. * The implementations are just mock. Your code should not depend on them. */ class OutStream { writeLine(line) { console.log(line); } writeBuffer(buff) { process.stdout.write(buff); } text(tex){ console.log(tex) } } class Processor { constructor() { this.inStream = new InStream(); this.outStream = new OutStream(); } process() { // 断行 while (!this.inStream.hasEnded()) { var line = this.inStream.readLine(); this.longestValidSubstrings(line); } } isSpaceArray(arr){ if(arr.length<1){ return true } return false } isUpper(text){ if(text<'A' || text>'Z') return false; return true; } isLower(text){ if(text<'a' || text>'z') return false; return true; } isSpace(text){ if(text !== ' ') return false; return true; } longestValidSubstrings(line) { var str = '' let arr = [] var sign = { isSpace: false, isUpper: false, isLower: false } for(const index in line){ if(this.isSpace(line[index])){ str = str + line[index] sign.isSpace = true }else if(this.isUpper(line[index])){ str = str + line[index] sign.isUpper = true }else if(this.isLower(line[index])){ str = str + line[index] sign.isLower = true }else{ if(sign.isLower&&sign.isUpper&&sign.isSpace){ arr.push(str) } str = '' sign = { isSpace: false, isUpper: false, isLower: false } } } let resultArr = [...new Set(arr)].filter(function (s) { return s && s.trim(); }); if(!this.isSpaceArray(resultArr)){ for(let i=0;i<resultArr.length;i++){ this.outStream.writeBuffer(resultArr[i]) // or // this.outStream.text(resultArr[i]) } } } } let processor = new Processor(); processor.process();</syntaxhighlight> 但是不符合题目求,然后改正: {{leftrightflex|<syntaxhighlight lang="javascript"> class InStream { constructor() { // TAKS 1: PLEASE ADD UNITTEST CASES HERE. this.UnittestCases = "^%oA i923diuo\n(98987234789fh hhh(kk:}{?>\nhh\nhjk d**df\ndfg d&&dfvsdcs"; this.ptr = 0; } readChar() { // 返回下标的单个字符串 if (!this.hasEnded()) { return this.UnittestCases[this.ptr++]; } else { return null; } } readLine() { if (!this.hasEnded()) { var start = this.ptr; var end = this.UnittestCases.indexOf('\n', this.ptr); // 检验 回车位置 if (end === -1) { end = this.UnittestCases.length; } this.ptr = end+1; return this.UnittestCases.substring(start, end); } else { return null; } } readBuffer(size) { if (!this.hasEnded()) { var start = this.ptr; var end = Math.min(this.ptr + size, this.UnittestCases.length) this.ptr = end; return this.UnittestCases.substring(start, end); } else { return null; } } // 计数器判断 hasEnded() { return this.UnittestCases.length <= this.ptr; } } /** * The interface for output * * Note: * Ask question if anything is not clear. * The implementations are just mock. Your code should not depend on them. */ class OutStream { writeLine(line) { console.log(line); } writeBuffer(buff) { process.stdout.write(buff); } } class Processor { constructor() { this.inStream = new InStream(); this.outStream = new OutStream(); } process() { // 断行 while (!this.inStream.hasEnded()) { var line = this.inStream.readLine(); this.longestValidSubstrings(line); } } isOk(text){ if((text<'A' || text>'Z')&&(text<'a' || text>'z')&&(text !== ' ')) return false; return true; } longestValidSubstrings(line) { let len = 0 let start = 0 let count = 0 for(const index in line){ if(this.isOk(line[index])){ if(count===0&&start===0&&len===0){ start = index } if(Number(index)+1===line.length){ if(len<count){ start = index - count len = count + 1 } } count++ }else{ if(len<count){ start = index - count len = count } count = 0 } } this.outStream.writeBuffer(line.slice(start, start+len)+'\n') } } let processor = new Processor(); processor.process(); </syntaxhighlight>|{{consoleout|<pre> oA i fh hhh hh hjk d dfvsdcs </pre>}}}} 然后笔试通过了,心满意足~ -->
返回
JavaScript/String/面试题
。
导航菜单
康健生活
搜索
user-page
通知
user-interface-preferences
个人工具
登录
五一
多读书,多运动,多到远处走走,再看看不一样的世界。