{"doc_id": "00821847ab9598500cf0ae5398bbfe91", "text": "// WARMUP FROM https://codefellows.github.io/code-401-javascript-guide/curriculum/warm-ups/daily/\nconst people = ['Kookla','Fran','Ollie'];\n\nconst stuff = {\n tv: 'huge',\n radio: 'old',\n toothbrush: 'frayed',\n cars: ['Toyota','Mazda']\n}\n\n// let state = {};\n// let newPeople = [];\n// const newStuff = {};\nlet newState = {};\n\n// SPREAD DEMOS\n// [Oddie, Kookla, Fran, Ollie, Garfield]\nlet newPeople = ['Oddie', ...people, 'Garfield'];\nconsole.log(newPeople);\n\nlet newStuff = { ...stuff, cars:[...stuff.cars, 'GMC'] };\nconsole.log(newStuff);\n\n/*\nCreat a state object with keys of people and stuff that contain the people and stuff data.\nDo this using object destructuring assignment\n*/\nlet state = { stuff, people }\nconsole.log(state)\n\nconsole.log('============')\n\nlet bigState = {\n ...state,\n people: ['Oddie', ...people, 'Garfield'],\n stuff: \n { cars: [...stuff.cars, 'GMC'] }\n}\n\nconsole.log(bigState);\n\n\nfunction forLoop(arr) {\n for(let i = 0; i < arr.length; i++) {\n console.log(arr[i]);\n }\n}\n\nfunction whileLoop(arr) {\n // OG WAY\n // let i = 0;\n // // while( arr[i]) {\n // while (i < arr.length) {\n // console.log(arr[i]);\n // i++;\n\n // PASS BY REF / CLONE WAY\n let internalArray = [...arr];\n while (internalArray.length) {\n console.log(internalArray.shift());\n }\n}\n\nfunction mapLoop(arr) {\n let newArray = arr.map((val, idx) => {\n console.log(val);\n return { value: val };\n });\n\n console.log('NEWARRAY', newArray);\n}\n\nfunction map(arr, callbackFunction) {\n\n}\n\nfunction filter(arr, callbackFunction) {\n\n}\n \n\nconsole.log('forLoop')\nforLoop(people);\nconsole.log('=========')\n\nconsole.log('whileLoop')\nwhileLoop(people);\nconsole.log('=========')\n\nconsole.log('mapLoop')\nmapLoop(people);\nconsole.log('=========')\n\nconsole.log('map')\nlet mapped = map(people, (val, idx) => 'Rosie');\nconsole.log('map', mapped);\nconsole.log('=========')\n\nlet filtered = filter(people, (val, idx) => val === 'Kookla')\nconsole.log('filter');\nconsole.log(filtered);\nconsole.log('=========')\n\nconsole.log('PEOPLE', people);"} {"doc_id": "00a2b9d16ed3b9f8426723b494b337d8", "text": "// I paired [by myself] on this challenge.\n\n\n\n\n// Pseudocode\n// secretNumber equals 7\n// password equals a string called \"just open the door\"\n// allowedIn should be boolean value false\n// members should be an array that contains John as first and Mary as 4th element\n\n\n\n// __________________________________________\n// Write your code below.\n\nvar secretNumber = 7\nvar password = \"just open the door\"\nvar allowedIn = false\nvar members = [\"John\", \"Tom\", \"Aki\", \"Mary\"]\n\n\n\n\n// __________________________________________\n// Driver Code: Do not alter code below this line.\n\nfunction assert(test, message, test_number) {\n if (!test) {\n console.log(test_number + \"false\");\n throw \"ERROR: \" + message;\n }\n console.log(test_number + \"true\");\n return true;\n}\n\nassert(\n (typeof secretNumber === 'number'),\n \"The value of secretNumber should be a number.\",\n \"1. \"\n)\n\nassert(\n secretNumber === 7,\n \"The value of secretNumber should be 7.\",\n \"2. \"\n)\n\nassert(\n typeof password === 'string',\n \"The value of password should be a string.\",\n \"3. \"\n)\n\nassert(\n password === \"just open the door\",\n \"The value of password should be 'just open the door'.\",\n \"4. \"\n)\n\nassert(\n typeof allowedIn === 'boolean',\n \"The value of allowedIn should be a boolean.\",\n \"5. \"\n)\n\nassert(\n allowedIn === false,\n \"The value of allowedIn should be false.\",\n \"6. \"\n)\n\nassert(\n members instanceof Array,\n \"The value of members should be an array\",\n \"7. \"\n)\n\nassert(\n members[0] === \"John\",\n \"The first element in the value of members should be 'John'.\",\n \"8. \"\n)\n\nassert(\n members[3] === \"Mary\",\n \"The fourth element in the value of members should be 'Mary'.\",\n \"9. \"\n)\n\n\n\n\n\n// __________________________________________\n// Refactored Code: Include a refactored version (or justification of your original code) here. \n\n// I thought this generally looked good. Since it was obvious that it was a number, I shortened the variable name\nvar secretNum = 7\n// Password as a var name was too generic so i made it applicable to the member scenario\nvar memberPW = \"just open the door\"\n// This one felt weird as a boolean question so i framed the var as a validation question instead\nvar isAllowed = false\n// members felt too generic so I added some context at the end while keeping it plural to indicate an array\nvar membersList = [\"John\", \"Tom\", \"Aki\", \"Mary\"]\n\n// with the new variables, I could also choose to flatten them into one expression\n{var secretNum = 7; var memberPW = \"just open the door\"; var isAllowed = false; var membersList = [\"John\", \"Tom\", \"Aki\", \"Mary\"]}\n\n\n\n// __________________________________________\n// Reflection: Use the reflection guidelines to write a reflection here. \n\n// Framing the pseudocode was a bit confusing to me at first as they can look so similar to user stories. \n// I've internalized it in my head that user stories most importantly provide context and don't have to be mapped to code expressions line-by-line, but pseudocode can be looked at as tiny nibble size user stories that where best practice is to have each line of pseudo code translatable to an equivalent code experssion. \n\n// Refactoring was another point of interest for me. In the DRY concept, minimal duplication and avoiding waste were big concepts, and the 7+1 tips for naming variables was also possible. \n// I can see the beauty in providing context to people unfamiliar with your code in the most efficient way. \n// \n\n"} {"doc_id": "012cf0428e425070b53bc43f956d779e", "text": "// Assignment Code\nconst generateBtn = document.querySelector(\"#generate\");\n\n// Write password to the #password input\nfunction writePassword() {\n const password = generatePassword();\n const passwordText = document.querySelector(\"#password\");\n\n // Stops last generated password from clearing if a follow-up generator was cancelled\n if (password != null) { // Falsy check for null or undefined\n passwordText.value = password;\n console.log(\"\\nGenerated Password: \" + passwordText.value);\n }\n}\n\n// Generate random password function\nfunction generatePassword() {\n let isGenerated = false;\n let passwordLength = 0;\n let passwd = \"\";\n let charStr = \"\"; // Character string to be used for password generation\n const lower = \"abcdefghijklmnopqrstuvwxyz\";\n const upper = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n const nums = \"0123456789\";\n const specialChars = \" !\\\"#$%&'()*+,-./:;<=>?@[\\\\]^_`{|}~\";\n let charTypeLog = \"Input validated!\\nPassword will be generated with these character types:\\n\\n\";\n\n // Loop until password is cancelled or generated \n while (!isGenerated) {\n // Prompt user for password length and display boundaries for user.\n passwordLength = prompt(\n \"How many characters would you like your password to be?\\nMinimum: 8\\nMaximum: 128\"\n );\n // Check for valid input\n if (passwordLength == null) { // If generator is cancelled\n return;\n }\n else if (!Number.isInteger(Number(passwordLength)) || passwordLength.trim() === \"\") { // If NaN/floating points/empty string inputs are found.\n alert(\"Invalid input. Try again!\");\n }\n else if (passwordLength < 8 || passwordLength > 128) { // If invalid password length.\n alert(\"You must choose a password length of at least 8 characters and no more than 128 characters. Try again!\");\n }\n else { // Valid input, proceed.\n // Prompt user for character types to be used in password.\n while (charStr.length === 0) {\n // Ask to include lowercase letters\n if (confirm(\"Include lowercase letters?\")) {\n charStr += lower;\n charTypeLog += \"Lowercase\\n\";\n }\n // Ask to include uppercase letters\n if (confirm(\"Include uppercase letters?\")) {\n charStr += upper;\n charTypeLog += \"Uppercase\\n\";\n }\n // Ask to include numbers\n if (confirm(\"Include numbers?\")) {\n charStr += nums;\n charTypeLog += \"Numbers\\n\";\n }\n // Ask to include special characters\n if (confirm(\"Include special characters?\")) {\n charStr += specialChars;\n charTypeLog += \"Special Characters\\n\";\n }\n // Check if character type has been selected\n if (charStr.length > 0) {\n // Log for password criteria\n console.log(charTypeLog + \"\\n\");\n console.log(charStr + \"\\n\");\n } else {\n alert(\"At least one character type should be selected!\");\n }\n }\n // Generate Password\n for (let i = 0; i < passwordLength; i++) {\n // Append random character based on size of charStr\n passwd += charStr[Math.floor(Math.random() * charStr.length)];\n }\n // Set variable to exit loop\n isGenerated = true;\n }\n }\n // Return generated password\n return passwd;\n}\n\n// Add event listener to generate button\ngenerateBtn.addEventListener(\"click\", writePassword);"} {"doc_id": "017c68de09dade03909aae51f08a5bd9", "text": "/*\n
There are N gas stations along a circular route, where the amount of gas at station i is gas[i].
You have a car with an unlimited gas tank and it costs cost[i] of gas to travel from station i to its next station (i+1). You begin the journey with an empty tank at one of the gas stations.
Return the starting gas station's index if you can travel around the circuit once in the clockwise direction, otherwise return -1.
\n\nNote:
\n\nExample 1:
\n\n\nInput: \ngas = [1,2,3,4,5]\ncost = [3,4,5,1,2]\n\nOutput: 3\n\nExplanation:\nStart at station 3 (index 3) and fill up with 4 unit of gas. Your tank = 0 + 4 = 4\nTravel to station 4. Your tank = 4 - 1 + 5 = 8\nTravel to station 0. Your tank = 8 - 2 + 1 = 7\nTravel to station 1. Your tank = 7 - 3 + 2 = 6\nTravel to station 2. Your tank = 6 - 4 + 3 = 5\nTravel to station 3. The cost is 5. Your gas is just enough to travel back to station 3.\nTherefore, return 3 as the starting index.\n\n\n
Example 2:
\n\n\nInput: \ngas = [2,3,4]\ncost = [3,4,3]\n\nOutput: -1\n\nExplanation:\nYou can't start at station 0 or 1, as there is not enough gas to travel to the next station.\nLet's start at station 2 and fill up with 4 unit of gas. Your tank = 0 + 4 = 4\nTravel to station 0. Your tank = 4 - 3 + 2 = 3\nTravel to station 1. Your tank = 3 - 3 + 3 = 3\nYou cannot travel back to station 2, as it requires 4 unit of gas but you only have 3.\nTherefore, you can't travel around the circuit once no matter where you start.\n\n
\u5728\u4e00\u6761\u73af\u8def\u4e0a\u6709 N \u4e2a\u52a0\u6cb9\u7ad9\uff0c\u5176\u4e2d\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u6709\u6c7d\u6cb9 gas[i] \u5347\u3002
\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66\uff0c\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c i+1 \u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9 cost[i] \u5347\u3002\u4f60\u4ece\u5176\u4e2d\u7684\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u5f00\u59cb\u65f6\u6cb9\u7bb1\u4e3a\u7a7a\u3002
\u5982\u679c\u4f60\u53ef\u4ee5\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\uff0c\u5219\u8fd4\u56de\u51fa\u53d1\u65f6\u52a0\u6cb9\u7ad9\u7684\u7f16\u53f7\uff0c\u5426\u5219\u8fd4\u56de -1\u3002
\n\n\u8bf4\u660e:
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: \ngas = [1,2,3,4,5]\ncost = [3,4,5,1,2]\n\n\u8f93\u51fa: 3\n\n\u89e3\u91ca:\n\u4ece 3 \u53f7\u52a0\u6cb9\u7ad9(\u7d22\u5f15\u4e3a 3 \u5904)\u51fa\u53d1\uff0c\u53ef\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002\u6b64\u65f6\u6cb9\u7bb1\u6709 = 0 + 4 = 4 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 4 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 1 + 5 = 8 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 8 - 2 + 1 = 7 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 7 - 3 + 2 = 6 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 2 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 6 - 4 + 3 = 5 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 3 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u4f60\u9700\u8981\u6d88\u8017 5 \u5347\u6c7d\u6cb9\uff0c\u6b63\u597d\u8db3\u591f\u4f60\u8fd4\u56de\u5230 3 \u53f7\u52a0\u6cb9\u7ad9\u3002\n\u56e0\u6b64\uff0c3 \u53ef\u4e3a\u8d77\u59cb\u7d22\u5f15\u3002\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: \ngas = [2,3,4]\ncost = [3,4,3]\n\n\u8f93\u51fa: -1\n\n\u89e3\u91ca:\n\u4f60\u4e0d\u80fd\u4ece 0 \u53f7\u6216 1 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u56e0\u4e3a\u6ca1\u6709\u8db3\u591f\u7684\u6c7d\u6cb9\u53ef\u4ee5\u8ba9\u4f60\u884c\u9a76\u5230\u4e0b\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u3002\n\u6211\u4eec\u4ece 2 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u53ef\u4ee5\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002 \u6b64\u65f6\u6cb9\u7bb1\u6709 = 0 + 4 = 4 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 3 + 2 = 3 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 3 - 3 + 3 = 3 \u5347\u6c7d\u6cb9\n\u4f60\u65e0\u6cd5\u8fd4\u56de 2 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u56e0\u4e3a\u8fd4\u7a0b\u9700\u8981\u6d88\u8017 4 \u5347\u6c7d\u6cb9\uff0c\u4f46\u662f\u4f60\u7684\u6cb9\u7bb1\u53ea\u6709 3 \u5347\u6c7d\u6cb9\u3002\n\u56e0\u6b64\uff0c\u65e0\u8bba\u600e\u6837\uff0c\u4f60\u90fd\u4e0d\u53ef\u80fd\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\u3002\n
\u5728\u4e00\u6761\u73af\u8def\u4e0a\u6709 N \u4e2a\u52a0\u6cb9\u7ad9\uff0c\u5176\u4e2d\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u6709\u6c7d\u6cb9 gas[i] \u5347\u3002
\u4f60\u6709\u4e00\u8f86\u6cb9\u7bb1\u5bb9\u91cf\u65e0\u9650\u7684\u7684\u6c7d\u8f66\uff0c\u4ece\u7b2c i \u4e2a\u52a0\u6cb9\u7ad9\u5f00\u5f80\u7b2c i+1 \u4e2a\u52a0\u6cb9\u7ad9\u9700\u8981\u6d88\u8017\u6c7d\u6cb9 cost[i] \u5347\u3002\u4f60\u4ece\u5176\u4e2d\u7684\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u5f00\u59cb\u65f6\u6cb9\u7bb1\u4e3a\u7a7a\u3002
\u5982\u679c\u4f60\u53ef\u4ee5\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\uff0c\u5219\u8fd4\u56de\u51fa\u53d1\u65f6\u52a0\u6cb9\u7ad9\u7684\u7f16\u53f7\uff0c\u5426\u5219\u8fd4\u56de -1\u3002
\n\n\u8bf4\u660e:
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: \ngas = [1,2,3,4,5]\ncost = [3,4,5,1,2]\n\n\u8f93\u51fa: 3\n\n\u89e3\u91ca:\n\u4ece 3 \u53f7\u52a0\u6cb9\u7ad9(\u7d22\u5f15\u4e3a 3 \u5904)\u51fa\u53d1\uff0c\u53ef\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002\u6b64\u65f6\u6cb9\u7bb1\u6709 = 0 + 4 = 4 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 4 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 1 + 5 = 8 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 8 - 2 + 1 = 7 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 7 - 3 + 2 = 6 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 2 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 6 - 4 + 3 = 5 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 3 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u4f60\u9700\u8981\u6d88\u8017 5 \u5347\u6c7d\u6cb9\uff0c\u6b63\u597d\u8db3\u591f\u4f60\u8fd4\u56de\u5230 3 \u53f7\u52a0\u6cb9\u7ad9\u3002\n\u56e0\u6b64\uff0c3 \u53ef\u4e3a\u8d77\u59cb\u7d22\u5f15\u3002\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: \ngas = [2,3,4]\ncost = [3,4,3]\n\n\u8f93\u51fa: -1\n\n\u89e3\u91ca:\n\u4f60\u4e0d\u80fd\u4ece 0 \u53f7\u6216 1 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u56e0\u4e3a\u6ca1\u6709\u8db3\u591f\u7684\u6c7d\u6cb9\u53ef\u4ee5\u8ba9\u4f60\u884c\u9a76\u5230\u4e0b\u4e00\u4e2a\u52a0\u6cb9\u7ad9\u3002\n\u6211\u4eec\u4ece 2 \u53f7\u52a0\u6cb9\u7ad9\u51fa\u53d1\uff0c\u53ef\u4ee5\u83b7\u5f97 4 \u5347\u6c7d\u6cb9\u3002 \u6b64\u65f6\u6cb9\u7bb1\u6709 = 0 + 4 = 4 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 0 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 4 - 3 + 2 = 3 \u5347\u6c7d\u6cb9\n\u5f00\u5f80 1 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u6b64\u65f6\u6cb9\u7bb1\u6709 3 - 3 + 3 = 3 \u5347\u6c7d\u6cb9\n\u4f60\u65e0\u6cd5\u8fd4\u56de 2 \u53f7\u52a0\u6cb9\u7ad9\uff0c\u56e0\u4e3a\u8fd4\u7a0b\u9700\u8981\u6d88\u8017 4 \u5347\u6c7d\u6cb9\uff0c\u4f46\u662f\u4f60\u7684\u6cb9\u7bb1\u53ea\u6709 3 \u5347\u6c7d\u6cb9\u3002\n\u56e0\u6b64\uff0c\u65e0\u8bba\u600e\u6837\uff0c\u4f60\u90fd\u4e0d\u53ef\u80fd\u7ed5\u73af\u8def\u884c\u9a76\u4e00\u5468\u3002\n*/\n\n\n/**\n * @param {number[]} gas\n * @param {number[]} cost\n * @return {number}\n */\nvar canCompleteCircuit = function(gas, cost) {\n \n};"} {"doc_id": "017f24be033ac3eb58824169b930b996", "text": "\n\n// create data structure to store the character set and random generation function\n// MODEL\n\nconst characterSet = {\n\n // all lower case alphabets\n lowerCase: \"abcdefghijklmnopqrstuvwxyz\",\n // all upper case alphabets\n upperCase: \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\",\n // all numbers\n numbers: \"0123456789\",\n // all special characters, list provided by the instructors\n specialCharacters: \"@%+\\\\/'!#$^?:,)(}{][~-_.\"\n\n};\n\n// save user choice\nvar includeLower;\nvar includeUpper;\nvar includeNumbers;\nvar includeSymbols;\nvar passwordLength;\n\n\n// Collect User Data, View\nfunction userInput() {\n \n\n // get user input\n // get the form element\n var formInput = document.getElementById('formInput');\n\n // store users selection\n includeLower = formInput.lower.checked;\n includeUpper = formInput.upper.checked;\n includeNumbers = formInput.number.checked;\n includeSymbols = formInput.symbols.checked;\n passwordLength = parseInt(formInput.passwordLength.value);\n\n // if user doesn't select any option, default to lower case\n if(!(includeLower || includeUpper || includeNumbers || includeSymbols)) {\n includeLower = true;\n } \n\n // store all options\n var option = \"\";\n if(includeLower) {\n option += characterSet.lowerCase;\n }\n if(includeUpper) {\n option += characterSet.upperCase;\n }\n if(includeNumbers) {\n option += characterSet.numbers;\n }\n if(includeSymbols) {\n option += characterSet.specialCharacters;\n }\n\n\n // create object userChoice\n var userChoice = {\n length: passwordLength,\n allCharacterSet: option\n };\n \n console.log(userChoice.length);\n console.log(userChoice.allCharacterSet);\n\n return userChoice;\n}\n\n\n\n// Generate Random Password, Controller\nexport function generatePassword() {\n\n // will store user password, random password\n var password=\"\";\n\n // get user Input, Character Set and the length of the password\n var userChoice = userInput();\n\n // get the length of the password\n var userPwdLength = userChoice.length;\n\n // get the character Set, all the option the user wants , is lowerCase by default\n var userCharacterSet = userChoice.allCharacterSet;\n\n\n // password must include at least 1 character from the characterSets \n var password = includeAtleastOneCharacter();\n var additionalPwd = userPwdLength - password.length;\n // create a loop to get a random number from 0 to the total length of the userCharacterSet-1\n // user that random number as an index\n // pick a random character untill we get desired number of password length\n for(let i = 0; i < additionalPwd; i++) {\n\n // Math.random() creates a random number between 0 to 1 , \n // Multiply it by the total length of the userCharacterSet to get the length\n // Round the result with Math.floor()\n var randomIndex = Math.floor( Math.random() * userCharacterSet.length );\n \n // user the random number as an index, get the random character and add save it\n // String is also of type array, we can access the string as with the array\n password += userCharacterSet[ randomIndex ];\n }\n\n // console log the result for testing, print both password and \n console.log(\"password : \" + password)\n console.log(\"password length: \" + password.length);\n\n // to avoid any further pattern we can shuffle it, \n // because we didn't pick from each charactre set individually, we don't have a pattern like\n // lowercaseRandom + upperCaseRandom + numberRandom + specialCharacterrandom \n\n return password;\n}\n\n\n// picks atleast 1 character from the characters set\nfunction includeAtleastOneCharacter() {\n var password = \"\";\n // random index\n var randomIndex = Math.floor( Math.random() * 10);\n\n if(includeLower) {\n password += characterSet.lowerCase[randomIndex];\n }\n\n if(includeUpper) {\n password += characterSet.upperCase[randomIndex];\n }\n\n if(includeNumbers) {\n password += characterSet.numbers[randomIndex];\n }\n\n if(includeSymbols) {\n password += characterSet.specialCharacters[randomIndex];\n }\n\n console.log(\"at : \" + password);\n\n return password;\n}\n\n"} {"doc_id": "01817c0f4578cddbbfec3fbd88a95e51", "text": "/*\n
\nIn English, we have a concept called root, which can be followed by some other words to form another longer word - let's call this word successor. For example, the root an, followed by other, which can form another word another.\n
\nNow, given a dictionary consisting of many roots and a sentence. You need to replace all the successor in the sentence with the root forming it. If a successor has many roots can form it, replace it with the root with the shortest length.\n
\nYou need to output the sentence after the replacement.\n
\n\n\nExample 1:
\n
\nInput: dict = [\"cat\", \"bat\", \"rat\"]\nsentence = \"the cattle was rattled by the battery\"\nOutput: \"the cat was rat by the bat\"\n\n\n\n\n
Note:
\n
\u5728\u82f1\u8bed\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u53eb\u505a \u8bcd\u6839(root)\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8ddf\u7740\u5176\u4ed6\u4e00\u4e9b\u8bcd\u7ec4\u6210\u53e6\u4e00\u4e2a\u8f83\u957f\u7684\u5355\u8bcd——\u6211\u4eec\u79f0\u8fd9\u4e2a\u8bcd\u4e3a \u7ee7\u627f\u8bcd(successor)\u3002\u4f8b\u5982\uff0c\u8bcd\u6839an\uff0c\u8ddf\u968f\u7740\u5355\u8bcd other(\u5176\u4ed6)\uff0c\u53ef\u4ee5\u5f62\u6210\u65b0\u7684\u5355\u8bcd another(\u53e6\u4e00\u4e2a)\u3002
\u73b0\u5728\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u7531\u8bb8\u591a\u8bcd\u6839\u7ec4\u6210\u7684\u8bcd\u5178\u548c\u4e00\u4e2a\u53e5\u5b50\u3002\u4f60\u9700\u8981\u5c06\u53e5\u5b50\u4e2d\u7684\u6240\u6709\u7ee7\u627f\u8bcd\u7528\u8bcd\u6839\u66ff\u6362\u6389\u3002\u5982\u679c\u7ee7\u627f\u8bcd\u6709\u8bb8\u591a\u53ef\u4ee5\u5f62\u6210\u5b83\u7684\u8bcd\u6839\uff0c\u5219\u7528\u6700\u77ed\u7684\u8bcd\u6839\u66ff\u6362\u5b83\u3002
\u4f60\u9700\u8981\u8f93\u51fa\u66ff\u6362\u4e4b\u540e\u7684\u53e5\u5b50\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: dict(\u8bcd\u5178) = ["cat", "bat", "rat"]\nsentence(\u53e5\u5b50) = "the cattle was rattled by the battery"\n\u8f93\u51fa: "the cat was rat by the bat"\n\n\n
\u6ce8:
\n\n\u5728\u82f1\u8bed\u4e2d\uff0c\u6211\u4eec\u6709\u4e00\u4e2a\u53eb\u505a \u8bcd\u6839(root)\u7684\u6982\u5ff5\uff0c\u5b83\u53ef\u4ee5\u8ddf\u7740\u5176\u4ed6\u4e00\u4e9b\u8bcd\u7ec4\u6210\u53e6\u4e00\u4e2a\u8f83\u957f\u7684\u5355\u8bcd——\u6211\u4eec\u79f0\u8fd9\u4e2a\u8bcd\u4e3a \u7ee7\u627f\u8bcd(successor)\u3002\u4f8b\u5982\uff0c\u8bcd\u6839an\uff0c\u8ddf\u968f\u7740\u5355\u8bcd other(\u5176\u4ed6)\uff0c\u53ef\u4ee5\u5f62\u6210\u65b0\u7684\u5355\u8bcd another(\u53e6\u4e00\u4e2a)\u3002
\u73b0\u5728\uff0c\u7ed9\u5b9a\u4e00\u4e2a\u7531\u8bb8\u591a\u8bcd\u6839\u7ec4\u6210\u7684\u8bcd\u5178\u548c\u4e00\u4e2a\u53e5\u5b50\u3002\u4f60\u9700\u8981\u5c06\u53e5\u5b50\u4e2d\u7684\u6240\u6709\u7ee7\u627f\u8bcd\u7528\u8bcd\u6839\u66ff\u6362\u6389\u3002\u5982\u679c\u7ee7\u627f\u8bcd\u6709\u8bb8\u591a\u53ef\u4ee5\u5f62\u6210\u5b83\u7684\u8bcd\u6839\uff0c\u5219\u7528\u6700\u77ed\u7684\u8bcd\u6839\u66ff\u6362\u5b83\u3002
\u4f60\u9700\u8981\u8f93\u51fa\u66ff\u6362\u4e4b\u540e\u7684\u53e5\u5b50\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: dict(\u8bcd\u5178) = ["cat", "bat", "rat"]\nsentence(\u53e5\u5b50) = "the cattle was rattled by the battery"\n\u8f93\u51fa: "the cat was rat by the bat"\n\n\n
\u6ce8:
\n\nThere are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to finish all courses.
\n\nThere may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.
\n\nExample 1:
\n\n\nInput: 2, [[1,0]] \nOutput:\n\n[0,1]\nExplanation: There are a total of 2 courses to take. To take course 1 you should have finished \n course 0. So the correct course order is[0,1] .
Example 2:
\n\n\nInput: 4, [[1,0],[2,0],[3,1],[3,2]]\nOutput:\n\n[0,1,2,3] or [0,2,1,3]\nExplanation: There are a total of 4 courses to take. To take course 3 you should have finished both \n courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. \n So one correct course order is[0,1,2,3]. Another correct ordering is[0,2,1,3] .
Note:
\n\n\u73b0\u5728\u4f60\u603b\u5171\u6709 n \u95e8\u8bfe\u9700\u8981\u9009\uff0c\u8bb0\u4e3a 0 \u5230 n-1\u3002
\u5728\u9009\u4fee\u67d0\u4e9b\u8bfe\u7a0b\u4e4b\u524d\u9700\u8981\u4e00\u4e9b\u5148\u4fee\u8bfe\u7a0b\u3002 \u4f8b\u5982\uff0c\u60f3\u8981\u5b66\u4e60\u8bfe\u7a0b 0 \uff0c\u4f60\u9700\u8981\u5148\u5b8c\u6210\u8bfe\u7a0b 1 \uff0c\u6211\u4eec\u7528\u4e00\u4e2a\u5339\u914d\u6765\u8868\u793a\u4ed6\u4eec: [0,1]
\u7ed9\u5b9a\u8bfe\u7a0b\u603b\u91cf\u4ee5\u53ca\u5b83\u4eec\u7684\u5148\u51b3\u6761\u4ef6\uff0c\u8fd4\u56de\u4f60\u4e3a\u4e86\u5b66\u5b8c\u6240\u6709\u8bfe\u7a0b\u6240\u5b89\u6392\u7684\u5b66\u4e60\u987a\u5e8f\u3002
\n\n\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u6b63\u786e\u7684\u987a\u5e8f\uff0c\u4f60\u53ea\u8981\u8fd4\u56de\u4e00\u79cd\u5c31\u53ef\u4ee5\u4e86\u3002\u5982\u679c\u4e0d\u53ef\u80fd\u5b8c\u6210\u6240\u6709\u8bfe\u7a0b\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: 2, [[1,0]] \n\u8f93\u51fa:\n\n[0,1]\n\u89e3\u91ca: \u603b\u5171\u6709 2 \u95e8\u8bfe\u7a0b\u3002\u8981\u5b66\u4e60\u8bfe\u7a0b 1\uff0c\u4f60\u9700\u8981\u5148\u5b8c\u6210\u8bfe\u7a0b 0\u3002\u56e0\u6b64\uff0c\u6b63\u786e\u7684\u8bfe\u7a0b\u987a\u5e8f\u4e3a[0,1] \u3002
\u793a\u4f8b 2:
\n\n\u8f93\u5165: 4, [[1,0],[2,0],[3,1],[3,2]]\n\u8f93\u51fa:\n\n[0,1,2,3] or [0,2,1,3]\n\u89e3\u91ca: \u603b\u5171\u6709 4 \u95e8\u8bfe\u7a0b\u3002\u8981\u5b66\u4e60\u8bfe\u7a0b 3\uff0c\u4f60\u5e94\u8be5\u5148\u5b8c\u6210\u8bfe\u7a0b 1 \u548c\u8bfe\u7a0b 2\u3002\u5e76\u4e14\u8bfe\u7a0b 1 \u548c\u8bfe\u7a0b 2 \u90fd\u5e94\u8be5\u6392\u5728\u8bfe\u7a0b 0 \u4e4b\u540e\u3002\n \u56e0\u6b64\uff0c\u4e00\u4e2a\u6b63\u786e\u7684\u8bfe\u7a0b\u987a\u5e8f\u662f[0,1,2,3]\u3002\u53e6\u4e00\u4e2a\u6b63\u786e\u7684\u6392\u5e8f\u662f[0,2,1,3]\u3002\n
\u8bf4\u660e:
\n\n\u63d0\u793a:
\n\n\u62d3\u6251\u6392\u5e8f\u4e5f\u53ef\u4ee5\u901a\u8fc7 BFS \u5b8c\u6210\u3002
\n\t\u73b0\u5728\u4f60\u603b\u5171\u6709 n \u95e8\u8bfe\u9700\u8981\u9009\uff0c\u8bb0\u4e3a 0 \u5230 n-1\u3002
\u5728\u9009\u4fee\u67d0\u4e9b\u8bfe\u7a0b\u4e4b\u524d\u9700\u8981\u4e00\u4e9b\u5148\u4fee\u8bfe\u7a0b\u3002 \u4f8b\u5982\uff0c\u60f3\u8981\u5b66\u4e60\u8bfe\u7a0b 0 \uff0c\u4f60\u9700\u8981\u5148\u5b8c\u6210\u8bfe\u7a0b 1 \uff0c\u6211\u4eec\u7528\u4e00\u4e2a\u5339\u914d\u6765\u8868\u793a\u4ed6\u4eec: [0,1]
\u7ed9\u5b9a\u8bfe\u7a0b\u603b\u91cf\u4ee5\u53ca\u5b83\u4eec\u7684\u5148\u51b3\u6761\u4ef6\uff0c\u8fd4\u56de\u4f60\u4e3a\u4e86\u5b66\u5b8c\u6240\u6709\u8bfe\u7a0b\u6240\u5b89\u6392\u7684\u5b66\u4e60\u987a\u5e8f\u3002
\n\n\u53ef\u80fd\u4f1a\u6709\u591a\u4e2a\u6b63\u786e\u7684\u987a\u5e8f\uff0c\u4f60\u53ea\u8981\u8fd4\u56de\u4e00\u79cd\u5c31\u53ef\u4ee5\u4e86\u3002\u5982\u679c\u4e0d\u53ef\u80fd\u5b8c\u6210\u6240\u6709\u8bfe\u7a0b\uff0c\u8fd4\u56de\u4e00\u4e2a\u7a7a\u6570\u7ec4\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: 2, [[1,0]] \n\u8f93\u51fa:\n\n[0,1]\n\u89e3\u91ca: \u603b\u5171\u6709 2 \u95e8\u8bfe\u7a0b\u3002\u8981\u5b66\u4e60\u8bfe\u7a0b 1\uff0c\u4f60\u9700\u8981\u5148\u5b8c\u6210\u8bfe\u7a0b 0\u3002\u56e0\u6b64\uff0c\u6b63\u786e\u7684\u8bfe\u7a0b\u987a\u5e8f\u4e3a[0,1] \u3002
\u793a\u4f8b 2:
\n\n\u8f93\u5165: 4, [[1,0],[2,0],[3,1],[3,2]]\n\u8f93\u51fa:\n\n[0,1,2,3] or [0,2,1,3]\n\u89e3\u91ca: \u603b\u5171\u6709 4 \u95e8\u8bfe\u7a0b\u3002\u8981\u5b66\u4e60\u8bfe\u7a0b 3\uff0c\u4f60\u5e94\u8be5\u5148\u5b8c\u6210\u8bfe\u7a0b 1 \u548c\u8bfe\u7a0b 2\u3002\u5e76\u4e14\u8bfe\u7a0b 1 \u548c\u8bfe\u7a0b 2 \u90fd\u5e94\u8be5\u6392\u5728\u8bfe\u7a0b 0 \u4e4b\u540e\u3002\n \u56e0\u6b64\uff0c\u4e00\u4e2a\u6b63\u786e\u7684\u8bfe\u7a0b\u987a\u5e8f\u662f[0,1,2,3]\u3002\u53e6\u4e00\u4e2a\u6b63\u786e\u7684\u6392\u5e8f\u662f[0,2,1,3]\u3002\n
\u8bf4\u660e:
\n\n\u63d0\u793a:
\n\n\u62d3\u6251\u6392\u5e8f\u4e5f\u53ef\u4ee5\u901a\u8fc7 BFS \u5b8c\u6210\u3002
\n\t\nGiven a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1.\n
\nExamples:\n
\ns = \"leetcode\"\nreturn 0.\n\ns = \"loveleetcode\",\nreturn 2.\n\n\n\n
\nNote: You may assume the string contain only lowercase letters.\n
\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u627e\u5230\u5b83\u7684\u7b2c\u4e00\u4e2a\u4e0d\u91cd\u590d\u7684\u5b57\u7b26\uff0c\u5e76\u8fd4\u56de\u5b83\u7684\u7d22\u5f15\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002
\n\n\u6848\u4f8b:
\n\n\ns = "leetcode"\n\u8fd4\u56de 0.\n\ns = "loveleetcode",\n\u8fd4\u56de 2.\n\n\n
\n\n
\u6ce8\u610f\u4e8b\u9879\uff1a\u60a8\u53ef\u4ee5\u5047\u5b9a\u8be5\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u5c0f\u5199\u5b57\u6bcd\u3002
\n\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u627e\u5230\u5b83\u7684\u7b2c\u4e00\u4e2a\u4e0d\u91cd\u590d\u7684\u5b57\u7b26\uff0c\u5e76\u8fd4\u56de\u5b83\u7684\u7d22\u5f15\u3002\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1\u3002
\n\n\u6848\u4f8b:
\n\n\ns = "leetcode"\n\u8fd4\u56de 0.\n\ns = "loveleetcode",\n\u8fd4\u56de 2.\n\n\n
\n\n
\u6ce8\u610f\u4e8b\u9879\uff1a\u60a8\u53ef\u4ee5\u5047\u5b9a\u8be5\u5b57\u7b26\u4e32\u53ea\u5305\u542b\u5c0f\u5199\u5b57\u6bcd\u3002
\n*/\n\n\n/**\n * @param {string} s\n * @return {number}\n */\nvar firstUniqChar = function(s) {\n \n};"} {"doc_id": "0247b8d95fe94097936556515ca2a275", "text": "/*\n\nGiven a word, you need to judge whether the usage of capitals in it is right or not.\n
\n\n\nWe define the usage of capitals in a word to be right when one of the following cases holds:\n
Example 1:
\n
\nInput: \"USA\"\nOutput: True\n\n\n\n
Example 2:
\n
\nInput: \"FlaG\"\nOutput: False\n\n\n\n
Note:\nThe input will be a non-empty word consisting of uppercase and lowercase latin letters.\n
\u7ed9\u5b9a\u4e00\u4e2a\u5355\u8bcd\uff0c\u4f60\u9700\u8981\u5224\u65ad\u5355\u8bcd\u7684\u5927\u5199\u4f7f\u7528\u662f\u5426\u6b63\u786e\u3002
\n\n\u6211\u4eec\u5b9a\u4e49\uff0c\u5728\u4ee5\u4e0b\u60c5\u51b5\u65f6\uff0c\u5355\u8bcd\u7684\u5927\u5199\u7528\u6cd5\u662f\u6b63\u786e\u7684\uff1a
\n\n\u5426\u5219\uff0c\u6211\u4eec\u5b9a\u4e49\u8fd9\u4e2a\u5355\u8bcd\u6ca1\u6709\u6b63\u786e\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: "USA"\n\u8f93\u51fa: True\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: "FlaG"\n\u8f93\u51fa: False\n\n\n
\u6ce8\u610f: \u8f93\u5165\u662f\u7531\u5927\u5199\u548c\u5c0f\u5199\u62c9\u4e01\u5b57\u6bcd\u7ec4\u6210\u7684\u975e\u7a7a\u5355\u8bcd\u3002
\n\u7ed9\u5b9a\u4e00\u4e2a\u5355\u8bcd\uff0c\u4f60\u9700\u8981\u5224\u65ad\u5355\u8bcd\u7684\u5927\u5199\u4f7f\u7528\u662f\u5426\u6b63\u786e\u3002
\n\n\u6211\u4eec\u5b9a\u4e49\uff0c\u5728\u4ee5\u4e0b\u60c5\u51b5\u65f6\uff0c\u5355\u8bcd\u7684\u5927\u5199\u7528\u6cd5\u662f\u6b63\u786e\u7684\uff1a
\n\n\u5426\u5219\uff0c\u6211\u4eec\u5b9a\u4e49\u8fd9\u4e2a\u5355\u8bcd\u6ca1\u6709\u6b63\u786e\u4f7f\u7528\u5927\u5199\u5b57\u6bcd\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: "USA"\n\u8f93\u51fa: True\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: "FlaG"\n\u8f93\u51fa: False\n\n\n
\u6ce8\u610f: \u8f93\u5165\u662f\u7531\u5927\u5199\u548c\u5c0f\u5199\u62c9\u4e01\u5b57\u6bcd\u7ec4\u6210\u7684\u975e\u7a7a\u5355\u8bcd\u3002
\n*/\n\n\n/**\n * @param {string} word\n * @return {boolean}\n */\nvar detectCapitalUse = function(word) {\n \n};"} {"doc_id": "04665eaaeb03c031d1833ed740164913", "text": "/**\n * Performance Review\n * 3 - superstar\n * 2 - meets expectations\n * 1 - needs improvement\n * anything else - wtf?\n */\n\nlet rating = 0;\n\nif (rating === 3) {\n console.log(\"YOU ARE A SUPERSTAR!\");\n} else if (rating === 2) {\n console.log(\"MEETS EXPECTATIONS\");\n} else if (rating === 1) {\n console.log(\"NEEDS IMPROVEMENT\");\n} else {\n // console.log(\"INVALID RATING!\");\n}\n\n//Exemple 2\nlet highScore = 1430;\nlet userScore = 1300;\n\nif (userScore >= highScore) {\n // console.log(`Congrats, you have new high score of ${userScore}`);\n highScore = userScore;\n} else {\n // console.log(\n // `Good Game. Your score of ${userScore} did not beat the high score ${highScore}`\n // );\n}\n\n//exemple 3 - Nestnig Conditionals\nlet password = \"teste321\";\n\nif (password.length >= 6) {\n if (password.indexOf(\" \") === -1) {\n // console.log(\"Valid Password\");\n } else {\n // console.log(\"Password is long enough, but cannot contain spaces\");\n }\n} else {\n // console.log(\"Password must be longer\");\n}\n\n// Exemple 4 - TRUTHY & FALSY VALUES\n/**\n * All values have an inherent truthy or falsy boolean value.\n *\n * FALSY VALUES:\n * false\n * 0\n * \"\" (empty string)\n * null\n * undefined\n * NaN\n * Everything else is TRUTHY.\n */\nlet mystery = 5;\n\nif (mystery) {\n // console.log(\"TRUTHY\");\n} else {\n // console.log(\"FALSY\");\n}\n\n/**\n * LOGICAL OPERATORS\n */\n\n// AND (&&)\nlet passwords = \"minhasenha\";\n\nif (passwords.length >= 8 && passwords.indexOf(\" \") === -1) {\n // console.log(\"VALID PASSWORD\");\n} else {\n // console.log(\"INVALID PASSWORD\");\n}\n\n// exemple 2\nlet num = 3;\nif (num >= 1 && num <= 10) {\n // console.log(\"Number is between 1 and 10\");\n} else {\n // console.log(\"Please guess a number between 1 and 10\");\n}\n\n// exemple 3\n// OR (||)\nlet age = 30;\n\nif (age < 6 || age >= 65) {\n // console.log(\"YOU GET IN FOR FREE\");\n} else {\n // console.log(\"YOU MUST PAYYYY!\");\n}\n\n// exemple 4\nlet color = \"purple\";\n\nif (color === \"purple\" || color === \"lilac\" || color === \"violet\") {\n // console.log(\"GREAT CHOICE\");\n}\n\n// NOT (!)\nlet loggedInUser;\n\nif (!loggedInUser) {\n console.log(\"GET OUT OF HERE\");\n}\n\n// exemple 2\n\nlet flavor = \"grepe\";\n\n// if (flavor !== \"grepe\" && flavor !== \"cherry\") {\n// console.log(\"WE DON'T HAVE THAT FLAVOR!\");\n// }\n\nif (!(flavor === \"grepe\" && flavor === \"cherry\")) {\n console.log(\"WE DON'T HAVE THAT FLAVOR\");\n}\n"} {"doc_id": "04aaf8ac396a622fd9016bb34c000b0b", "text": "// Assignment code here\nfunction generatePassword()\n{\n//Do while loop to prompt for password length for user input.\n do {\n var passlength = window.prompt(\"How long would you like your password to be?\");\n console.log(\"Length of Password: \" + passlength);\n } while (passlength < 8 || passlength > 128)\n\n//Variables for window confirms for each question prompt.\n var passSc = window.confirm(\"Do you want symbols in your password?\");\n var passNM = window.confirm(\"Do you want numerical characters in your password?\");\n var passUC = window.confirm(\"Do you want uppercase characters in your password?\");\n var passLC = window.confirm(\"Do you want lowercase characters in your password?\");\n\n var character = \"\";\n var result = [];\n\n//If statements determining characters for each variable\n if (passSc == true)\n {\n character += \"+-!@#$%^&()?><\";\n console.log(\"passSc is True\");\n }\n if (passNM == true)\n {\n character += \"0123456789\";\n console.log(\"passNM is True\");\n }\n if (passUC == true)\n {\n character += \"ABCDEFGHIJKLMOPQRSTUVWXYZ\";\n console.log(\"passUC is True\");\n }\n if (passLC == true)\n {\n character += \"abcdefghijklmnopqrstuvwxyz\";\n console.log(\"passLC is True\");\n } \n\n console.log(character)\n\n for (var i = 0; i < passlength; i++)\n {\n result += character.charAt(Math.floor(Math.random() * character.length));\n } \n\n return result;\n}\n// code to display result\n\n\n// Get references to the #generate element\nvar generateBtn = document.querySelector(\"#generate\");\n\n// Write password to the #password input\nfunction writePassword() {\n var password = generatePassword();\n var passwordText = document.querySelector(\"#password\");\n\n passwordText.value = password;\n\n}\n\n// Add event listener to generate button\ngenerateBtn.addEventListener(\"click\", writePassword);\n"} {"doc_id": "06062562866313c17977d691dfd2f17a", "text": "/*\nGiven a positive integer n, return the number of all possible attendance records with length n, which will be regarded as rewardable. The answer may be very large, return it after mod 109 + 7.
\n\nA student attendance record is a string that only contains the following three characters:
\n\n\n
\nA record is regarded as rewardable if it doesn't contain more than one 'A' (absent) or more than two continuous 'L' (late).
\n\nExample 1:
\n
\nInput: n = 2\nOutput: 8 \nExplanation:\nThere are 8 records with length 2 will be regarded as rewardable:\n\"PP\" , \"AP\", \"PA\", \"LP\", \"PL\", \"AL\", \"LA\", \"LL\"\nOnly \"AA\" won't be regarded as rewardable owing to more than one absent times. \n\n\n\n
Note:\nThe value of n won't exceed 100,000.\n
\n\n\n\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 n\uff0c\u8fd4\u56de\u957f\u5ea6\u4e3a n \u7684\u6240\u6709\u53ef\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\u7684\u51fa\u52e4\u8bb0\u5f55\u7684\u6570\u91cf\u3002 \u7b54\u6848\u53ef\u80fd\u975e\u5e38\u5927\uff0c\u4f60\u53ea\u9700\u8fd4\u56de\u7ed3\u679cmod 109 + 7\u7684\u503c\u3002
\n\n\u5b66\u751f\u51fa\u52e4\u8bb0\u5f55\u662f\u53ea\u5305\u542b\u4ee5\u4e0b\u4e09\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff1a
\n\n\u5982\u679c\u8bb0\u5f55\u4e0d\u5305\u542b\u591a\u4e8e\u4e00\u4e2a'A'\uff08\u7f3a\u52e4\uff09\u6216\u8d85\u8fc7\u4e24\u4e2a\u8fde\u7eed\u7684'L'\uff08\u8fdf\u5230\uff09\uff0c\u5219\u8be5\u8bb0\u5f55\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\u7684\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: n = 2\n\u8f93\u51fa: 8 \n\u89e3\u91ca\uff1a\n\u67098\u4e2a\u957f\u5ea6\u4e3a2\u7684\u8bb0\u5f55\u5c06\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\uff1a\n"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"\n\u53ea\u6709"AA"\u4e0d\u4f1a\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\uff0c\u56e0\u4e3a\u7f3a\u52e4\u6b21\u6570\u8d85\u8fc7\u4e00\u6b21\u3002\n\n
\u6ce8\u610f\uff1an \u7684\u503c\u4e0d\u4f1a\u8d85\u8fc7100000\u3002
\n\u7ed9\u5b9a\u4e00\u4e2a\u6b63\u6574\u6570 n\uff0c\u8fd4\u56de\u957f\u5ea6\u4e3a n \u7684\u6240\u6709\u53ef\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\u7684\u51fa\u52e4\u8bb0\u5f55\u7684\u6570\u91cf\u3002 \u7b54\u6848\u53ef\u80fd\u975e\u5e38\u5927\uff0c\u4f60\u53ea\u9700\u8fd4\u56de\u7ed3\u679cmod 109 + 7\u7684\u503c\u3002
\n\n\u5b66\u751f\u51fa\u52e4\u8bb0\u5f55\u662f\u53ea\u5305\u542b\u4ee5\u4e0b\u4e09\u4e2a\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\uff1a
\n\n\u5982\u679c\u8bb0\u5f55\u4e0d\u5305\u542b\u591a\u4e8e\u4e00\u4e2a'A'\uff08\u7f3a\u52e4\uff09\u6216\u8d85\u8fc7\u4e24\u4e2a\u8fde\u7eed\u7684'L'\uff08\u8fdf\u5230\uff09\uff0c\u5219\u8be5\u8bb0\u5f55\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\u7684\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: n = 2\n\u8f93\u51fa: 8 \n\u89e3\u91ca\uff1a\n\u67098\u4e2a\u957f\u5ea6\u4e3a2\u7684\u8bb0\u5f55\u5c06\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\uff1a\n"PP" , "AP", "PA", "LP", "PL", "AL", "LA", "LL"\n\u53ea\u6709"AA"\u4e0d\u4f1a\u88ab\u89c6\u4e3a\u53ef\u5956\u52b1\uff0c\u56e0\u4e3a\u7f3a\u52e4\u6b21\u6570\u8d85\u8fc7\u4e00\u6b21\u3002\n\n
\u6ce8\u610f\uff1an \u7684\u503c\u4e0d\u4f1a\u8d85\u8fc7100000\u3002
\n*/\n\n\n/**\n * @param {number} n\n * @return {number}\n */\nvar checkRecord = function(n) {\n \n};"} {"doc_id": "06ce34220a58450eb6119a3fe7829177", "text": "// \"use strict\";\n// single line comment\n\n/* \n multi\n line\n comment\n*/\n\n/**\n * Documentation comments\n * are multi line\n */\n\n/*\n JS Types:\n Primitives:\n - string\n - number\n - bigint\n - boolean\n - symbol\n - undefined\n - null\n\n Objects:\n - object\n - array\n - function (first-class members)\n\n \n*/\n\n// standard function declaration\nfunction a() {\n console.log('this is a named function declaration');\n}\n\na();\nconsole.log(a);\n\n// anonymous function declaration\nlet x = function() {\n console.log('this is an anonymous function assigned to a variable named \"x\".');\n}\n\nx();\nconsole.log(x);\n\n// IIFEs (immediately invoked function expressions)\n(function() {\n console.log('this is an immediately invoked function expression!')\n})();\n\n/*\n Variable declarative keywords\n - var\n - let (introduced in ES6)\n - const (introduced in ES6)\n\n Variable Scopes:\n - global\n - function/local\n - block\n - lexical (closures)\n*/\n\nfunction varScope() {\n\n console.log('line 70: ' + greeting);\n\n if (5 > 4) {\n // the variable \"greeting\" is hoisted to the top of the function scope\n var greeting = \"hello\"; \n console.log('line 74: ' + greeting);\n }\n\n console.log('line 77: ' + greeting);\n\n}\n\nvarScope();\n\nfunction letScope() {\n\n // throws a ReferenceError because \"greeting\" is not hoisted\n // console.log('line 86: ' + greeting); \n\n if (5 > 4) {\n // the variable \"greeting\" is NOT hoisted (because we used let)\n let greeting = \"hello\"; \n console.log('line 91: ' + greeting);\n\n if (4 < 5) {\n console.log('line 94: ' + greeting);\n }\n }\n\n // \"greeting\"is not visible here because let/const are scoped to the block in which they are declared\n // console.log('line 98: ' + greeting);\n\n}\n\nletScope();\n\nfunction constScope() {\n console.log(constantGreeting);\n const constantGreeting = \"hola!\";\n}\n\n// Cannot reference const before declaration, it is not hoisted.\n// constScope();\n\n\n// function hoisting\nmyFunction()\nfunction myFunction() {\n console.log('this is my function!');\n}\n\n\n// otherFunction();\nvar otherFunction = function() {\n console.log('this is another function');\n}\n\n// more on const\n// const y; // you must initialize const variables when declaring them\n\n// const variable \nconst y = 42;\n// y = 43; // once initialized, const variables can never be reassigned\n\nconst immutableMaybe = {\n someValue: 10,\n anotherValue: 'test',\n nestedObj: {\n nestedValue: 100\n }\n};\n\n// not allowed, for obvious reasons (reassigning a const variable)\n// immutableMaybe = {};\n\nconsole.log(immutableMaybe);\n\nimmutableMaybe.someValue = 11;\n\nconsole.log(immutableMaybe);\n\n// basic object\nlet myObject = {\n a: 'a',\n b: 2,\n c: true,\n d: function() {\n console.log('d');\n },\n e() {\n console.log('e');\n },\n 'some property': 'some value'\n}\n\nmyObject.e();\nmyObject.d();\n\nconsole.log(myObject.a); // dot notation for accessing object members\nconsole.log(myObject['b']); // bracket notation for accessing object members\nconsole.log(myObject['some property']);\n\n\n// old-school way of creating a JS \"class\"\nfunction Car(model) {\n this.model = model;\n this.color = 'silver';\n this.year = '2015';\n\n // this.toString = function() {\n // console.log(this.model + ', ' + this.color + ', ' + this.year);\n // }\n}\n\n// to properly overwrite the toString method, we need to access the prototype object within our Car \"class\"\nCar.prototype.toString = function() {\n console.log(`${this.model}, ${this.color}, ${this.year}`);\n}\n\nlet myCar = new Car('Nissan Rogue');\nconsole.log(myCar);\nmyCar.toString();\n\nCar.prototype.toString = function() {\n console.log(`${this.model}, ${this.color}, ${this.year}`, `${4 + 2 + 3}`); // template literals\n}\n\nlet yourCar = new Car('Honda Civic');\nconsole.log(yourCar);\nyourCar.toString();\n\n// ES6 Class syntax (sugar syntax)\nclass Truck {\n\n // only one constructor allow in ES6 class syntax\n constructor(model, year, color = 'silver') {\n this.model = model;\n this.year = year;\n this.color = color;\n }\n\n that = this;\n\n squeal() {\n console.log(this == this.that);\n console.log('SQUUUUUUEAK');\n }\n\n beep = function() {\n console.log(this == this.that);\n console.log(\"BEEEEEP!\");\n }\n\n honk = () => {\n console.log(this == this.that);\n console.log('HONK!');\n }\n\n}\n\nlet myTruck = new Truck('Toyota Tacoma', 2020, 'black');\nconsole.log(myTruck);\nmyTruck.beep();\nmyTruck.honk();\nmyTruck.squeal();\n\n// Arrays\nlet myArr = [1, 2, 3];\nlet crazyArray = [1, true, 'hello', new Car(), new Truck(), function () { console.log ('whut?!')}];\n\nconsole.log(myArr[0]); // 1\n\n// passing a arrow function to another function/method\nlet squaredValues = myArr.map(val => {\n return Math.pow(val, 2);\n});\n\nconsole.log(squaredValues);\n\nconsole.log(this); // in Node this = {}, in the browser it would = the Window object\n\n// creates its own 'this', which is the global NodeJS object\nfunction a() {\n console.log(this); // this == Object [global] {...} (provided by NodeJS)\n}\na();\n\n// creates its own 'this', which is the global NodeJS object\nlet b = function() {\n console.log(this); // this == Object [global] {...} (provided by NodeJS)\n}\nb();\n\n// does not create it own 'this', instead it inherits the 'this' from the outer scope.\nlet c = () => {\n console.log(this);\n}\nc();\n\n\nfunction D() {\n this.x = 1;\n this.y = 2;\n this.z = 3;\n console.log(this); // this == D {...}\n}\nlet d = new D();\n\nfunction E() {\n this.x = 1;\n\n console.log(this); // this == F {...}\n\n let that = this;\n\n const b = () => {\n console.log(this == that);\n }\n\n b();\n\n const c = function () {\n console.log(this == that);\n }\n\n c();\n}\n\nlet e = new E();\n\n/*\n Truthy and Falsy\n\n - due to type coercion all values in JS can be interpreted as booleans\n - just remember: there are only 6 falsy values:\n - false\n - 0\n - NaN\n - null \n - undefined\n - \"\", '', ``\n*/\n\nconsole.log(10 / 0);\nconsole.log('hello ' + 3); // type coercion\nconsole.log('hello' - 3); // NaN\nconsole.log('hello ' * 3); // NaN\nconsole.log(('hello' - 3) == ('hello' - 3)); // surprisingly false, because...\nconsole.log(NaN == NaN); // false\nconsole.log(typeof(NaN)); // not a number is a number!\nconsole.log('2' == 2); // comparison operator that performs type coercion\nconsole.log('2' === 2); // comparison operator that DOES NOT perform type coercion\n\nlet username = {};\nlet password = 'password';\n\nif (username && password) {\n login(username, password);\n} else {\n console.log('you provided empty or null values!');\n}\n\nfunction login(un, pw) {\n console.log(`Logging in: ${un} with ${pw}`);\n}\n\nconsole.log(([]+{}+!{}));\n\nconsole.log(!!\"dsgafgkj.usgarfa.hsgfjavf\");"} {"doc_id": "06d756a58ead6584829e9cd3eadcf860", "text": "/* lab 11_rps\n\n* When you click a button, the computer picks a move with the getRandomPlay() function\n* Update the scoreboard in the event of a win\n* Display the player moves and who won below the scoreboard\n* See this website for what the solution would look like:\n* https://pages.git.generalassemb.ly/fewd201810/class-resources/teacher/11_rps/\n*/\n\nlet yourScore = 0;\nlet computerScore = 0;\n\n// use this function to figure out how the computer plays each move\n// DO NOT EDIT THIS FUNCTION. just use it in the code you right\nfunction getRandomPlay() {\n // define the possible plays in an array of strings\n let plays = ['rock', 'paper', 'scissors'];\n // randomly select one of those options\n let play = plays[Math.floor(Math.random() * plays.length)];\n // return the randomly selected option\n return play;\n}\n\n\n$('#rockButton').click(function(){\n let computerResult = getRandomPlay();\n $('#choice').html(\"The computer plays \"+ computerResult+\" you plays rock\");\n if (computerResult === \"paper\"){\n console.log(\"you lose\");\n computerScore += 1;\n $('#computerScore').html(computerScore);\n $('#status').html(\"you lose\");\n } else if(computerResult === \"scissors\"){\n console.log(\"you win\");\n yourScore += 1;\n $('#humanScore').html(yourScore);\n $('#status').html(\"you win\");\n } else if(computerResult === \"rock\"){\n console.log(\"you tie\");\n $('#humanScore').html(yourScore);\n $('#status').html(\"you tie\");\n }\n })\n\n\n $('#paperButton').click(function(){\n let computerResult = getRandomPlay();\n $('#choice').html(\"The computer plays \"+ computerResult+\" you plays paper\");\n if (computerResult === \"scissors\"){\n console.log(\"you lose\");\n computerScore += 1;\n $('#computerScore').html(computerScore);\n $('#status').html(\"you lose\");\n }else if(computerResult === \"rock\"){\n console.log(\"you win\");\n yourScore += 1;\n $('#humanScore').html(yourScore);\n $('#status').html(\"you win\");\n }else if(computerResult === \"paper\"){\n console.log(\"you tie\");\n $('#humanScore').html(yourScore);\n $('#status').html(\"you tie\");\n }\n })\n\n $('#scissorsButton').click(function(){\n let computerResult = getRandomPlay();\n $('#choice').html(\"The computer plays \"+ computerResult+\" you plays scissors\");\n if (computerResult === \"rock\"){\n console.log(\"you lose\");\n computerScore += 1;\n $('#computerScore').html(computerScore);\n $('#status').html(\"you lose\");\n }else if(computerResult === \"paper\"){\n console.log(\"you win\");\n yourScore += 1;\n $('#humanScore').html(yourScore);\n $('#status').html(\"you win\");\n }else if(computerResult === \"scissors\"){\n console.log(\"you tie\");\n $('#humanScore').html(yourScore);\n $('#status').html(\"you tie\");\n }\n })\n//human play stone\n//cpu: getRandomPlay\n// if (play= paper) print you lose/ and add one to computer score\n//\n"} {"doc_id": "072f2d4ff0b1ae7845b62a4f934dd0bb", "text": "// Promise\n// async code\n\n//Exocution context\n// global -> functions\n\n//global --> this\n\n//Event Queue\n\n// 1. onClick\n// 2. setSitmeOut\n// 3. onHover\n\n// JS runs with 2 phase\n// Phase 1. Creation Phase\n// Pahse 2. Execution Phase\n\n\n// console.log(x);\n \n// var x = 0;\n\n// function x(){\n\n// };\n\n//x();\n\n// foo();\n\n//function declaration\n// function foo(){\n// console.log(\"foo\")\n// }\n\n//bar(); undefined is not a function\n\n//function expression\n// var bar = function(){\n// console.log(\"bar\");\n// }\n\n//----------Promse------------\n\n//1. Pending 2. Rejected 3. Resolved\n\nconst promise = new Promise(function(resolve, reject){\n setTimeout(()=>{\n resolve(\"Sucsess!!\")\n }, 500);\n\n});\n\npromise.then((result)=>{\n console.log(result);\n return \"some other calculated result!\"\n})\n.then(result=>console.log(result))\n.then(result=>{\n let err = new Error();\n err.message = \"error message\";\n throw err;\n})\n.catch( (e)=> {\n console.error(e.message);\n})\n\nfor(let i = 0; i< 10000; i++){\n console.log(i);\n}\n \n"} {"doc_id": "074197b839e4fd801ae10eded846de8a", "text": " //-------------Filter #01---------------//\n// Filtra o array devolvendo OUTRO array com os elementos que tenham as caracter\u00edsticas que procuramos\n// Difrente de 'map' que retorna um array com o mesmo n\u00famero de elementos, por\u00e9m com modifica\u00e7\u00f5es, fun\u00e7\u00e3o 'filter' pode retornar quantidade menor de elementos, sem modifica\u00e7\u00f5es.\n//Tem 4 par\u00e2metros, fun\u00e7\u00e3o callback (value, index, array) e this (que \u00e9 opcional))\n\n \n \n const produtos = [\n { nome: 'Notebook', preco: 2499, fragil: true },\n { nome: 'Ipad Pro', preco: 4199, fragil: true },\n { nome: 'Copo de Vidro', preco: 12.49, fragil: true },\n { nome: 'Copo de Pl\u00e1stico', preco: 18.99, fragil: false }\n ]\n\n console.log(produtos.filter(function(p){ // 'p' primeiro par\u00e2metro 'callback'\n return p.preco >= 500 & p.fragil\n } ))\n/*\n[\n { nome: 'Notebook', preco: 2499, fragil: true },\n { nome: 'Ipad Pro', preco: 4199, fragil: true }\n]\n*/\n\n const prodCaro = produto => produto.preco >= 500\n const prodFragil = produto => produto.fragil \n // repare que por 'fragil' ser uma chave com valor bollean, n\u00e3o necessita de operador.\n\n console.log(produtos.filter(prodCaro).filter(prodFragil))\n /*\n[\n { nome: 'Notebook', preco: 2499, fragil: true },\n { nome: 'Ipad Pro', preco: 4199, fragil: true }\n]\n*/"} {"doc_id": "07c0a0dd0a3d1596999e24afdc5caaef", "text": "// Funciones\n\n// Las funciones son las tareas o sentencias que va a llevar a cabo el navegador. Existen 2 tipos de funciones\n// 1) Declarativas\n// 2) De expresi\u00f3n\n// Ambas pueden llevar par\u00e1metros, que son los datos que necesitan para ejecutarse.\n// Cada par\u00e1metro va separado por una coma.\n// Cada instrucci\u00f3n que tenga la funci\u00f3n debe terminar con ; .\n// Si queremos que una funci\u00f3n nos d\u00e9 un numero o dato tenemos que usar la siguiente sintaxis:\n\n// return El dato que queremos que nos d\u00e9;\n\n// Las funciones declarativas tienen la siguiente sintaxis:\n\n// function Nombre de la funci\u00f3n (Par\u00e1metros de la funci\u00f3n) {Instrucciones}\n\n// Un ejemplo de una funci\u00f3n puede ser una suma:\n\n// _\n// function suma (a,b) {return a+b;}_\n\n// Las funciones de expresi\u00f3n son aquellas que guardamos en una variable, por lo tanto, no es necesario nombrarlas y tienen la siguiente sintaxis:\n\n// _var Nombre de la variable = function(Parametros){Instrucciones}.\n// _\n\n// Un ejemplo de una funci\u00f3n de expresi\u00f3n ser\u00eda:\n\n// var suma = function(a,b){return a+b;}\n\n// Para ejecutar las funciones debemos usar la siguiente sintaxis:\n\n// _Nombre de la funcion(Parametros funci\u00f3n); _\n\n// Si la funci\u00f3n no tiene ning\u00fan par\u00e1metro, \u00fanicamente se escribe:\n\n// _Nombre de la funci\u00f3n(); _\n\n// una funcion que tambien necesita son parametros los que va a utilizar\n//FUNCION DECLARATIVA\n//==================== la funcion va a guardar ciertos valores\nfunction miFuncion(){\n return 3; \n}\nmiFuncion(); //llamar la funcion\n//Ejemplo funcion declarativa\nfunction saludarEstudiantes(estudiante){\n console.log(estudiante);\n}\nsaludarEstudiantes(\"Jimmy\");\n//Ejemplo funcion cadena de texto\nfunction saludarEstudiantes(estudiante){\n console.log(`Hola ${estudiante}`);\n}\nsaludarEstudiantes(\"Jimmy\");\n//FUNCION EXPRESION\n//================== variables tambien puede guardar otro tipo de valores, una varible que almacena una funcion, conocidad tambien como funciones anonimas.\nvar miFuncion = function(a,b){\n return a + b;\n}\nmiFuncion();\n\n//Ejemplo de return\nfunction sumar(a,b){\n var resultado = a + b;\n return resultado;\n}\nsumar (1,2);\n\n// Cuando hablamos de funciones en JavaScript, tenemos dos tipos de funciones: Funciones Declarativas (function declaration / function statement) y Expresiones de funci\u00f3n (function expression / funciones an\u00f3nimas).\n// Funciones Declarativas:\n\n// En las funciones declarativas, utilizamos la palabra reservada function al inicio para poder declarar la funci\u00f3n:\nfunction saludar(nombre) {\n\tconsole.log(`Hola ${nombre}`);\n}\nsaludar('Diego');\n\n// Expresi\u00f3n de funci\u00f3n:\n\n// En la expresi\u00f3n de funci\u00f3n, la declaraci\u00f3n se inicia con la palabra reservada var, donde se generar\u00e1 una variable que guardar\u00e1 una funci\u00f3n an\u00f3nima.\nvar nombre = function(nombre){\n console.log(`Hola ${nombre}`)\n}\nnombre('Diego');\n\n// En la expresi\u00f3n de funci\u00f3n, la funci\u00f3n podr\u00eda o no llevar nombre, aunque es m\u00e1s com\u00fan que se hagan an\u00f3nimas.\n// Diferencias:\n\n// A las funciones declarativas se les aplica hoisting, y a la expresi\u00f3n de funci\u00f3n, no. Ya que el hoisting solo se aplica en las palabras reservadas var y function.\n\n// Lo que quiere decir que con las funciones declarativas, podemos mandar llamar la funci\u00f3n antes de que \u00e9sta sea declarada, y con la expresi\u00f3n de funci\u00f3n, no, tendr\u00edamos que declararla primero, y despu\u00e9s mandarla llamar."} {"doc_id": "0826f7475d512e4ee521e68236ef2764", "text": "// JS Functions creation\n\n// 1.Cryptos Storage\nlet cryptosList = ['crypto 1', 'crypto 2', 'crypto 3'];\nconsole.log('start:', cryptosList); \n\n// 2.Cryptos Display\nfunction cryptosDisplay() {\n console.log(cryptosList);\n}\n\n// 3.Add new cryptos\nfunction addCryptos(crypto) {\n cryptosList.push(crypto);\n cryptosDisplay(); \n}\n\n// 4.Change cryptos\nfunction changeCryptos(index, value) {\n cryptosList[index] = value;\n cryptosDisplay();\n}\n\n// 5.Delete crypto\nfunction deleteCrypto(index) {\n cryptosList.splice(index,1);\n cryptosDisplay();\n}\n// JS objects creation\n\nlet cryptos = {\n // 1. list\n listCrypto: ['crypto 1', 'crypto 2', 'crypto 3'],\n // 2. Display\n displayCryptos: function() {\n console.log(this.listCrypto);\n },\n // 3. Add\n addCryptos: function(crypto) {\n this.listCrypto.push(crypto);\n this.displayCryptos();\n },\n\n // 4. Change\n changeCryptos: function(index, value) {\n this.listCrypto[index] = value;\n this.displayCryptos();\n },\n\n //5. Delete\n deleteCrypto: function(index) {\n this.listCrypto.splice(index,1);\n this.displayCryptos();\n }\n\n};\n\nconsole.log('cryptos object: ', cryptos);"} {"doc_id": "091185ad66f847981340d7945af60fe9", "text": "\"use strict\";\r\n/*\r\nrunning lite-srver\r\n*/\r\n// console.log(\"This is a ts file\");\r\n/*\r\nadding types to simple functions\r\n*/\r\n// {\r\n// function add(num1: number, num2: number): number {\r\n// \treturn num1 + num2;\r\n// };\r\n// const num1 = 20,\r\n// \tnum2 = 8;\r\n// const result = add(num1, num2);\r\n// console.log(result);\r\n// }\r\n/*\r\nTypes (Number, Boolean, String...)\r\n*/\r\n// {\r\n// \tfunction add(num1: number, num2: number, showresult: boolean, phrase: string): number | void {\r\n// \t\tif (showresult) {\r\n// \t\t\tconsole.log(phrase, num1 + num2);\r\n// \t\t} else {\r\n// \t\t\treturn num1 + num2;\r\n// \t\t}\r\n// \t};\r\n// \tconst num1 = 20,\r\n// \t\tnum2 = 8,\r\n// \t\tprintResult = true,\r\n// \t\tresultPhrase = \"The result is \";\r\n// \tadd(num1, num2, printResult, resultPhrase);\r\n// \t/*\r\n// \t\tObject\r\n// \t*/\r\n// \t{\r\n// \t\t//not recomended\r\n// \t\tconst person: {\r\n// \t\t\tname: string;\r\n// \t\t\tage: number;\r\n// \t\t} = {\r\n// \t\t\tname: \"Paddington\",\r\n// \t\t\tage: 23\r\n// \t\t};\r\n// \t\tconsole.log(\"Object\", person.name);\r\n// \t\t//recomended\r\n// \t\tconst person1 = {\r\n// \t\t\tname: \"Nick\",\r\n// \t\t\tage: 23\r\n// \t\t};\r\n// \t\tconsole.log(\"Object\", person1.name);\r\n// \t}\r\n// \t/*\r\n// \tArrays\r\n// \t*/\r\n// \t{\r\n// \t\tconst person3 = {\r\n// \t\t\tname: \"Nick\",\r\n// \t\t\tage: 23,\r\n// \t\t\thobbies: [\"sports\", \"cooking\"]\r\n// \t\t};\r\n// \t\tlet favActivities: string[];\r\n// \t\tfavActivities = [\"swimming\"];\r\n// \t\tconsole.log(\"Arrys\", person3.hobbies, favActivities);\r\n// \t\t//allows explicit functions to run as it knows the type of varible\r\n// \t\tfor (const hobby of person3.hobbies) {\r\n// \t\t\tconsole.log(\"Arrys\", hobby.toUpperCase());\r\n// \t\t}\r\n// \t}\r\n// \t/*\r\n// \tTuple fixed length, fixed type arrrays (does not exist in JS but does in some other programming languages)\r\n// \t*/\r\n// \t{\r\n// \t\t//ts assumes it as this and allows the following modifications which aere not acceptable\r\n// \t\tconst person4: {\r\n// \t\t\tname: string;\r\n// \t\t\tage: number;\r\n// \t\t\thobbies: string[];\r\n// \t\t\trole: (number | string)[]\r\n// \t\t} = {\r\n// \t\t\tname: \"Nick\",\r\n// \t\t\tage: 23,\r\n// \t\t\thobbies: [\"sports\", \"cooking\"],\r\n// \t\t\trole: [2, 'author']\r\n// \t\t};\r\n// \t\tperson4.role.push(\"admin\"); //still works but not ideal\r\n// \t\tperson4.role[1] = (1); //still works but not ideal\r\n// \t\tconsole.log(\"Tuple\", person4);\r\n// \t\t//ts assumes it as this and allows the following modifications which aere not acceptable\r\n// \t\tconst person5: {\r\n// \t\t\tname: string;\r\n// \t\t\tage: number;\r\n// \t\t\thobbies: string[];\r\n// \t\t\trole: [number, string] //tells ts that first element is a number and second is a string\r\n// \t\t} = {\r\n// \t\t\tname: \"Nick\",\r\n// \t\t\tage: 23,\r\n// \t\t\thobbies: [\"sports\", \"cooking\"],\r\n// \t\t\trole: [2, 'author']\r\n// \t\t};\r\n// \t\tperson5.role.push(\"admin\"); //still works but not ideal as push works in tupleand can push any value\r\n// \t\t// person5.role[1] = (1); //now throws an error\r\n// \t\tconsole.log(\"Tuple\", person5)\r\n// \t}\r\n// \t/*\r\n// \tEnum exists in other programming languages but not in JS\r\n// \t*/\r\n// \t{\r\n// \t\t// default enum\r\n// \t\tenum Roles {\r\n// \t\t\tADMIN, READ_ONLY, AUTHOR\r\n// \t\t}; //assigns 0, 1, 2 as keys to the enum\r\n// \t\tconst person6 = {\r\n// \t\t\tname: \"Nick\",\r\n// \t\t\tage: 23,\r\n// \t\t\thobbies: [\"sports\", \"cooking\"],\r\n// \t\t\trole: Roles.ADMIN\r\n// \t\t};\r\n// \t\tconsole.log(\"Enum\", person6)\r\n// \t\tif (person6.role === Roles.AUTHOR) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is Author\");\r\n// \t\t} else if (person6.role === Roles.ADMIN) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is Admin\");\r\n// \t\t} else if (person6.role === Roles.READ_ONLY) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is readOnly\");\r\n// \t\t}\r\n// \t\t// custom enum\r\n// \t\tenum Roles1 {\r\n// \t\t\tADMIN = 5, READ_ONLY, AUTHOR\r\n// \t\t}; //assigns 5, 6, 7 as keys to the enum\r\n// \t\tconst person7 = {\r\n// \t\t\tname: \"Nick\",\r\n// \t\t\tage: 23,\r\n// \t\t\thobbies: [\"sports\", \"cooking\"],\r\n// \t\t\trole: Roles1.ADMIN\r\n// \t\t};\r\n// \t\tconsole.log(\"Enum\", person7)\r\n// \t\tif (person7.role === Roles1.AUTHOR) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is Author\");\r\n// \t\t} else if (person7.role === Roles1.ADMIN) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is Admin\");\r\n// \t\t} else if (person7.role === Roles1.READ_ONLY) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is readOnly\");\r\n// \t\t}\r\n// \t\t// custom enum\r\n// \t\tenum Roles2 {\r\n// \t\t\tADMIN = \"Administrator\",\r\n// \t\t\tREAD_ONLY = 6,\r\n// \t\t\tAUTHOR = 100\r\n// \t\t}; //assigns 5, 6, 7 as keys to the enum\r\n// \t\tconst person8 = {\r\n// \t\t\tname: \"Nick\",\r\n// \t\t\tage: 23,\r\n// \t\t\thobbies: [\"sports\", \"cooking\"],\r\n// \t\t\trole: Roles2.ADMIN\r\n// \t\t};\r\n// \t\tconsole.log(\"Enum\", person8)\r\n// \t\tif (person8.role === Roles2.AUTHOR) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is Author\");\r\n// \t\t} else if (person8.role === Roles2.ADMIN) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is Admin\");\r\n// \t\t} else if (person8.role === Roles2.READ_ONLY) {\r\n// \t\t\tconsole.log(\"Enum\", \"role is readOnly\");\r\n// \t\t}\r\n// \t}\r\n// \t/*\r\n// \tAny is the most flexible as it does not tell ts anything and allow any type for the variable\r\n// \tnot recomended to use as its same to vanila js and does not do an ts checks\r\n// \t*/\r\n// }\r\n/*\r\n Exploring union type\r\n*/\r\n// {\r\n// \t//should work with numbers and strings\r\n// \tfunction combnine(num1: number | string,\r\n// \t\tnum2: number | string,\r\n// \t\tresultType: string): number | string {\r\n// \t\tlet result: number | string;\r\n// \t\t//sometines runtime checks might be required when using union types as ts does not know about the exact type of input from the union\r\n// \t\tif (typeof num1 === \"number\" && typeof num2 === \"number\") {\r\n// \t\t\tresult = num1 + num2;\r\n// \t\t} else {\r\n// \t\t\tresult = num1.toString() + num2.toString();\r\n// \t\t}\r\n// \t\tif (resultType === \"as-string\") {\r\n// \t\t\treturn result.toString();\r\n// \t\t} else {\r\n// \t\t\treturn +result;\r\n// \t\t}\r\n// \t};\r\n// \tconst combineAges = combnine(\"12\", \"23\", \"as-number\");\r\n// \tconsole.log(\"union type\", combineAges);\r\n// \tconst combinestr = combnine(12, 23, \"as-string\");\r\n// \tconsole.log(\"union type\", combinestr);\r\n// \tconst combinestr2 = combnine(\"Amit\", \"23\", \"as-string\");\r\n// \tconsole.log(\"union type\", combinestr2);\r\n// \t//literal Strings\r\n// \t//does not allow any other value other than as-number or as-string for resultType\r\n// \tfunction combnine1(num1: number | string,\r\n// \t\tnum2: number | string,\r\n// \t\tresultType: \"as-number\" | \"as-string\"): number | string {\r\n// \t\tlet result1: number | string;\r\n// \t\t//sometines runtime checks might be required when using union types as ts does not know about the exact type of input from the union\r\n// \t\tif (typeof num1 === \"number\" && typeof num2 === \"number\") {\r\n// \t\t\tresult1 = num1 + num2;\r\n// \t\t} else {\r\n// \t\t\tresult1 = num1.toString() + num2.toString();\r\n// \t\t}\r\n// \t\tif (resultType === \"as-string\") {\r\n// \t\t\treturn result1.toString();\r\n// \t\t} else {\r\n// \t\t\treturn +result1;\r\n// \t\t}\r\n// \t};\r\n// \tconst combineAges1 = combnine1(\"12\", \"23\", \"as-number\");\r\n// \tconsole.log(\"literal type\", combineAges1);\r\n// \tconst combinestr3 = combnine1(12, 23, \"as-string\");\r\n// \tconsole.log(\"literal type\", combinestr3);\r\n// \tconst combinestr4 = combnine1(\"Amit\", \"23\", \"as-string\");\r\n// \tconsole.log(\"literal type\", combinestr4);\r\n// \t//type alias\r\n// \ttype Combinable = number | string\r\n// \tfunction combnine2(num1: Combinable,\r\n// \t\tnum2: Combinable,\r\n// \t\tresultType: \"as-number\" | \"as-string\"): Combinable {\r\n// \t\tlet result1: Combinable;\r\n// \t\t//sometines runtime checks might be required when using union types as ts does not know about the exact type of input from the union\r\n// \t\tif (typeof num1 === \"number\" && typeof num2 === \"number\") {\r\n// \t\t\tresult1 = num1 + num2;\r\n// \t\t} else {\r\n// \t\t\tresult1 = num1.toString() + num2.toString();\r\n// \t\t}\r\n// \t\tif (resultType === \"as-string\") {\r\n// \t\t\treturn result1.toString();\r\n// \t\t} else {\r\n// \t\t\treturn +result1;\r\n// \t\t}\r\n// \t};\r\n// \tconst combineAges2 = combnine2(\"12\", \"23\", \"as-number\");\r\n// \tconsole.log(\"alias type\", combineAges2);\r\n// \tconst combinestr1 = combnine2(12, 23, \"as-string\");\r\n// \tconsole.log(\"alias type\", combinestr1);\r\n// \tconst combinestr5 = combnine2(\"Amit\", \"23\", \"as-string\");\r\n// \tconsole.log(\"alias type\", combinestr5);\r\n// }\r\n/*\r\n Function type\r\n*/\r\n// {\r\n// \t//declaring a variable as function and using it\r\n// \tfunction add1(num1: number, num2: number): number {\r\n// \t\treturn num1 + num2;\r\n// \t};\r\n// \tfunction printResult(num: number): void {\r\n// \t\tconsole.log(\"Result\", num);\r\n// \t}\r\n// \tprintResult1(add1(4, 2));\r\n// \tlet combineValues: Function;\r\n// \tcombineValues = add1;\r\n// \t// combineValues = 5;// would have been possible if combineValue was not typecasted as a function and would have resulted in a run time error\r\n// \tconsole.log(\"function Types\", combineValues(2, 3));\r\n// \t//declaring function types with a return type\r\n// \tfunction add2(num1: number, num2: number): number {\r\n// \t\treturn num1 + num2;\r\n// \t};\r\n// \tfunction printResult1(num: number): void {\r\n// \t\tconsole.log(\"Result\", num);\r\n// \t}\r\n// \tprintResult1(add2(4, 2));\r\n// \tlet combineValues1: (a: number, b: number) => number;\r\n// \tcombineValues1 = add2;\r\n// \t// combineValues1 = 5;// thros error as a number cannot be assigned to a function type\r\n// \tconsole.log(\"function Types\", combineValues1(2, 3));\r\n//function type and callback\r\n// function add3(num1: number, num2: number): number {\r\n// \treturn num1 + num2;\r\n// };\r\n// function printResult2(num: number): void {\r\n// \tconsole.log(\"Result\", num);\r\n// }\r\n// function addAndHandle(num1: number, num2: number, callbackFunc: (num: number) => void) {\r\n// \tconst result = num1 + num2;\r\n// \tcallbackFunc(result);\r\n// }\r\n// printResult2(add3(4, 2));\r\n// let combineValues2: (a: number, b: number) => number;\r\n// combineValues2 = add3;\r\n// // combineValues1 = 5;// thros error as a number cannot be assigned to a function type\r\n// console.log(\"function callback\", combineValues2(2, 3));\r\n// addAndHandle(10, 20, num => { console.log(num); })\r\n// }\r\n/*\r\ntype Unknown\r\n*/\r\n// {\r\n// \tlet userInput: unknown,\r\n// \t\tuserName: string;\r\n// \tuserInput = 5;\r\n// \tuserInput = \"max\";\r\n// \t// userName = userInput;//throws an error as unknown can be of any type\r\n// \tif (typeof userInput === \"string\") {\r\n// \t\tuserName = userInput;\r\n// \t}\r\n// }\r\n/*\r\nnever type\r\n*/\r\n// {\r\n// \t//the below function never returns an error. It either crases the app or goes to catch\r\n// \t//void is usually assumed as never in older code\r\n// \t//newer code shoud use never if a code never returns anything\r\n// \tfunction generateErr(message: string, code: number): never {\r\n// \t\tthrow { message: message, errorCode: code };\r\n// \t}\r\n// \tgenerateErr(\"unknown type of variable nice\", 301);\r\n// }\r\n"} {"doc_id": "094210a66b7fdc5bb7392027ed790243", "text": "/**\n * Created by thanos on 4/8/16.\n */\n//\u521b\u5efa\u6570\u7ec4(creating an array)\nvar arr = new Array(element0, element1, elementN);\nvar arr = Array(elechuangjment0, element1, elementN);\nvar arr = [element0, element1, elementN];\n// \u8bd1\u8005\u6ce8: var arr=[4] \u548c var arr=new Array(4)\u662f\u4e0d\u7b49\u6548\u7684\uff0c\n// \u540e\u80054\u6307\u6570\u7ec4\u957f\u5ea6\uff0c\u6240\u4ee5\u4f7f\u7528\u5b57\u9762\u503c(literal)\u7684\u65b9\u5f0f\u5e94\u8be5\u4e0d\u4ec5\u4ec5\u662f\u4fbf\u6377\uff0c\u540c\u65f6\u4e5f\u4e0d\u6613\u8e29\u5751\n\n//forEach() \u65b9\u6cd5\u63d0\u4f9b\u4e86\u904d\u5386\u6570\u7ec4\u5143\u7d20\u7684\u5176\u4ed6\u65b9\u6cd5\nvar colors = ['red', 'green', 'blue'];\ncolors.forEach(function(color) {\n console.log(color);\n});\n\n//--------------------------------------------------------\n//\u6570\u7ec4\u7684\u65b9\u6cd5(array methods)\n//concat() \u8fde\u63a5\u4e24\u4e2a\u6570\u7ec4\u5e76\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002\nvar myArray = new Array(\"1\", \"2\", \"3\");\nmyArray = myArray.concat(\"a\", \"b\", \"c\");\n// myArray is now [\"1\", \"2\", \"3\", \"a\", \"b\", \"c\"]\n\n//join(\u5206\u9694\u7b26 = ',') \u5c06\u6570\u7ec4\u7684\u6240\u6709\u5143\u7d20\u8fde\u63a5\u6210\u4e00\u4e2a\u5b57\u7b26\u4e32\nvar myArray = new Array(\"Wind\", \"Rain\", \"Fire\");\nvar list = myArray.join(\" - \"); // list is \"Wind - Rain - Fire\"\n\n//push() \u5728\u6570\u7ec4\u672b\u5c3e\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u6570\u7ec4\u64cd\u4f5c\u540e\u7684\u957f\u5ea6\u3002\nvar myArray = new Array(\"1\", \"2\");\nmyArray.push(\"3\"); // myArray is now [\"1\", \"2\", \"3\"]\n\n//pop() \u4ece\u6570\u7ec4\u79fb\u51fa\u6700\u540e\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\nvar myArray = new Array(\"1\", \"2\", \"3\");\nvar last = myArray.pop();\n// myArray is now [\"1\", \"2\"], last = \"3\"\n\n//shift() \u4ece\u6570\u7ec4\u79fb\u51fa\u7b2c\u4e00\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u8be5\u5143\u7d20\u3002\nvar myArray = new Array (\"1\", \"2\", \"3\");\nvar first = myArray.shift();\n// myArray is now [\"2\", \"3\"], first is \"1\"\n\n//unshift() \u5728\u6570\u7ec4\u5f00\u5934\u6dfb\u52a0\u4e00\u4e2a\u6216\u591a\u4e2a\u5143\u7d20\uff0c\u5e76\u8fd4\u56de\u6570\u7ec4\u7684\u65b0\u957f\u5ea6\u3002\nvar myArray = new Array (\"1\", \"2\", \"3\");\nmyArray.unshift(\"4\", \"5\");\n// myArray becomes [\"4\", \"5\", \"1\", \"2\", \"3\"]\n\n//slice(start_index, upto_index) \u4ece\u6570\u7ec4\u63d0\u53d6\u4e00\u4e2a\u7247\u6bb5\uff0c\u5e76\u4f5c\u4e3a\u4e00\u4e2a\u65b0\u6570\u7ec4\u8fd4\u56de\u3002\nvar myArray = new Array (\"a\", \"b\", \"c\", \"d\", \"e\");\nmyArray = myArray.slice(1, 4);\n// starts at index 1 and extracts all elements until index 3, returning [ \"b\", \"c\", \"d\"]\n\n//splice(index, count_to_remove, addElement1, addElement2, ...)\u4ece\u6570\u7ec4\u79fb\u51fa\u4e00\u4e9b\u5143\u7d20\uff0c\uff08\u53ef\u9009\uff09\u5e76\u66ff\u6362\u5b83\u4eec\u3002\nvar myArray = new Array (\"1\", \"2\", \"3\", \"4\", \"5\");\nmyArray.splice(1, 3, \"a\", \"b\", \"c\", \"d\");\n\n//reverse() \u98a0\u5012\u6570\u7ec4\u5143\u7d20\u7684\u987a\u5e8f\uff1a\u7b2c\u4e00\u4e2a\u53d8\u6210\u6700\u540e\u4e00\u4e2a\uff0c\u6700\u540e\u4e00\u4e2a\u53d8\u6210\u7b2c\u4e00\u4e2a\u3002\nvar myArray = new Array (\"1\", \"2\", \"3\");\nmyArray.reverse();\n// transposes the array so that myArray = [ \"3\", \"2\", \"1\" ]\n\n//sort() \u7ed9\u6570\u7ec4\u5143\u7d20\u6392\u5e8f\u3002\nvar myArray = new Array(\"Wind\", \"Rain\", \"Fire\");\nmyArray.sort();\n// sorts the array so that myArray = [ \"Fire\", \"Rain\", \"Wind\" ]\n\n//indexOf(searchElement[, fromIndex]) \u5728\u6570\u7ec4\u4e2d\u641c\u7d22searchElement \u5e76\u8fd4\u56de\u7b2c\u4e00\u4e2a\u5339\u914d\u7684\u7d22\u5f15\u3002\nvar a = ['a', 'b', 'a', 'b', 'a'];\nconsole.log(a.indexOf('b')); // logs 1\n// Now try again, starting from after the last match\nconsole.log(a.indexOf('b', 2)); // logs 3\nconsole.log(a.indexOf('z')); // logs -1, because 'z' was not found\n\n//lastIndexOf(searchElement[, fromIndex]) \u548c indexOf\u5dee\u4e0d\u591a\uff0c\u4f46\u662f\u662f\u4ece\u7ed3\u5c3e\u5f00\u59cb\uff0c\u5e76\u4e14\u662f\u53cd\u5411\u641c\u7d22\u3002\nvar a = ['a', 'b', 'c', 'd', 'a', 'b'];\nconsole.log(a.lastIndexOf('b')); // logs 5\n// Now try again, starting from before the last match\nconsole.log(a.lastIndexOf('b', 4)); // logs 1\nconsole.log(a.lastIndexOf('z')); // logs -1\n\n//forEach(callback[, thisObject]) \u5728\u6570\u7ec4\u6bcf\u4e2a\u5143\u7d20\u9879\u4e0a\u6267\u884ccallback\u3002\n\n//map(callback[, thisObject]) \u5728\u6570\u7ec4\u7684\u6bcf\u4e2a\u5355\u5143\u9879\u4e0a\u6267\u884ccallback\u51fd\u6570\uff0c\u5e76\u628a\u8fd4\u56de\u5305\u542b\u56de\u8c03\u51fd\u6570\u8fd4\u56de\u503c\u7684\u65b0\u6570\u7ec4\u3002\n// \u8bd1\u8005\u6ce8\uff1a\u4e5f\u5c31\u662f\u904d\u5386\u6570\u7ec4\uff0c\u5e76\u901a\u8fc7callback\u5bf9\u6570\u7ec4\u5143\u7d20\u8fdb\u884c\u64cd\u4f5c\uff0c\u5e76\u5c06\u6240\u6709\u64cd\u4f5c\u7ed3\u679c\u653e\u5165\u6570\u7ec4\u4e2d\u5e76\u8fd4\u56de\u8be5\u6570\u7ec4\u3002\n\n//filter(callback[, thisObject]) \u8fd4\u56de\u4e00\u4e2a\u5305\u542b\u6240\u6709\u5728\u56de\u8c03\u51fd\u6570\u4e0a\u8fd4\u56de\u4e3atrue\u7684\u5143\u7d20\u7684\u65b0\u6570\u7ec4\u3002\n// \u8bd1\u8005\u6ce8\uff1acallback\u5728\u8fd9\u91cc\u62c5\u4efb\u7684\u662f\u8fc7\u6ee4\u5668\u7684\u89d2\u8272\uff0c\u5f53\u5143\u7d20\u7b26\u5408\u6761\u4ef6\uff0c\u8fc7\u6ee4\u5668\u5c31\u8fd4\u56detrue\uff0c\u800cfilter\u5219\u4f1a\u8fd4\u56de\u6240\u6709\u7b26\u5408\u8fc7\u6ee4\u6761\u4ef6\u7684\u5143\u7d20\u3002\n\n//every(callback[, thisObject]) \u5f53\u6570\u7ec4\u4e2d\u6bcf\u4e00\u4e2a\u5143\u7d20\u5728callback\u4e0a\u88ab\u8fd4\u56detrue\u65f6\u5c31\u8fd4\u56detrue\u3002\n// \u8bd1\u8005\u6ce8\uff1a\u540c\u4e0a\uff0cevery\u5176\u5b9e\u7c7b\u4f3cfilter\uff0c\u53ea\u4e0d\u8fc7\u5b83\u7684\u529f\u80fd\u662f\u5224\u65ad\u662f\u4e0d\u662f\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5143\u7d20\u90fd\u7b26\u5408\u6761\u4ef6\uff0c\u5e76\u4e14\u8fd4\u56de\u7684\u662fbool\u503c\u3002\n\n//some(callback[, thisObject]) \u53ea\u8981\u6570\u7ec4\u4e2d\u6709\u4e00\u9879\u5728callback\u4e0a\u88ab\u8fd4\u56detrue\uff0c\u5c31\u8fd4\u56detrue\u3002\n// \u8bd1\u8005\u6ce8\uff1a\u540c\u4e0a\uff0c\u7c7b\u4f3cevery\uff0c\u4e0d\u8fc7\u524d\u8005\u8981\u6c42\u90fd\u7b26\u5408\u7b5b\u9009\u6761\u4ef6\u624d\u8fd4\u56detrue\uff0c\u540e\u8005\u53ea\u8981\u6709\u7b26\u5408\u6761\u4ef6\u7684\u5c31\u8fd4\u56detrue\u3002"} {"doc_id": "0960c691348486f74db09b0d80f85fb1", "text": "var numeroPremiado = parseInt(Math.random() * (10 - 0) + 0);\nvar tentativas = 3;\nwhile (tentativas > 0) {\nvar entradaUser = parseInt(prompt(\"Digite um n\u00famero:\"));\n \n if (numeroPremiado == entradaUser) {\n alert(\"Voc\u00ea acertou\");\n break\n }\n else if (numeroPremiado > entradaUser){\n alert(\"O n\u00famero premiado \u00e9 maior\");\n tentativas = tentativas - 1;\n }\n else if (numeroPremiado < entradaUser){\n alert(\"O n\u00famero premiado \u00e9 menor\");\n tentativas = tentativas - 1;\n } \n}\nif( numeroPremiado != entradaUser ) {\n alert(\"Voc\u00ea j\u00e1 utilizou as 3 tentativas\");\n }"} {"doc_id": "0a1451bf99c83294d982a54f8e7d06ba", "text": "var Rectangle = (function () {\n function Rectangle(width, height) {\n this.width = width;\n this.height = height;\n }\n ;\n Rectangle.prototype.compareTo = function (value) {\n if (this.width * this.height >= value.width * value.height) {\n return 1;\n }\n else {\n return -1;\n }\n };\n return Rectangle;\n}());\nvar rect1 = new Rectangle(2, 5);\nvar rect2 = new Rectangle(2, 3);\nrect1.compareTo(rect2) === 1 ? console.log(\"rect1 is bigger\") :\n console.log(\"rect1 is smaller\");\nvar Programmer = (function () {\n function Programmer(name, salary) {\n this.name = name;\n this.salary = salary;\n }\n ;\n Programmer.prototype.compareTo = function (value) {\n if (this.salary >= value.salary) {\n return 1;\n }\n else {\n return -1;\n }\n };\n return Programmer;\n}());\nvar prog1 = new Programmer(\"John\", 20000);\nvar prog2 = new Programmer(\"Alex\", 30000);\nprog1.compareTo(prog2) === 1 ? console.log(prog1.name + \" is richer\") :\n console.log(prog1.name + \" is poorer\");\n"} {"doc_id": "0aa4211e39fff5bfb491baa2988b5e24", "text": "/*\n===============\nDeny dan Mainan\n===============\n\n[INSTRUCTION]\nDeny dan Jenny sangat bahagia setelah kelahiran anak pertama. Anak mereka sangat\nmenyukai mainan, jadi Deny ingin membeli beberapa. Ada banyak pilihan mainan\nyang dapat dibeli.\nFunction maximum toys menerima 2 parameter array dan number, array merupakan daftar harga mainan dan number merupakan uang yang dimiliki Deny.\n\n[EXAMPLE]\nBila input harga array adalah [1, 12, 5, 111, 200, 1000, 10] dan uang k = 50\nmaka hasilnya adalah 4, karena jumlah mainan yang dapat dibeli terbanyak adalah mainan\ndengan harga 1,12,5,10.\n\n[RULE]\n1. Wajib menulis pseudo code, bila tidak menulis pseudo code maka dianggap tidak\nmengerjakan.\n2. dilarang menggunakan indexOf(), find(), filter() dan sort()\n3. dilarang menggunakan regex\n*/\n\n/*\n Pseudo code disini\n\n */\n\nfunction maximumToys(prices, k) {\n //code disini\n}\n\nconsole.log(maximumToys([3, 7, 2, 9, 4], 15)); // 3\nconsole.log(maximumToys([1, 12, 5, 111, 200, 1000, 10], 50)); // 4\nconsole.log(maximumToys([1, 2, 3, 4], 7)); // 3\nconsole.log(\nmaximumToys(\n [\n 52952808,\n 39586066,\n 70403274,\n 33392541,\n 37992362,\n 55743111,\n 55380991,\n 48022854,\n 54843595,\n 440\n ],\n 100000\n)\n); // 1"} {"doc_id": "0b20ed0f521a1a9f86a2a5e88df37781", "text": "/*\nGiven a string, find the length of the longest substring without repeating characters.
\n\nExamples:
\n\nGiven \"abcabcbb\", the answer is \"abc\", which the length is 3.
Given \"bbbbb\", the answer is \"b\", with the length of 1.
Given \"pwwkew\", the answer is \"wke\", with the length of 3. Note that the answer must be a substring, \"pwke\" is a subsequence and not a substring.
\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u627e\u51fa\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u7684\u957f\u5ea6\u3002
\n\n\u793a\u4f8b\uff1a
\n\n\u7ed9\u5b9a "abcabcbb" \uff0c\u6ca1\u6709\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f "abc" \uff0c\u90a3\u4e48\u957f\u5ea6\u5c31\u662f3\u3002
\u7ed9\u5b9a "bbbbb" \uff0c\u6700\u957f\u7684\u5b50\u4e32\u5c31\u662f "b" \uff0c\u957f\u5ea6\u662f1\u3002
\u7ed9\u5b9a "pwwkew" \uff0c\u6700\u957f\u5b50\u4e32\u662f "wke" \uff0c\u957f\u5ea6\u662f3\u3002\u8bf7\u6ce8\u610f\u7b54\u6848\u5fc5\u987b\u662f\u4e00\u4e2a\u5b50\u4e32\uff0c"pwke" \u662f \u5b50\u5e8f\u5217 \u800c\u4e0d\u662f\u5b50\u4e32\u3002
\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u627e\u51fa\u4e0d\u542b\u6709\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u7684\u957f\u5ea6\u3002
\n\n\u793a\u4f8b\uff1a
\n\n\u7ed9\u5b9a "abcabcbb" \uff0c\u6ca1\u6709\u91cd\u590d\u5b57\u7b26\u7684\u6700\u957f\u5b50\u4e32\u662f "abc" \uff0c\u90a3\u4e48\u957f\u5ea6\u5c31\u662f3\u3002
\u7ed9\u5b9a "bbbbb" \uff0c\u6700\u957f\u7684\u5b50\u4e32\u5c31\u662f "b" \uff0c\u957f\u5ea6\u662f1\u3002
\u7ed9\u5b9a "pwwkew" \uff0c\u6700\u957f\u5b50\u4e32\u662f "wke" \uff0c\u957f\u5ea6\u662f3\u3002\u8bf7\u6ce8\u610f\u7b54\u6848\u5fc5\u987b\u662f\u4e00\u4e2a\u5b50\u4e32\uff0c"pwke" \u662f \u5b50\u5e8f\u5217 \u800c\u4e0d\u662f\u5b50\u4e32\u3002
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.
\nSymbol Value\nI 1\nV 5\nX 10\nL 50\nC 100\nD 500\nM 1000\n\n
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.
Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:
I can be placed before V (5) and X (10) to make 4 and 9. X can be placed before L (50) and C (100) to make 40 and 90. C can be placed before D (500) and M (1000) to make 400 and 900.Given a roman numeral, convert it to an integer. Input is guaranteed to be within the range from 1 to 3999.
\n\nExample 1:
\n\n\nInput: "III"\nOutput: 3\n\n
Example 2:
\n\n\nInput: "IV"\nOutput: 4\n\n
Example 3:
\n\n\nInput: "IX"\nOutput: 9\n\n
Example 4:
\n\n\nInput: "LVIII"\nOutput: 58\nExplanation: C = 100, L = 50, XXX = 30 and III = 3.\n\n\n
Example 5:
\n\n\nInput: "MCMXCIV"\nOutput: 1994\nExplanation: M = 1000, CM = 900, XC = 90 and IV = 4.\n
\u7f57\u9a6c\u6570\u5b57\u5305\u542b\u4ee5\u4e0b\u4e03\u79cd\u5b57\u7b26\uff1aI\uff0c V\uff0c X\uff0c L\uff0cC\uff0cD \u548c M\u3002
\u5b57\u7b26 \u6570\u503c\nI 1\nV 5\nX 10\nL 50\nC 100\nD 500\nM 1000\n\n
\u4f8b\u5982\uff0c \u7f57\u9a6c\u6570\u5b57 2 \u5199\u505a II \uff0c\u5373\u4e3a\u4e24\u4e2a\u5e76\u5217\u7684 1\u300212 \u5199\u505a XII \uff0c\u5373\u4e3a X + II \u3002 27 \u5199\u505a XXVII, \u5373\u4e3a XX + V + II \u3002
\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7f57\u9a6c\u6570\u5b57\u4e2d\u5c0f\u7684\u6570\u5b57\u5728\u5927\u7684\u6570\u5b57\u7684\u53f3\u8fb9\u3002\u4f46\u4e5f\u5b58\u5728\u7279\u4f8b\uff0c\u4f8b\u5982 4 \u4e0d\u5199\u505a IIII\uff0c\u800c\u662f IV\u3002\u6570\u5b57 1 \u5728\u6570\u5b57 5 \u7684\u5de6\u8fb9\uff0c\u6240\u8868\u793a\u7684\u6570\u7b49\u4e8e\u5927\u6570 5 \u51cf\u5c0f\u6570 1 \u5f97\u5230\u7684\u6570\u503c 4 \u3002\u540c\u6837\u5730\uff0c\u6570\u5b57 9 \u8868\u793a\u4e3a IX\u3002\u8fd9\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\u53ea\u9002\u7528\u4e8e\u4ee5\u4e0b\u516d\u79cd\u60c5\u51b5\uff1a
I \u53ef\u4ee5\u653e\u5728 V (5) \u548c X (10) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 4 \u548c 9\u3002X \u53ef\u4ee5\u653e\u5728 L (50) \u548c C (100) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 40 \u548c 90\u3002 C \u53ef\u4ee5\u653e\u5728 D (500) \u548c M (1000) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 400 \u548c 900\u3002\u7ed9\u5b9a\u4e00\u4e2a\u7f57\u9a6c\u6570\u5b57\uff0c\u5c06\u5176\u8f6c\u6362\u6210\u6574\u6570\u3002\u8f93\u5165\u786e\u4fdd\u5728 1 \u5230 3999 \u7684\u8303\u56f4\u5185\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: "III"\n\u8f93\u51fa: 3\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: "IV"\n\u8f93\u51fa: 4\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: "IX"\n\u8f93\u51fa: 9\n\n
\u793a\u4f8b 4:
\n\n\u8f93\u5165: "LVIII"\n\u8f93\u51fa: 58\n\u89e3\u91ca: C = 100, L = 50, XXX = 30, III = 3.\n\n\n
\u793a\u4f8b 5:
\n\n\u8f93\u5165: "MCMXCIV"\n\u8f93\u51fa: 1994\n\u89e3\u91ca: M = 1000, CM = 900, XC = 90, IV = 4.\n
\u7f57\u9a6c\u6570\u5b57\u5305\u542b\u4ee5\u4e0b\u4e03\u79cd\u5b57\u7b26\uff1aI\uff0c V\uff0c X\uff0c L\uff0cC\uff0cD \u548c M\u3002
\u5b57\u7b26 \u6570\u503c\nI 1\nV 5\nX 10\nL 50\nC 100\nD 500\nM 1000\n\n
\u4f8b\u5982\uff0c \u7f57\u9a6c\u6570\u5b57 2 \u5199\u505a II \uff0c\u5373\u4e3a\u4e24\u4e2a\u5e76\u5217\u7684 1\u300212 \u5199\u505a XII \uff0c\u5373\u4e3a X + II \u3002 27 \u5199\u505a XXVII, \u5373\u4e3a XX + V + II \u3002
\u901a\u5e38\u60c5\u51b5\u4e0b\uff0c\u7f57\u9a6c\u6570\u5b57\u4e2d\u5c0f\u7684\u6570\u5b57\u5728\u5927\u7684\u6570\u5b57\u7684\u53f3\u8fb9\u3002\u4f46\u4e5f\u5b58\u5728\u7279\u4f8b\uff0c\u4f8b\u5982 4 \u4e0d\u5199\u505a IIII\uff0c\u800c\u662f IV\u3002\u6570\u5b57 1 \u5728\u6570\u5b57 5 \u7684\u5de6\u8fb9\uff0c\u6240\u8868\u793a\u7684\u6570\u7b49\u4e8e\u5927\u6570 5 \u51cf\u5c0f\u6570 1 \u5f97\u5230\u7684\u6570\u503c 4 \u3002\u540c\u6837\u5730\uff0c\u6570\u5b57 9 \u8868\u793a\u4e3a IX\u3002\u8fd9\u4e2a\u7279\u6b8a\u7684\u89c4\u5219\u53ea\u9002\u7528\u4e8e\u4ee5\u4e0b\u516d\u79cd\u60c5\u51b5\uff1a
I \u53ef\u4ee5\u653e\u5728 V (5) \u548c X (10) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 4 \u548c 9\u3002X \u53ef\u4ee5\u653e\u5728 L (50) \u548c C (100) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 40 \u548c 90\u3002 C \u53ef\u4ee5\u653e\u5728 D (500) \u548c M (1000) \u7684\u5de6\u8fb9\uff0c\u6765\u8868\u793a 400 \u548c 900\u3002\u7ed9\u5b9a\u4e00\u4e2a\u7f57\u9a6c\u6570\u5b57\uff0c\u5c06\u5176\u8f6c\u6362\u6210\u6574\u6570\u3002\u8f93\u5165\u786e\u4fdd\u5728 1 \u5230 3999 \u7684\u8303\u56f4\u5185\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: "III"\n\u8f93\u51fa: 3\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: "IV"\n\u8f93\u51fa: 4\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: "IX"\n\u8f93\u51fa: 9\n\n
\u793a\u4f8b 4:
\n\n\u8f93\u5165: "LVIII"\n\u8f93\u51fa: 58\n\u89e3\u91ca: C = 100, L = 50, XXX = 30, III = 3.\n\n\n
\u793a\u4f8b 5:
\n\n\u8f93\u5165: "MCMXCIV"\n\u8f93\u51fa: 1994\n\u89e3\u91ca: M = 1000, CM = 900, XC = 90, IV = 4.\n*/\n\n\n/**\n * @param {string} s\n * @return {number}\n */\nvar romanToInt = function(s) {\n \n};"} {"doc_id": "0bd2d7379c63ad921eb0913a57b556c7", "text": "/**\n * Created by montaque22 on 2/24/15.\n */\n(function(){\n var queue = (function(){\n var cell = {};\n function strip(str){\n return str.replace(/[^a-zA-Z ]/g, \"\");\n }\n return {\n store:function(item, args){\n var id = strip(item.toString());\n if(!cell[id]){\n cell[id]={item:item, extra:args};\n }\n },\n find:function(item){\n var id = strip(item.toString());\n var obj = cell[id];\n cell[id] = null;\n return obj;\n }\n }\n })();\n /*\n Curry allows your to feed parameters\n to your function until all the requirements are met.\n\n Ex.\n add(a, b){\n //.. Logic to add a & b\n }\n\n // All are Equivalent\n add.curry()(1)(2);\n add.curry(1)(2);\n add.curry(1,2)();\n add.curry()(1,2);\n add.curry(1)()(2);\n // All of the above equals 3\n */\n Function.prototype.curry = function(){\n var length = this.length;\n var fn = this;\n var args = [].splice.call(arguments,0);\n return args.length >= length ?\n function(){\n return fn.apply(fn, args);\n }\n :\n function(){\n var combined = args.concat([].splice.call(arguments,0));\n return combined.length < length ?\n fn.curry.apply(fn,combined)\n :\n fn.apply(fn, combined);\n };\n };\n\n /*\n Pipe feeds the value returned from the caller to the next\n function in the list. Pipe can take an unlimited number of\n functions to chain in the pipe.\n\n Pipe works similar to piping in the terminal however\n this function also employs currying to allow functions\n that require more than one parameter to have a chance to\n fill their remaining values.\n\n\n Ex.\n function add(a, b){\n return a + b;\n }\n\n function sub(a, b){\n return b - a;\n }\n\n function multi(a, b, c){\n return a * b * c;\n }\n\n function half(a){\n return a / 2;\n }\n\n var pipeOne = add.pipe(half);\n pipeOne(2,4); // Returns 2;\n\n var pipeTwo = add.pipe(half, sub);\n\n var pipeInSecondState = pipeTwo(8,2);\n // Pipe executed the add function which piped 10 to the half function.\n // The half function needs only 1 parameter which was fulfilled by the previous function so\n // half was executed with the value 10 which returned 5.\n // However the sub function needs 2 parameters, so it stored the 5 (which came from the half function)\n // internally and returned a curried function that needs only 1 more value to execute.\n\n pipeInSecondState(3)\n // Executes the sub function against the internally stored value 5 which returns 2\n\n pipeInSecondState(10)\n // The function can be reused multiple times and the values passed in will be executed against the\n // internally stored 5. So the function will return -5\n\n var pipeThree = add.pipe(sub, multi, half);\n var anotherPipeInSecondState = pipeThree(1)(2); // stores 3 internally\n // The add function needs two values so it will curry until the parameters are satisfied.\n\n var pipeInThirdState = anotherPipeInSecondState()()(5); // stores 2 internally\n // The sub function was fed 3 by the add function and needs 1 more parameter fulfilled.\n // It will return itself until the function is satisfied\n\n pipeInThirdState()()(2)()(2,3); // returns 6\n // The multi function needs two more of its parameters fulfilled so, like the example before it,\n // it will continue to return itself until it receives a input that satisfies the correct number\n // of input. pipeInThirdState()()(2)() were incorrect number of parameters. The function doesnt\n // execute until it reaches the input (2,3).\n // Since the next function only needs 1 parameter it is immediately fired which returns 6\n\n // The above executed the following:\n // ((5 - (1 + 2)) * 2 * 3) / 2\n */\n Function.prototype.pipe = function(){\n var functions = [].splice.call(arguments,0);\n var currentFunc = this;\n var savedInputs = [];\n return function stream(){\n var duplicatedFunctions = functions.slice();\n\n // If the arguments satisfy the current Function's Needs on the first try\n if(!savedInputs.length && arguments.length >= currentFunc.length)\n return helper(arguments);\n\n // If the arguments satisfy the current function's needs on any 'second' try\n else if(arguments.length + savedInputs.length >= currentFunc.length)\n return helper(savedInputs\n .concat(Array\n .prototype\n .splice\n .call(arguments,0)));\n\n // Return your self cuz the caller sucked\n else {\n // Only Save the arguments the first time\n // If we make it to this point means that we were being called recursively from the function before this\n // Saving the input is similar to saving the state and allows the user to pickup where they left off.\n if(!savedInputs.length){\n savedInputs = savedInputs.concat([].splice.call(arguments,0));\n }\n\n return stream;\n\n }\n\n function helper(args){\n var nextFunction = duplicatedFunctions.shift();\n var param = currentFunc.apply(currentFunc, args);\n return (!nextFunction)? param : nextFunction.pipe.apply(nextFunction, duplicatedFunctions)(param);\n }\n };\n };\n\n\n /*\n Stores the values for a function to be used later on.\n This is supposed to be used with release.\n */\n Function.prototype.hold = function(){\n //store the function internally along with any arguments passed\n //only one function can be stored at a time\n // note : the function is still usable\n queue.store(this, arguments);\n };\n\n /*\n This releases the function that was held previously\n and executes it along with the arguments that were given at both\n instances.\n\n This companion function is useful in situations similar to the following:\n\n function add(a, b){\n return a + b\n }\n\n add.hold(10);\n\n setTimeout(function(){\n var ans = add.release(5);\n // returns 15\n },5000)\n\n */\n Function.prototype.release = function(){\n // call the function that was in queue\n // any previous arguements plus the ones passed in at this point are used\n // the function is released from holding and\n // the process can be repeated again\n var data = queue.find(this.toString());\n var args = [].splice.call(data.extra,0);\n var combine = args.concat([].splice.call(arguments, 0));\n return this.apply(this, combine);\n\n };\n\n})();\n\n"} {"doc_id": "0bf3397160dd49d3f5f089a24288caaa", "text": "/*\nTwo red beads are placed between every two blue beads. There are N blue beads. After looking at the arrangement below work out the number of red beads.\n\n@ @@ @ @@ @ @@ @ @@ @ @@ @\n\nImplement count_red_beads(n) (in PHP count_red_beads($n); in Java, Javascript, TypeScript, C, C++ countRedBeads(n)) so that it returns the number of red beads.\nIf there are less than 2 blue beads return 0. \n*/\n\nfunction countRedBeads(n) {\n if (n < 2) return 0;\n return (n - 1) * 2;\n \n // Another Solution:\n // return n < 2 ? 0 : 2 * n - 2;\n}"} {"doc_id": "0bf3b733e45c31e49d6e3384095e3786", "text": "/*\n
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
\n\nThe digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
\n\nYou may assume the integer does not contain any leading zero, except the number 0 itself.
\n\nExample 1:
\n\n\nInput: [1,2,3]\nOutput: [1,2,4]\nExplanation: The array represents the integer 123.\n\n\n
Example 2:
\n\n\nInput: [4,3,2,1]\nOutput: [4,3,2,2]\nExplanation: The array represents the integer 4321.\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u7ec4\u6210\u7684\u975e\u7a7a\u6570\u7ec4\uff0c\u5728\u8be5\u6570\u7684\u57fa\u7840\u4e0a\u52a0\u4e00\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002
\n\n\u6700\u9ad8\u4f4d\u6570\u5b57\u5b58\u653e\u5728\u6570\u7ec4\u7684\u9996\u4f4d\uff0c \u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u53ea\u5b58\u50a8\u4e00\u4e2a\u6570\u5b57\u3002
\n\n\u4f60\u53ef\u4ee5\u5047\u8bbe\u9664\u4e86\u6574\u6570 0 \u4e4b\u5916\uff0c\u8fd9\u4e2a\u6574\u6570\u4e0d\u4f1a\u4ee5\u96f6\u5f00\u5934\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: [1,2,3]\n\u8f93\u51fa: [1,2,4]\n\u89e3\u91ca: \u8f93\u5165\u6570\u7ec4\u8868\u793a\u6570\u5b57 123\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: [4,3,2,1]\n\u8f93\u51fa: [4,3,2,2]\n\u89e3\u91ca: \u8f93\u5165\u6570\u7ec4\u8868\u793a\u6570\u5b57 4321\u3002\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u975e\u8d1f\u6574\u6570\u7ec4\u6210\u7684\u975e\u7a7a\u6570\u7ec4\uff0c\u5728\u8be5\u6570\u7684\u57fa\u7840\u4e0a\u52a0\u4e00\uff0c\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u6570\u7ec4\u3002
\n\n\u6700\u9ad8\u4f4d\u6570\u5b57\u5b58\u653e\u5728\u6570\u7ec4\u7684\u9996\u4f4d\uff0c \u6570\u7ec4\u4e2d\u6bcf\u4e2a\u5143\u7d20\u53ea\u5b58\u50a8\u4e00\u4e2a\u6570\u5b57\u3002
\n\n\u4f60\u53ef\u4ee5\u5047\u8bbe\u9664\u4e86\u6574\u6570 0 \u4e4b\u5916\uff0c\u8fd9\u4e2a\u6574\u6570\u4e0d\u4f1a\u4ee5\u96f6\u5f00\u5934\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: [1,2,3]\n\u8f93\u51fa: [1,2,4]\n\u89e3\u91ca: \u8f93\u5165\u6570\u7ec4\u8868\u793a\u6570\u5b57 123\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: [4,3,2,1]\n\u8f93\u51fa: [4,3,2,2]\n\u89e3\u91ca: \u8f93\u5165\u6570\u7ec4\u8868\u793a\u6570\u5b57 4321\u3002\n\n*/\n\n\n/**\n * @param {number[]} digits\n * @return {number[]}\n */\nvar plusOne = function(digits) {\n \n};"} {"doc_id": "0d088f89fe8a4d4e5f6e43b97e7b72d9", "text": "/*\n
\nA string S of lowercase letters is given. We want to partition this string into as many parts as possible so that each letter appears in at most one part, and return a list of integers representing the size of these parts.\n
\n\n
Example 1:
\n
\nInput: S = \"ababcbacadefegdehijhklij\"\nOutput: [9,7,8]\nExplanation:\nThe partition is \"ababcbaca\", \"defegde\", \"hijhklij\".\nThis is a partition so that each letter appears in at most one part.\nA partition like \"ababcbacadefegde\", \"hijhklij\" is incorrect, because it splits S into less parts.\n\n\n\n
Note:
S will have length in range [1, 500].S will consist of lowercase letters ('a' to 'z') only.\u5b57\u7b26\u4e32 S \u7531\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u3002\u6211\u4eec\u8981\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5212\u5206\u4e3a\u5c3d\u53ef\u80fd\u591a\u7684\u7247\u6bb5\uff0c\u540c\u4e00\u4e2a\u5b57\u6bcd\u53ea\u4f1a\u51fa\u73b0\u5728\u5176\u4e2d\u7684\u4e00\u4e2a\u7247\u6bb5\u3002\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u6bcf\u4e2a\u5b57\u7b26\u4e32\u7247\u6bb5\u7684\u957f\u5ea6\u7684\u5217\u8868\u3002
\u793a\u4f8b 1:
\n\n\u8f93\u5165: S = "ababcbacadefegdehijhklij"\n\u8f93\u51fa: [9,7,8]\n\u89e3\u91ca:\n\u5212\u5206\u7ed3\u679c\u4e3a "ababcbaca", "defegde", "hijhklij"\u3002\n\u6bcf\u4e2a\u5b57\u6bcd\u6700\u591a\u51fa\u73b0\u5728\u4e00\u4e2a\u7247\u6bb5\u4e2d\u3002\n\u50cf "ababcbacadefegde", "hijhklij" \u7684\u5212\u5206\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u5212\u5206\u7684\u7247\u6bb5\u6570\u8f83\u5c11\u3002\n\n\n
\u6ce8\u610f:
\n\nS\u7684\u957f\u5ea6\u5728[1, 500]\u4e4b\u95f4\u3002S\u53ea\u5305\u542b\u5c0f\u5199\u5b57\u6bcd'a'\u5230'z'\u3002\u5b57\u7b26\u4e32 S \u7531\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u3002\u6211\u4eec\u8981\u628a\u8fd9\u4e2a\u5b57\u7b26\u4e32\u5212\u5206\u4e3a\u5c3d\u53ef\u80fd\u591a\u7684\u7247\u6bb5\uff0c\u540c\u4e00\u4e2a\u5b57\u6bcd\u53ea\u4f1a\u51fa\u73b0\u5728\u5176\u4e2d\u7684\u4e00\u4e2a\u7247\u6bb5\u3002\u8fd4\u56de\u4e00\u4e2a\u8868\u793a\u6bcf\u4e2a\u5b57\u7b26\u4e32\u7247\u6bb5\u7684\u957f\u5ea6\u7684\u5217\u8868\u3002
\u793a\u4f8b 1:
\n\n\u8f93\u5165: S = "ababcbacadefegdehijhklij"\n\u8f93\u51fa: [9,7,8]\n\u89e3\u91ca:\n\u5212\u5206\u7ed3\u679c\u4e3a "ababcbaca", "defegde", "hijhklij"\u3002\n\u6bcf\u4e2a\u5b57\u6bcd\u6700\u591a\u51fa\u73b0\u5728\u4e00\u4e2a\u7247\u6bb5\u4e2d\u3002\n\u50cf "ababcbacadefegde", "hijhklij" \u7684\u5212\u5206\u662f\u9519\u8bef\u7684\uff0c\u56e0\u4e3a\u5212\u5206\u7684\u7247\u6bb5\u6570\u8f83\u5c11\u3002\n\n\n
\u6ce8\u610f:
\n\nS\u7684\u957f\u5ea6\u5728[1, 500]\u4e4b\u95f4\u3002S\u53ea\u5305\u542b\u5c0f\u5199\u5b57\u6bcd'a'\u5230'z'\u3002Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
\n\nExample 1:
\n\n\nInput:\nmatrix = [\n [1, 3, 5, 7],\n [10, 11, 16, 20],\n [23, 30, 34, 50]\n]\ntarget = 3\nOutput: true\n\n\n
Example 2:
\n\n\nInput:\nmatrix = [\n [1, 3, 5, 7],\n [10, 11, 16, 20],\n [23, 30, 34, 50]\n]\ntarget = 13\nOutput: false\n
\u7f16\u5199\u4e00\u4e2a\u9ad8\u6548\u7684\u7b97\u6cd5\u6765\u5224\u65ad m x n \u77e9\u9635\u4e2d\uff0c\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u76ee\u6807\u503c\u3002\u8be5\u77e9\u9635\u5177\u6709\u5982\u4e0b\u7279\u6027\uff1a
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165:\nmatrix = [\n [1, 3, 5, 7],\n [10, 11, 16, 20],\n [23, 30, 34, 50]\n]\ntarget = 3\n\u8f93\u51fa: true\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165:\nmatrix = [\n [1, 3, 5, 7],\n [10, 11, 16, 20],\n [23, 30, 34, 50]\n]\ntarget = 13\n\u8f93\u51fa: false\n
\u7f16\u5199\u4e00\u4e2a\u9ad8\u6548\u7684\u7b97\u6cd5\u6765\u5224\u65ad m x n \u77e9\u9635\u4e2d\uff0c\u662f\u5426\u5b58\u5728\u4e00\u4e2a\u76ee\u6807\u503c\u3002\u8be5\u77e9\u9635\u5177\u6709\u5982\u4e0b\u7279\u6027\uff1a
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165:\nmatrix = [\n [1, 3, 5, 7],\n [10, 11, 16, 20],\n [23, 30, 34, 50]\n]\ntarget = 3\n\u8f93\u51fa: true\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165:\nmatrix = [\n [1, 3, 5, 7],\n [10, 11, 16, 20],\n [23, 30, 34, 50]\n]\ntarget = 13\n\u8f93\u51fa: false\n*/\n\n\n/**\n * @param {number[][]} matrix\n * @param {number} target\n * @return {boolean}\n */\nvar searchMatrix = function(matrix, target) {\n \n};"} {"doc_id": "11d1dda98dff45b1026fa4d2d4875f47", "text": "/*\n
Design a class to find the kth largest element in a stream. Note that it is the kth largest element in the sorted order, not the kth distinct element.
\n\nYour KthLargest class will have a constructor which accepts an integer k and an integer array nums, which contains initial elements from the stream. For each call to the method KthLargest.add, return the element representing the kth largest element in the stream.
Example:
\n\n\nint k = 3;\nint[] arr = [4,5,8,2];\nKthLargest kthLargest = new KthLargest(3, arr);\nkthLargest.add(3); // returns 4\nkthLargest.add(5); // returns 5\nkthLargest.add(10); // returns 5\nkthLargest.add(9); // returns 8\nkthLargest.add(4); // returns 8\n\n\n
Note:
\nYou may assume that nums' length ≥ k-1 and k ≥ 1.
\u8bbe\u8ba1\u4e00\u4e2a\u627e\u5230\u6570\u636e\u6d41\u4e2d\u7b2cK\u5927\u5143\u7d20\u7684\u7c7b\uff08class\uff09\u3002\u6ce8\u610f\u662f\u6392\u5e8f\u540e\u7684\u7b2cK\u5927\u5143\u7d20\uff0c\u4e0d\u662f\u7b2cK\u4e2a\u4e0d\u540c\u7684\u5143\u7d20\u3002
\n\n\u4f60\u7684 KthLargest \u7c7b\u9700\u8981\u4e00\u4e2a\u540c\u65f6\u63a5\u6536\u6574\u6570 k \u548c\u6574\u6570\u6570\u7ec4nums \u7684\u6784\u9020\u5668\uff0c\u5b83\u5305\u542b\u6570\u636e\u6d41\u4e2d\u7684\u521d\u59cb\u5143\u7d20\u3002\u6bcf\u6b21\u8c03\u7528 KthLargest.add\uff0c\u8fd4\u56de\u5f53\u524d\u6570\u636e\u6d41\u4e2d\u7b2cK\u5927\u7684\u5143\u7d20\u3002
\u793a\u4f8b:
\n\n\nint k = 3;\nint[] arr = [4,5,8,2];\nKthLargest kthLargest = new KthLargest(3, arr);\nkthLargest.add(3); // returns 4\nkthLargest.add(5); // returns 5\nkthLargest.add(10); // returns 5\nkthLargest.add(9); // returns 8\nkthLargest.add(4); // returns 8\n\n\n
\u8bf4\u660e:
\n\u4f60\u53ef\u4ee5\u5047\u8bbe nums \u7684\u957f\u5ea6≥ k-1 \u4e14k ≥ 1\u3002
\u8bbe\u8ba1\u4e00\u4e2a\u627e\u5230\u6570\u636e\u6d41\u4e2d\u7b2cK\u5927\u5143\u7d20\u7684\u7c7b\uff08class\uff09\u3002\u6ce8\u610f\u662f\u6392\u5e8f\u540e\u7684\u7b2cK\u5927\u5143\u7d20\uff0c\u4e0d\u662f\u7b2cK\u4e2a\u4e0d\u540c\u7684\u5143\u7d20\u3002
\n\n\u4f60\u7684 KthLargest \u7c7b\u9700\u8981\u4e00\u4e2a\u540c\u65f6\u63a5\u6536\u6574\u6570 k \u548c\u6574\u6570\u6570\u7ec4nums \u7684\u6784\u9020\u5668\uff0c\u5b83\u5305\u542b\u6570\u636e\u6d41\u4e2d\u7684\u521d\u59cb\u5143\u7d20\u3002\u6bcf\u6b21\u8c03\u7528 KthLargest.add\uff0c\u8fd4\u56de\u5f53\u524d\u6570\u636e\u6d41\u4e2d\u7b2cK\u5927\u7684\u5143\u7d20\u3002
\u793a\u4f8b:
\n\n\nint k = 3;\nint[] arr = [4,5,8,2];\nKthLargest kthLargest = new KthLargest(3, arr);\nkthLargest.add(3); // returns 4\nkthLargest.add(5); // returns 5\nkthLargest.add(10); // returns 5\nkthLargest.add(9); // returns 8\nkthLargest.add(4); // returns 8\n\n\n
\u8bf4\u660e:
\n\u4f60\u53ef\u4ee5\u5047\u8bbe nums \u7684\u957f\u5ea6≥ k-1 \u4e14k ≥ 1\u3002
Given a non-empty string containing an out-of-order English representation of digits 0-9, output the digits in ascending order.
Note:
\n
Example 1:
\n
\nInput: \"owoztneoer\"\n\nOutput: \"012\"\n\n\n\n
Example 2:
\n
\nInput: \"fviefuro\"\n\nOutput: \"45\"\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u5b57\u6bcd\u987a\u5e8f\u6253\u4e71\u7684\u82f1\u6587\u5355\u8bcd\u8868\u793a\u7684\u6570\u5b570-9\u3002\u6309\u5347\u5e8f\u8f93\u51fa\u539f\u59cb\u7684\u6570\u5b57\u3002
\u6ce8\u610f:
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: "owoztneoer"\n\n\u8f93\u51fa: "012" (zeroonetwo)\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: "fviefuro"\n\n\u8f93\u51fa: "45" (fourfive)\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u4e32\uff0c\u5176\u4e2d\u5305\u542b\u5b57\u6bcd\u987a\u5e8f\u6253\u4e71\u7684\u82f1\u6587\u5355\u8bcd\u8868\u793a\u7684\u6570\u5b570-9\u3002\u6309\u5347\u5e8f\u8f93\u51fa\u539f\u59cb\u7684\u6570\u5b57\u3002
\u6ce8\u610f:
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: "owoztneoer"\n\n\u8f93\u51fa: "012" (zeroonetwo)\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: "fviefuro"\n\n\u8f93\u51fa: "45" (fourfive)\n\n*/\n\n\n/**\n * @param {string} s\n * @return {string}\n */\nvar originalDigits = function(s) {\n \n};"} {"doc_id": "123539bb72de07d91b553cd73e8c7784", "text": "// JavaScript source code\nvar factorial = parseInt(prompt(\"Enter the number to factorialize\"));\nvar counter = factorial;\nvar total = 1;\n\nif (factorial > 0) {\n\twhile (counter > 1) {\n\t\ttotal *= counter;\n\t\tcounter--;\n\t}\n\twindow.alert(factorial + \"! = \" + total);\n} else {\n\twindow.alert(\"That number is invalid\");\n}"} {"doc_id": "1248637b0e01351a7c65bfc82e021c45", "text": "/*\n
Given a list of strings words representing an English Dictionary, find the longest word in words that can be built one character at a time by other words in words. If there is more than one possible answer, return the longest word with the smallest lexicographical order.
Example 1:
\n
\nInput: \nwords = [\"w\",\"wo\",\"wor\",\"worl\", \"world\"]\nOutput: \"world\"\nExplanation: \nThe word \"world\" can be built one character at a time by \"w\", \"wo\", \"wor\", and \"worl\".\n\n\n\n
Example 2:
\n
\nInput: \nwords = [\"a\", \"banana\", \"app\", \"appl\", \"ap\", \"apply\", \"apple\"]\nOutput: \"apple\"\nExplanation: \nBoth \"apply\" and \"apple\" can be built from other words in the dictionary. However, \"apple\" is lexicographically smaller than \"apply\".\n\n\n\n
Note:\n
words will be in the range [1, 1000].words[i] will be in the range [1, 30].\u7ed9\u51fa\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4words\u7ec4\u6210\u7684\u4e00\u672c\u82f1\u8bed\u8bcd\u5178\u3002\u4ece\u4e2d\u627e\u51fa\u6700\u957f\u7684\u4e00\u4e2a\u5355\u8bcd\uff0c\u8be5\u5355\u8bcd\u662f\u7531words\u8bcd\u5178\u4e2d\u5176\u4ed6\u5355\u8bcd\u9010\u6b65\u6dfb\u52a0\u4e00\u4e2a\u5b57\u6bcd\u7ec4\u6210\u3002\u82e5\u5176\u4e2d\u6709\u591a\u4e2a\u53ef\u884c\u7684\u7b54\u6848\uff0c\u5219\u8fd4\u56de\u7b54\u6848\u4e2d\u5b57\u5178\u5e8f\u6700\u5c0f\u7684\u5355\u8bcd\u3002
\u82e5\u65e0\u7b54\u6848\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: \nwords = ["w","wo","wor","worl", "world"]\n\u8f93\u51fa: "world"\n\u89e3\u91ca: \n\u5355\u8bcd"world"\u53ef\u7531"w", "wo", "wor", \u548c "worl"\u6dfb\u52a0\u4e00\u4e2a\u5b57\u6bcd\u7ec4\u6210\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: \nwords = ["a", "banana", "app", "appl", "ap", "apply", "apple"]\n\u8f93\u51fa: "apple"\n\u89e3\u91ca: \n"apply"\u548c"apple"\u90fd\u80fd\u7531\u8bcd\u5178\u4e2d\u7684\u5355\u8bcd\u7ec4\u6210\u3002\u4f46\u662f"apple"\u5f97\u5b57\u5178\u5e8f\u5c0f\u4e8e"apply"\u3002\n\n\n
\u6ce8\u610f:
\n\nwords\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u4e3a[1,1000]\u3002words[i]\u7684\u957f\u5ea6\u8303\u56f4\u4e3a[1,30]\u3002\u7ed9\u51fa\u4e00\u4e2a\u5b57\u7b26\u4e32\u6570\u7ec4words\u7ec4\u6210\u7684\u4e00\u672c\u82f1\u8bed\u8bcd\u5178\u3002\u4ece\u4e2d\u627e\u51fa\u6700\u957f\u7684\u4e00\u4e2a\u5355\u8bcd\uff0c\u8be5\u5355\u8bcd\u662f\u7531words\u8bcd\u5178\u4e2d\u5176\u4ed6\u5355\u8bcd\u9010\u6b65\u6dfb\u52a0\u4e00\u4e2a\u5b57\u6bcd\u7ec4\u6210\u3002\u82e5\u5176\u4e2d\u6709\u591a\u4e2a\u53ef\u884c\u7684\u7b54\u6848\uff0c\u5219\u8fd4\u56de\u7b54\u6848\u4e2d\u5b57\u5178\u5e8f\u6700\u5c0f\u7684\u5355\u8bcd\u3002
\u82e5\u65e0\u7b54\u6848\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: \nwords = ["w","wo","wor","worl", "world"]\n\u8f93\u51fa: "world"\n\u89e3\u91ca: \n\u5355\u8bcd"world"\u53ef\u7531"w", "wo", "wor", \u548c "worl"\u6dfb\u52a0\u4e00\u4e2a\u5b57\u6bcd\u7ec4\u6210\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: \nwords = ["a", "banana", "app", "appl", "ap", "apply", "apple"]\n\u8f93\u51fa: "apple"\n\u89e3\u91ca: \n"apply"\u548c"apple"\u90fd\u80fd\u7531\u8bcd\u5178\u4e2d\u7684\u5355\u8bcd\u7ec4\u6210\u3002\u4f46\u662f"apple"\u5f97\u5b57\u5178\u5e8f\u5c0f\u4e8e"apply"\u3002\n\n\n
\u6ce8\u610f:
\n\nwords\u6570\u7ec4\u957f\u5ea6\u8303\u56f4\u4e3a[1,1000]\u3002words[i]\u7684\u957f\u5ea6\u8303\u56f4\u4e3a[1,30]\u3002X is a good number if after rotating each digit individually by 180 degrees, we get a valid number that is different from X. Each digit must be rotated - we cannot choose to leave it alone.
\n\nA number is valid if each digit remains a digit after rotation. 0, 1, and 8 rotate to themselves; 2 and 5 rotate to each other; 6 and 9 rotate to each other, and the rest of the numbers do not rotate to any other number and become invalid.
\n\nNow given a positive number N, how many numbers X from 1 to N are good?
\nExample:\nInput: 10\nOutput: 4\nExplanation: \nThere are four good numbers in the range [1, 10] : 2, 5, 6, 9.\nNote that 1 and 10 are not good numbers, since they remain unchanged after rotating.\n\n\n
Note:
\n\n[1, 10000].\u6211\u4eec\u79f0\u4e00\u4e2a\u6570 X \u4e3a\u597d\u6570, \u5982\u679c\u5b83\u7684\u6bcf\u4f4d\u6570\u5b57\u9010\u4e2a\u5730\u88ab\u65cb\u8f6c 180 \u5ea6\u540e\uff0c\u6211\u4eec\u4ecd\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u6709\u6548\u7684\uff0c\u4e14\u548c X \u4e0d\u540c\u7684\u6570\u3002\u8981\u6c42\u6bcf\u4f4d\u6570\u5b57\u90fd\u8981\u88ab\u65cb\u8f6c\u3002
\n\n\u5982\u679c\u4e00\u4e2a\u6570\u7684\u6bcf\u4f4d\u6570\u5b57\u88ab\u65cb\u8f6c\u4ee5\u540e\u4ecd\u7136\u8fd8\u662f\u4e00\u4e2a\u6570\u5b57\uff0c \u5219\u8fd9\u4e2a\u6570\u662f\u6709\u6548\u7684\u30020, 1, \u548c 8 \u88ab\u65cb\u8f6c\u540e\u4ecd\u7136\u662f\u5b83\u4eec\u81ea\u5df1\uff1b2 \u548c 5 \u53ef\u4ee5\u4e92\u76f8\u65cb\u8f6c\u6210\u5bf9\u65b9\uff1b6 \u548c 9 \u540c\u7406\uff0c\u9664\u4e86\u8fd9\u4e9b\u4ee5\u5916\u5176\u4ed6\u7684\u6570\u5b57\u65cb\u8f6c\u4ee5\u540e\u90fd\u4e0d\u518d\u662f\u6709\u6548\u7684\u6570\u5b57\u3002
\n\n\u73b0\u5728\u6211\u4eec\u6709\u4e00\u4e2a\u6b63\u6574\u6570 N, \u8ba1\u7b97\u4ece 1 \u5230 N \u4e2d\u6709\u591a\u5c11\u4e2a\u6570 X \u662f\u597d\u6570\uff1f
\n\u793a\u4f8b:\n\u8f93\u5165: 10\n\u8f93\u51fa: 4\n\u89e3\u91ca: \n\u5728[1, 10]\u4e2d\u6709\u56db\u4e2a\u597d\u6570\uff1a 2, 5, 6, 9\u3002\n\u6ce8\u610f 1 \u548c 10 \u4e0d\u662f\u597d\u6570, \u56e0\u4e3a\u4ed6\u4eec\u5728\u65cb\u8f6c\u4e4b\u540e\u4e0d\u53d8\u3002\n\n\n
\u6ce8\u610f:
\n\n[1, 10000]\u3002\u6211\u4eec\u79f0\u4e00\u4e2a\u6570 X \u4e3a\u597d\u6570, \u5982\u679c\u5b83\u7684\u6bcf\u4f4d\u6570\u5b57\u9010\u4e2a\u5730\u88ab\u65cb\u8f6c 180 \u5ea6\u540e\uff0c\u6211\u4eec\u4ecd\u53ef\u4ee5\u5f97\u5230\u4e00\u4e2a\u6709\u6548\u7684\uff0c\u4e14\u548c X \u4e0d\u540c\u7684\u6570\u3002\u8981\u6c42\u6bcf\u4f4d\u6570\u5b57\u90fd\u8981\u88ab\u65cb\u8f6c\u3002
\n\n\u5982\u679c\u4e00\u4e2a\u6570\u7684\u6bcf\u4f4d\u6570\u5b57\u88ab\u65cb\u8f6c\u4ee5\u540e\u4ecd\u7136\u8fd8\u662f\u4e00\u4e2a\u6570\u5b57\uff0c \u5219\u8fd9\u4e2a\u6570\u662f\u6709\u6548\u7684\u30020, 1, \u548c 8 \u88ab\u65cb\u8f6c\u540e\u4ecd\u7136\u662f\u5b83\u4eec\u81ea\u5df1\uff1b2 \u548c 5 \u53ef\u4ee5\u4e92\u76f8\u65cb\u8f6c\u6210\u5bf9\u65b9\uff1b6 \u548c 9 \u540c\u7406\uff0c\u9664\u4e86\u8fd9\u4e9b\u4ee5\u5916\u5176\u4ed6\u7684\u6570\u5b57\u65cb\u8f6c\u4ee5\u540e\u90fd\u4e0d\u518d\u662f\u6709\u6548\u7684\u6570\u5b57\u3002
\n\n\u73b0\u5728\u6211\u4eec\u6709\u4e00\u4e2a\u6b63\u6574\u6570 N, \u8ba1\u7b97\u4ece 1 \u5230 N \u4e2d\u6709\u591a\u5c11\u4e2a\u6570 X \u662f\u597d\u6570\uff1f
\n\u793a\u4f8b:\n\u8f93\u5165: 10\n\u8f93\u51fa: 4\n\u89e3\u91ca: \n\u5728[1, 10]\u4e2d\u6709\u56db\u4e2a\u597d\u6570\uff1a 2, 5, 6, 9\u3002\n\u6ce8\u610f 1 \u548c 10 \u4e0d\u662f\u597d\u6570, \u56e0\u4e3a\u4ed6\u4eec\u5728\u65cb\u8f6c\u4e4b\u540e\u4e0d\u53d8\u3002\n\n\n
\u6ce8\u610f:
\n\n[1, 10000]\u3002\nGiven an array nums of integers, you can perform operations on the array.\n
\nIn each operation, you pick any nums[i] and delete it to earn nums[i] points. After, you must delete every element equal to nums[i] - 1 or nums[i] + 1.\n
\nYou start with 0 points. Return the maximum number of points you can earn by applying such operations.\n
\n\nExample 1:
\n
\nInput: nums = [3, 4, 2]\nOutput: 6\nExplanation: \nDelete 4 to earn 4 points, consequently 3 is also deleted.\nThen, delete 2 to earn 2 points. 6 total points are earned.\n\n\n\n
Example 2:
\n
\nInput: nums = [2, 2, 3, 3, 3, 4]\nOutput: 9\nExplanation: \nDelete 3 to earn 3 points, deleting both 2's and the 4.\nThen, delete 3 again to earn 3 points, and 3 again to earn 3 points.\n9 total points are earned.\n\n\n\n
Note:\n
nums is at most 20000.nums[i] is an integer in the range [1, 10000].\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b83\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002
\u6bcf\u6b21\u64cd\u4f5c\u4e2d\uff0c\u9009\u62e9\u4efb\u610f\u4e00\u4e2a nums[i] \uff0c\u5220\u9664\u5b83\u5e76\u83b7\u5f97 nums[i] \u7684\u70b9\u6570\u3002\u4e4b\u540e\uff0c\u4f60\u5fc5\u987b\u5220\u9664\u6bcf\u4e2a\u7b49\u4e8e nums[i] - 1 \u6216 nums[i] + 1 \u7684\u5143\u7d20\u3002
\u5f00\u59cb\u4f60\u62e5\u6709 0 \u4e2a\u70b9\u6570\u3002\u8fd4\u56de\u4f60\u80fd\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\u83b7\u5f97\u7684\u6700\u5927\u70b9\u6570\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: nums = [3, 4, 2]\n\u8f93\u51fa: 6\n\u89e3\u91ca: \n\u5220\u9664 4 \u6765\u83b7\u5f97 4 \u4e2a\u70b9\u6570\uff0c\u56e0\u6b64 3 \u4e5f\u88ab\u5220\u9664\u3002\n\u4e4b\u540e\uff0c\u5220\u9664 2 \u6765\u83b7\u5f97 2 \u4e2a\u70b9\u6570\u3002\u603b\u5171\u83b7\u5f97 6 \u4e2a\u70b9\u6570\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: nums = [2, 2, 3, 3, 3, 4]\n\u8f93\u51fa: 9\n\u89e3\u91ca: \n\u5220\u9664 3 \u6765\u83b7\u5f97 3 \u4e2a\u70b9\u6570\uff0c\u63a5\u7740\u8981\u5220\u9664\u4e24\u4e2a 2 \u548c 4 \u3002\n\u4e4b\u540e\uff0c\u518d\u6b21\u5220\u9664 3 \u83b7\u5f97 3 \u4e2a\u70b9\u6570\uff0c\u518d\u6b21\u5220\u9664 3 \u83b7\u5f97 3 \u4e2a\u70b9\u6570\u3002\n\u603b\u5171\u83b7\u5f97 9 \u4e2a\u70b9\u6570\u3002\n\n\n
\u6ce8\u610f:
\n\nnums\u7684\u957f\u5ea6\u6700\u5927\u4e3a20000\u3002nums[i]\u7684\u5927\u5c0f\u90fd\u5728[1, 10000]\u8303\u56f4\u5185\u3002\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 nums \uff0c\u4f60\u53ef\u4ee5\u5bf9\u5b83\u8fdb\u884c\u4e00\u4e9b\u64cd\u4f5c\u3002
\u6bcf\u6b21\u64cd\u4f5c\u4e2d\uff0c\u9009\u62e9\u4efb\u610f\u4e00\u4e2a nums[i] \uff0c\u5220\u9664\u5b83\u5e76\u83b7\u5f97 nums[i] \u7684\u70b9\u6570\u3002\u4e4b\u540e\uff0c\u4f60\u5fc5\u987b\u5220\u9664\u6bcf\u4e2a\u7b49\u4e8e nums[i] - 1 \u6216 nums[i] + 1 \u7684\u5143\u7d20\u3002
\u5f00\u59cb\u4f60\u62e5\u6709 0 \u4e2a\u70b9\u6570\u3002\u8fd4\u56de\u4f60\u80fd\u901a\u8fc7\u8fd9\u4e9b\u64cd\u4f5c\u83b7\u5f97\u7684\u6700\u5927\u70b9\u6570\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: nums = [3, 4, 2]\n\u8f93\u51fa: 6\n\u89e3\u91ca: \n\u5220\u9664 4 \u6765\u83b7\u5f97 4 \u4e2a\u70b9\u6570\uff0c\u56e0\u6b64 3 \u4e5f\u88ab\u5220\u9664\u3002\n\u4e4b\u540e\uff0c\u5220\u9664 2 \u6765\u83b7\u5f97 2 \u4e2a\u70b9\u6570\u3002\u603b\u5171\u83b7\u5f97 6 \u4e2a\u70b9\u6570\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: nums = [2, 2, 3, 3, 3, 4]\n\u8f93\u51fa: 9\n\u89e3\u91ca: \n\u5220\u9664 3 \u6765\u83b7\u5f97 3 \u4e2a\u70b9\u6570\uff0c\u63a5\u7740\u8981\u5220\u9664\u4e24\u4e2a 2 \u548c 4 \u3002\n\u4e4b\u540e\uff0c\u518d\u6b21\u5220\u9664 3 \u83b7\u5f97 3 \u4e2a\u70b9\u6570\uff0c\u518d\u6b21\u5220\u9664 3 \u83b7\u5f97 3 \u4e2a\u70b9\u6570\u3002\n\u603b\u5171\u83b7\u5f97 9 \u4e2a\u70b9\u6570\u3002\n\n\n
\u6ce8\u610f:
\n\nnums\u7684\u957f\u5ea6\u6700\u5927\u4e3a20000\u3002nums[i]\u7684\u5927\u5c0f\u90fd\u5728[1, 10000]\u8303\u56f4\u5185\u3002\nGiven a sequence of n integers a1, a2, ..., an, a 132 pattern is a subsequence ai, aj, ak such\nthat i < j < k and ai < ak < aj. Design an algorithm that takes a list of n numbers as input and checks whether there is a 132 pattern in the list.
\n\nNote: n will be less than 15,000.
\n\nExample 1:
\n
\nInput: [1, 2, 3, 4]\n\nOutput: False\n\nExplanation: There is no 132 pattern in the sequence.\n\n\n\n
Example 2:
\n
\nInput: [3, 1, 4, 2]\n\nOutput: True\n\nExplanation: There is a 132 pattern in the sequence: [1, 4, 2].\n\n\n\n
Example 3:
\n
\nInput: [-1, 3, 2, 0]\n\nOutput: True\n\nExplanation: There are three 132 patterns in the sequence: [-1, 3, 2], [-1, 3, 0] and [-1, 2, 0].\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\uff1aa1, a2, ..., an\uff0c\u4e00\u4e2a132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217 ai, aj, ak \u88ab\u5b9a\u4e49\u4e3a\uff1a\u5f53 i < j < k \u65f6\uff0cai < ak < aj\u3002\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\uff0c\u5f53\u7ed9\u5b9a\u6709 n \u4e2a\u6570\u5b57\u7684\u5e8f\u5217\u65f6\uff0c\u9a8c\u8bc1\u8fd9\u4e2a\u5e8f\u5217\u4e2d\u662f\u5426\u542b\u6709132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217\u3002
\n\n\u6ce8\u610f\uff1an \u7684\u503c\u5c0f\u4e8e15000\u3002
\n\n\u793a\u4f8b1:
\n\n\n\u8f93\u5165: [1, 2, 3, 4]\n\n\u8f93\u51fa: False\n\n\u89e3\u91ca: \u5e8f\u5217\u4e2d\u4e0d\u5b58\u5728132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: [3, 1, 4, 2]\n\n\u8f93\u51fa: True\n\n\u89e3\u91ca: \u5e8f\u5217\u4e2d\u6709 1 \u4e2a132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217\uff1a [1, 4, 2].\n\n\n
\u793a\u4f8b 3:
\n\n\n\u8f93\u5165: [-1, 3, 2, 0]\n\n\u8f93\u51fa: True\n\n\u89e3\u91ca: \u5e8f\u5217\u4e2d\u6709 3 \u4e2a132\u6a21\u5f0f\u7684\u7684\u5b50\u5e8f\u5217: [-1, 3, 2], [-1, 3, 0] \u548c [-1, 2, 0].\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u5e8f\u5217\uff1aa1, a2, ..., an\uff0c\u4e00\u4e2a132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217 ai, aj, ak \u88ab\u5b9a\u4e49\u4e3a\uff1a\u5f53 i < j < k \u65f6\uff0cai < ak < aj\u3002\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\uff0c\u5f53\u7ed9\u5b9a\u6709 n \u4e2a\u6570\u5b57\u7684\u5e8f\u5217\u65f6\uff0c\u9a8c\u8bc1\u8fd9\u4e2a\u5e8f\u5217\u4e2d\u662f\u5426\u542b\u6709132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217\u3002
\n\n\u6ce8\u610f\uff1an \u7684\u503c\u5c0f\u4e8e15000\u3002
\n\n\u793a\u4f8b1:
\n\n\n\u8f93\u5165: [1, 2, 3, 4]\n\n\u8f93\u51fa: False\n\n\u89e3\u91ca: \u5e8f\u5217\u4e2d\u4e0d\u5b58\u5728132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: [3, 1, 4, 2]\n\n\u8f93\u51fa: True\n\n\u89e3\u91ca: \u5e8f\u5217\u4e2d\u6709 1 \u4e2a132\u6a21\u5f0f\u7684\u5b50\u5e8f\u5217\uff1a [1, 4, 2].\n\n\n
\u793a\u4f8b 3:
\n\n\n\u8f93\u5165: [-1, 3, 2, 0]\n\n\u8f93\u51fa: True\n\n\u89e3\u91ca: \u5e8f\u5217\u4e2d\u6709 3 \u4e2a132\u6a21\u5f0f\u7684\u7684\u5b50\u5e8f\u5217: [-1, 3, 2], [-1, 3, 0] \u548c [-1, 2, 0].\n\n*/\n\n\n/**\n * @param {number[]} nums\n * @return {boolean}\n */\nvar find132pattern = function(nums) {\n \n};"} {"doc_id": "17a218b62648ece24fe8101886d521fd", "text": "/**\n * Created by\uff1aCaMnter\n */\n\n/****************\n * ES6 Set \u57fa\u672c *\n ****************/\n\n/**\n * ES6 \u63d0\u4f9b\u4e86\u65b0\u7684\u6570\u636e\u7ed3\u6784 Set\n * \u5b83\u7c7b\u4f3c\u4e8e\u6570\u7ec4\n * \u4f46\u662f\u6210\u5458\u7684\u503c\u90fd\u662f\u552f\u4e00\u7684\n * \u6ca1\u6709\u91cd\u590d\u7684\u503c\n */\n\n(() => {\n var set = new Set();\n // \u6dfb\u52a0\u91cd\u590d\u503c\u65e0\u6548\n [2, 3, 3].map(v => set.add(v));\n console.log(\"[structure]\\t\\t[test-\" + 1 + \"]\\t\\t[value] = \", [...set], '\\n');\n})();\n\n/**\n * \u6570\u7ec4\u4f5c\u4e3a\u53c2\u6570\n */\n(() => {\n var set1 = new Set([1, 2, 3, 4]);\n for (let value of set1) {\n console.log(\"[structure]\\t\\t[test-\" + 2 + \"]\\t\\t[value] = \", value);\n }\n console.log('');\n\n var set2 = new Set([2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3]);\n console.log(\"[structure]\\t\\t[test-\" + 3 + \"]\\t\\t[set2.size] = \", set2.size, '\\n');\n\n function getArray() {\n let array = [2, 3, 3]\n return [...array];\n }\n\n var set3 = new Set(getArray());\n console.log(\"[structure]\\t\\t[test-\" + 4 + \"]\\t\\t[set3] = \", set3, '\\n');\n\n var set4 = new Set();\n getArray().forEach(v => set4.add(v));\n console.log(\"[structure]\\t\\t[test-\" + 5 + \"]\\t\\t[set4] = \", set4, '\\n');\n})();\n\n\n/**\n * \u4f7f\u7528\u6280\u5de7\uff1a\n *\n * \u5229\u7528 Set\n * \u53bb\u9664\u6570\u7ec4\u4e2d\u7684\u91cd\u590d\u503c\n * \u7136\u540e\u5f97\u5230\u6570\u7ec4\n */\n\n(() => {\n let array = [2, 3, 3, 3, 3, 3, 3];\n console.log(\"[structure]\\t\\t[test-\" + 6 + \"]\\t\\t[...new Set(array)] = \", [...new Set(array)], '\\n');\n})();\n\n/**\n * Set \u52a0\u5165\u503c\n * \u4e0d\u4f1a\u53d1\u751f\u7c7b\u578b\u8f6c\u6362\n *\n * 2 \u548c \"2\" \u662f\u4e24\u4e2a\u4e0d\u540c\u7684\u503c\n * Set \u5b83\u4eec\u662f\u4e0d\u540c\u7684\n * \u4f7f\u7528\u7684\u7b97\u6cd5\u53eb\u505a Same-value equality\n *\n * \u7c7b\u4f3c\u4e8e\u7cbe\u786e\u76f8\u7b49\u8fd0\u7b97\u7b26\uff08===\uff09\n * \u4e3b\u8981\u7684\u533a\u522b\u662f NaN \u7b49\u4e8e\u81ea\u8eab\uff0c\u800c\u7cbe\u786e\u76f8\u7b49\u8fd0\u7b97\u7b26\u8ba4\u4e3a NaN \u4e0d\u7b49\u4e8e\u81ea\u8eab\n */\n(() => {\n let set = new Set();\n set.add(NaN);\n set.add(NaN);\n console.log(\"[structure]\\t\\t[test-\" + 6 + \"]\\t\\t[set] = \", set);\n set.clear();\n\n set.add(2);\n set.add(\"2\");\n console.log(\"[structure]\\t\\t[test-\" + 6 + \"]\\t\\t[set] = \", set, '\\n');\n set.clear();\n})();\n\n/**\n * \u7a7a\u5bf9\u8c61\u662f\u4e0d\u76f8\u7b49\u7684\n */\n(() => {\n let set = new Set();\n set.add({}).add({}).add({}).add({}).add({}).add({});\n console.log(\"[structure]\\t\\t[test-\" + 7 + \"]\\t\\t[set] = \", set);\n})();"} {"doc_id": "17c2eca03a9f05d0f5fcebb02a60bc6b", "text": "\n/*\nPseudocode - create 3 functions to apply to an array of numbers\n\nCreate a function called 'sum' \n\nsum will have an array of numbers as its argument\n\nsum will add the numbers within that array\n\n--------------------------------------------------\n\nCreate a new function called 'mean'\n\nmean will average all the numbers within the array of numbers\n\n--------------------------------------------------------\n\nCreate a function called 'median'\n\nmedian will find the middle value from the array of numbers. If there is no single middle number, the function should average the 2 middle numbers\n*/\n//User Story 1: As a user, I'll want to sum the numbers in a list of numbers.\n\nvar sum = function(numbers){\n\n var add = 0\n for(var i = 0; i < numbers.length; i++){\n add += numbers[i];\n }\n\n console.log(add);\n}\n\n\nsum([1,2,3,4,5])\n\n\n/*############################################*/\n\n//User Story 2: As a user, I'll want to find the average within a list of numbers.\n\nvar mean = function(numbers){\n\n var answer = 0;\n var totalNumbers = numbers.length;\n var add = 0;\n\n for(var i = 0; i < totalNumbers; i++){\n add += numbers[i];\n }\n\n answer = (add / totalNumbers);\n\n console.log(answer);\n}\n\nmean([1,2,3,5,5])\n\n/*############################################*/\n\n//User Story 3: As a user, I'll want to find the middle number within a list of numbers, or both middle numbers if the list is even.\n\nvar median = function(numbers){\n\n var index = 0;\n var totalNumbers = numbers.length;\n var results = 0;\n\n var results = [];\n\n if (totalNumbers % 2 != 0){\n index = Math.floor(totalNumbers / 2);\n results = numbers[index];\n }\n \n else if (totalNumbers % 2 == 0){\n index = Math.floor(totalNumbers / 2);\n results = (numbers[index] + numbers[index-1]) / 2;\n\n\n\n }\n\n console.log(results);\n\n}\n\nmedian([1, 1, 2, 5, 6, 6, 9])\nmedian([1, 1, 2, 6, 6, 9])\n"} {"doc_id": "17f70b9a8bc7363658008c331ad5b643", "text": "\"use strict\";\n\n/**\n * TODO:\n * Write some JavaScript that uses a `confirm` dialog to ask the user if they\n * would like to enter a number. If they click 'Ok', prompt the user for a\n * number, then use 3 separate alerts to tell the user:\n *\n * - whether the number is even or odd\n * - what the number plus 100 is\n * - if the number is negative or positive\n *\n * if what the user enters is not a number, use an alert to tell them that, and\n * do *not* display any of the above information.\n *\n * Can you refactor your code to use functions?\n */\n\n//\n// function enterNumber(number){\n// confirm(\"would you like to enter a number?\");\n// number = prompt(\"Please enter a number...\");\n//\n// if(number % 2 === 0){\n// alert(\"this number is even\");\n// } else {\n// alert(\"this number is odd\");\n// }\n//\n// alert((number) + \" plus 100 is: \" + (parseInt(number) + 100));\n//\n// if(number > 0){\n// alert(\"this number is positive\");\n// } else {\n// alert(\"this number is negative.\");\n// }\n//\n// return \"thank you for participating!\";\n// }\n//\n// enterNumber();\n/* ########################################################################## */\n\n/**\n * TODO:\n * Create a function named `analyzeColor` that accepts a string that is a color\n * name as input. This function should return a message that related to that\n * color. Only worry about the colors defined above, if the color passed is not\n * one of the ones defined above, return a message that says so\n *\n * Example:\n * > analyzeColor('blue') // returns \"blue is the color of the sky\"\n * > analyzeColor('red') // returns \"Strawberries are red\"\n * > analyzeColor('cyan') // returns \"I don't know anything about cyan\"\n *\n * You should use an if-else-if-else block to return different messages.\n *\n * Test your function by passing various string literals to it and\n * console.logging the function's return value\n */\n\n// function analyzeColor(anyColorString) {\n// anyColorString = prompt(\"please enter a color here...\");\n// switch (anyColorString){\n// case(\"red\"):\n// return \"Strawberries are red\"\n// break;\n// case(\"orange\"):\n// return \"Orange is my favorite color!\"\n// break;\n// case(\"yellow\"):\n// return \"bananas are yellow\"\n// break;\n// case(\"green\"):\n// return \"grass is always greener\"\n// break;\n// case(\"blue\"):\n// return \"the sky is blue\");\n// break;\n// case(\"indigo\"):\n// return \"there is a pokemon indigo league\"\n// break;\n// case(\"violet\"):\n// return \"are violets really violet?\"\n// break;\n// default:\n// return \"I have never heard of that color\"\n// break;\n// }\n// }\n\n// analyzeColor();\n\n\n\n// Don't change the next two lines!\n// These lines create two variables for you:\n// - `colors`: a list of the colors of the rainbow\n// - `randomColor`: contains a single random color value from the list (this\n// will contain a different color everytime the page loads)\nvar colors = ['red', 'orange', 'yellow', 'green', 'blue', 'indigo', 'violet'];\nvar randomColor = colors[Math.floor(Math.random() * colors.length)];\n/**\n * TODO:\n * Pass the `randomColor` variable to your function and console.log the results.\n * You should see a different message everytime you refresh the page\n */\n\n/**\n * TODO:\n * Refactor your above function to use a switch-case statement\n */\n\n/**\n * TODO:\n * Prompt the user for a color when the page loads, and pass the input from the\n * user to your `analyzeColor` function. Alert the return value from your\n * function to show it to the user.\n */\n\n/* ########################################################################## */\n\n/**\n * TODO:\n * Suppose there's a promotion in Walmart, each customer is given a randomly\n * generated \"lucky number\" between 0 and 5. If your lucky number is 0 you have\n * no discount, if your lucky number is 1 you'll get a 10% discount, if it's 2,\n * the discount is 25%, if it's 3, 35%, if it's 4, 50%, and if it's 5 you'll get\n * all for free!.\n *\n * Write a function named `calculateTotal` that accepts a lucky number and total\n * amount, and returns the discounted price.\n *\n * Example:\n * calculateTotal(0, 100) // returns 100\n * calculateTotal(4, 100) // returns 50\n * calculateTotal(5, 100) // returns 0\n *\n * Test your function by passing it various values and checking for the expected\n * return value.\n */\n\n// var luckyNumber = Math.floor(Math.random()* 6)\n// var pay = 60\n// var total;\n//\n// switch (luckyNumber) {\n// case \"1\":\n// total = .9 * pay;\n// break;\n// case \"2\":\n// total = pay * .75;\n// break;\n// case \"4\":\n// total = pay * .5;\n// break;\n// case \"5\":\n// total = 0;\n// break;\n// default:\n// total = pay;\n// break;\n// }\n//\n// console.log(\"your total pay is \" + total);\n\n\n/**\n * TODO:\n * Uncomment the line below to generate a random number between 0 and 6.\n * Prompt the user for their total bill, then use your `calculateTotal` function\n * and alerts to display to the user what their lucky number was, what their\n * price before the discount was, and what their price after the discount is.\n */\n// Generate a random number between 0 and 6\n// var luckyNumber = Math.floor(Math.random() * 6);\n\n\n\n/*bonuses\n===============================================================\n */\n\n// write a function (or multiple functions) that will return\n// a boolean depending on if the string value passed to it\n// as an argument is the name of a day of the week that starts with a T.\n// Otherwise, it should return false.\n\n// Example: dayOfTheWeekStartsWithT(\"Monday\") => returns false\n// Example: dayOfTheWeekStartsWithT(\"Tuesday\") => returns true\n// Example: dayOfTheWeekStartsWithT(\"Tommy\") => returns false\nfunction dayOfTheWeekStartsWithT(day) {\n day = prompt(\"What day starts with T?\");\n switch (day){\n case \"Monday\":\n alert(\"false\");\n break;\n case \"Tuesday\":\n alert(\"true\");\n break;\n case \"Wednesday\":\n alert(\"false\");\n break;\n case \"Thursday\":\n alert(\"true\");\n break;\n case \"Friday\":\n alert(\"false\");\n break;\n default:\n alert(\"false\");\n }\n}\n\nconsole.log(dayOfTheWeekStartsWithT());\n\n/*bonus 2\n===============================================================================\n */\n// Write a function, isValidPassword, that takes in a string argument and returns true or false\n// depending on whether or not all the following conditions are true:\n// 1) Must be 6 characters long (only for the sake of this exercise, NOT a best practice)\n// 2) Contains at least one letter and one number\n// 3) Contains at least one upper and one lower case letter\n// 4) Only comprised of letters and numbers\n// 5) EXTRA BONUS: is not the same forwards and backwards\n\n\nfunction isValidPassword(password) {\n\n\n}\n\n"} {"doc_id": "17fc0b04921459c8f8acee5abc6f8b13", "text": "/*\n
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided input is the start and end coordinates of the horizontal diameter. Since it's horizontal, y-coordinates don't matter and hence the x-coordinates of start and end of the diameter suffice. Start is always smaller than end. There will be at most 104 balloons.
\n\nAn arrow can be shot up exactly vertically from different points along the x-axis. A balloon with xstart and xend bursts by an arrow shot at x if xstart ≤ x ≤ xend. There is no limit to the number of arrows that can be shot. An arrow once shot keeps travelling up infinitely. The problem is to find the minimum number of arrows that must be shot to burst all balloons.
\n\nExample:\n
\nInput:\n[[10,16], [2,8], [1,6], [7,12]]\n\nOutput:\n2\n\nExplanation:\nOne way is to shoot one arrow for example at x = 6 (bursting the balloons [2,8] and [1,6]) and another arrow at x = 11 (bursting the other two balloons).\n\n
\u5728\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\u6709\u8bb8\u591a\u7403\u5f62\u7684\u6c14\u7403\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u6c14\u7403\uff0c\u63d0\u4f9b\u7684\u8f93\u5165\u662f\u6c34\u5e73\u65b9\u5411\u4e0a\uff0c\u6c14\u7403\u76f4\u5f84\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u5750\u6807\u3002\u7531\u4e8e\u5b83\u662f\u6c34\u5e73\u7684\uff0c\u6240\u4ee5y\u5750\u6807\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u6b64\u53ea\u8981\u77e5\u9053\u5f00\u59cb\u548c\u7ed3\u675f\u7684x\u5750\u6807\u5c31\u8db3\u591f\u4e86\u3002\u5f00\u59cb\u5750\u6807\u603b\u662f\u5c0f\u4e8e\u7ed3\u675f\u5750\u6807\u3002\u5e73\u9762\u5185\u6700\u591a\u5b58\u5728104\u4e2a\u6c14\u7403\u3002
\n\n\u4e00\u652f\u5f13\u7bad\u53ef\u4ee5\u6cbf\u7740x\u8f74\u4ece\u4e0d\u540c\u70b9\u5b8c\u5168\u5782\u76f4\u5730\u5c04\u51fa\u3002\u5728\u5750\u6807x\u5904\u5c04\u51fa\u4e00\u652f\u7bad\uff0c\u82e5\u6709\u4e00\u4e2a\u6c14\u7403\u7684\u76f4\u5f84\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u5750\u6807\u4e3a xstart\uff0cxend\uff0c \u4e14\u6ee1\u8db3 xstart ≤ x ≤ xend\uff0c\u5219\u8be5\u6c14\u7403\u4f1a\u88ab\u5f15\u7206\u3002\u53ef\u4ee5\u5c04\u51fa\u7684\u5f13\u7bad\u7684\u6570\u91cf\u6ca1\u6709\u9650\u5236\u3002 \u5f13\u7bad\u4e00\u65e6\u88ab\u5c04\u51fa\u4e4b\u540e\uff0c\u53ef\u4ee5\u65e0\u9650\u5730\u524d\u8fdb\u3002\u6211\u4eec\u60f3\u627e\u5230\u4f7f\u5f97\u6240\u6709\u6c14\u7403\u5168\u90e8\u88ab\u5f15\u7206\uff0c\u6240\u9700\u7684\u5f13\u7bad\u7684\u6700\u5c0f\u6570\u91cf\u3002
\n\nExample:
\n\n\n\u8f93\u5165:\n[[10,16], [2,8], [1,6], [7,12]]\n\n\u8f93\u51fa:\n2\n\n\u89e3\u91ca:\n\u5bf9\u4e8e\u8be5\u6837\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728x = 6\uff08\u5c04\u7206[2,8],[1,6]\u4e24\u4e2a\u6c14\u7403\uff09\u548c x = 11\uff08\u5c04\u7206\u53e6\u5916\u4e24\u4e2a\u6c14\u7403\uff09\u3002\n\n
\u5728\u4e8c\u7ef4\u7a7a\u95f4\u4e2d\u6709\u8bb8\u591a\u7403\u5f62\u7684\u6c14\u7403\u3002\u5bf9\u4e8e\u6bcf\u4e2a\u6c14\u7403\uff0c\u63d0\u4f9b\u7684\u8f93\u5165\u662f\u6c34\u5e73\u65b9\u5411\u4e0a\uff0c\u6c14\u7403\u76f4\u5f84\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u5750\u6807\u3002\u7531\u4e8e\u5b83\u662f\u6c34\u5e73\u7684\uff0c\u6240\u4ee5y\u5750\u6807\u5e76\u4e0d\u91cd\u8981\uff0c\u56e0\u6b64\u53ea\u8981\u77e5\u9053\u5f00\u59cb\u548c\u7ed3\u675f\u7684x\u5750\u6807\u5c31\u8db3\u591f\u4e86\u3002\u5f00\u59cb\u5750\u6807\u603b\u662f\u5c0f\u4e8e\u7ed3\u675f\u5750\u6807\u3002\u5e73\u9762\u5185\u6700\u591a\u5b58\u5728104\u4e2a\u6c14\u7403\u3002
\n\n\u4e00\u652f\u5f13\u7bad\u53ef\u4ee5\u6cbf\u7740x\u8f74\u4ece\u4e0d\u540c\u70b9\u5b8c\u5168\u5782\u76f4\u5730\u5c04\u51fa\u3002\u5728\u5750\u6807x\u5904\u5c04\u51fa\u4e00\u652f\u7bad\uff0c\u82e5\u6709\u4e00\u4e2a\u6c14\u7403\u7684\u76f4\u5f84\u7684\u5f00\u59cb\u548c\u7ed3\u675f\u5750\u6807\u4e3a xstart\uff0cxend\uff0c \u4e14\u6ee1\u8db3 xstart ≤ x ≤ xend\uff0c\u5219\u8be5\u6c14\u7403\u4f1a\u88ab\u5f15\u7206\u3002\u53ef\u4ee5\u5c04\u51fa\u7684\u5f13\u7bad\u7684\u6570\u91cf\u6ca1\u6709\u9650\u5236\u3002 \u5f13\u7bad\u4e00\u65e6\u88ab\u5c04\u51fa\u4e4b\u540e\uff0c\u53ef\u4ee5\u65e0\u9650\u5730\u524d\u8fdb\u3002\u6211\u4eec\u60f3\u627e\u5230\u4f7f\u5f97\u6240\u6709\u6c14\u7403\u5168\u90e8\u88ab\u5f15\u7206\uff0c\u6240\u9700\u7684\u5f13\u7bad\u7684\u6700\u5c0f\u6570\u91cf\u3002
\n\nExample:
\n\n\n\u8f93\u5165:\n[[10,16], [2,8], [1,6], [7,12]]\n\n\u8f93\u51fa:\n2\n\n\u89e3\u91ca:\n\u5bf9\u4e8e\u8be5\u6837\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5728x = 6\uff08\u5c04\u7206[2,8],[1,6]\u4e24\u4e2a\u6c14\u7403\uff09\u548c x = 11\uff08\u5c04\u7206\u53e6\u5916\u4e24\u4e2a\u6c14\u7403\uff09\u3002\n\n*/\n\n\n/**\n * @param {number[][]} points\n * @return {number}\n */\nvar findMinArrowShots = function(points) {\n \n};"} {"doc_id": "18685ec2ff3e77d260df567826bf4bfc", "text": "var numeroSecreto = parseInt(Math.random() * 11);\n//var numeroLimite = 10;\n\nfunction Chutar() {\n var chute = parseInt(document.getElementById(\"valor\").value);\n\n var elementoResultado = document.getElementById(\"resultado\");\n\n //console.log(chute);\n\n if (chute == numeroSecreto) {\n // console.log(\"acertou\");\n\n elementoResultado.innerHTML = \"Acertou, mizeravi!\";\n } else if (chute > 10 || chute < 0) {\n //console.log(\"Digitar um n\u00famero entre 0 a 10 voc\u00ea deve\");\n\n elementoResultado.innerHTML = \"Digitar um n\u00famero entre 0 a 10 voc\u00ea deve\";\n } else {\n //console.log(\"Errowwwww! O n\u00famero secreto era \" + numeroSecreto);\n\n elementoResultado.innerHTML =\n \"Errowwwww! O n\u00famero secreto era \" + numeroSecreto;\n }\n}"} {"doc_id": "18edb35dd48c7ef6ca240a92fad46804", "text": "/*\nA valid parentheses string is either empty (\"\"), \"(\" + A + \")\", or A + B, where A and B are valid parentheses strings, and + represents string concatenation. For example, \"\", \"()\", \"(())()\", and \"(()(()))\" are all valid parentheses strings.\n\nA valid parentheses string S is primitive if it is nonempty, and there does not exist a way to split it into S = A+B, with A and B nonempty valid parentheses strings.\n\nGiven a valid parentheses string S, consider its primitive decomposition: S = P_1 + P_2 + ... + P_k, where P_i are primitive valid parentheses strings.\n\nReturn S after removing the outermost parentheses of every primitive string in the primitive decomposition of S.\n\n \n\nExample 1:\n\nInput: \"(()())(())\"\nOutput: \"()()()\"\nExplanation: \nThe input string is \"(()())(())\", with primitive decomposition \"(()())\" + \"(())\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" = \"()()()\".\nExample 2:\n\nInput: \"(()())(())(()(()))\"\nOutput: \"()()()()(())\"\nExplanation: \nThe input string is \"(()())(())(()(()))\", with primitive decomposition \"(()())\" + \"(())\" + \"(()(()))\".\nAfter removing outer parentheses of each part, this is \"()()\" + \"()\" + \"()(())\" = \"()()()()(())\".\nExample 3:\n\nInput: \"()()\"\nOutput: \"\"\nExplanation: \nThe input string is \"()()\", with primitive decomposition \"()\" + \"()\".\nAfter removing outer parentheses of each part, this is \"\" + \"\" = \"\".\n \n\nNote:\n\nS.length <= 10000\nS[i] is \"(\" or \")\"\nS is a valid parentheses string\n*/\nvar removeOuterParentheses = function(S) {\n let parenthesCount = 0;\n let result = \"\";\n for (const letter of S) {\n if (letter === \"(\") {\n if (parenthesCount) {\n result += letter;\n }\n parenthesCount++;\n } else {\n parenthesCount--;\n if (parenthesCount) {\n result += letter;\n }\n }\n }\n \n return result;\n};\n"} {"doc_id": "196e912802fadab9879e7219881e5947", "text": "/*----------UNDERSTANDING THE PROBLEM----------\n- Expected Input(s):\n - Number of Inputs: 2\n - Types (respectively): Number (an integer), Array\n - Names/Values (respectively): 'k', 'numbers'\n\n- Expected Output(s):\n - Number of Outputs: 1 (with 2 possibilities)\n - Type: String\n - Names/Values (respectively): 'Yes', 'No'\n\n- Constraints:\n - Each element in the array occurs in EXACTLY one subsequence.\n - This means that the elements within a subsequence are UNIQUE.\n - All the numbers in a subsequence are distinct.\n - Elements in the array having the same value must be in different\n subsequences.\n - ^All of these statements are essentially saying the same thing.\n - 1 <= n <= 100,000 (n = the length of the given array)\n - 1 <= k <= n (k = the number of items required in the subsequence)\n - 1 <= numbers[i] <= 100,000 (the value of an item in the array)\n\n- Examples:\n - Sample Input #1: 2, [3, 5, 3, 2]\n - Sample Output #1: Yes\n - Explanation #1:\n - The array is [3, 5, 3, 2]. One solution is to partition into 2\n subsequences - [3, 2] and [5, 3]. So the answer is 'Yes'.\n\n - Sample Input #2: 3, [4, 8, 8, 8, 6, 4]\n - Sample Output #2: No\n - Explanation #2:\n - Element 8 occurs thrice in the array. There is no possible way to\n partition into subsequences of length 3, such that 8 occurs at most\n once in each subsequence. So the answer is 'No'.\n\n - Sample Input #3: 2, [1, 1, 1, 3, 4, 5]\n - Sample Output #3: No\n - Explanation #2:\n - Element 1 occurs thrice in the array. There is no possible way to\n partition into subsequences of length 3, such that 1 occurs at most\n once in each subsequence. So the answer is 'No'.\n */\n\n/*----------DEVISING A PLAN----------\n- BRUTE FORCE SOLUTION:\n\n 1. Create a function, 'partitionArray()', that takes in 2 parameters,\n 'k' (an integer) and 'numbers' (an array), and returns either the\n string 'Yes' or the string 'No' based on whether or not the given\n 'numbers' array can be partitioned into subsequences where the\n length of each subsequence is equal to 'k' and each item in the\n subsequent subsequences is a distinctly unique integer.\n\n 2. Create a variable with 'let' called 'lenOfNums', which denotes the\n length of the given input array, 'numbers'.\n\n 3. Create a variable with 'const' called 'cache', which denotes the\n frequency of each occuring item in the given input array. \n\n 4. Use an 'if/else' statement to evaluate whether or not 'lenOfArr' \n can be partitioned evenly into subsequences with a length of 'k'.\n\n a. If 'lenOfArr' can be partitioned evenly into subsequences with\n a length of 'k', use a 'for' loop and a cache to determine the\n frequency of each item in the given array, storing the item\n itself as a 'key' and the number of occurrences as the 'value' \n of a key/value pair in the cache.\n\n b. If 'lenOfArr' CANNOT be partitioned evenly into subsequence\n with a length of 'k', do nothing.\n \n 5. Use a separate 'for' loop that will iterate through the keys of the\n cache object to determine if any associated values are > 1.\n\n a. If a key's value IS greater than 1, do the following:\n\n i. Evaluate whether or not the multiple occurrences of the\n key in quesion are AT LEAST 'k' indices apart in the\n given array.\n\n 1. If the occurrence in question ARE at least 'k' \n indices apart in the original given input array,\n return 'Yes'.\n\n 2. If the occurrence in question is NOT at least 'k'\n indices apart in the original given input array,\n return 'No'.\n\n b. If a key's value is NOT greater than 1, do nothing.\n\n 6. Return 'No'.\n */\n\n/*----------IMPLEMENTING THE PLAN----------*/\n\nconst partitionArray = (k, numbers) => {\n let lenOfNums = numbers.length;\n const cache = {};\n\n if (lenOfNums % k == 0) {\n for (let i = 0; i < lenOfNums; i++) {\n // console.log(`i = ${i}; numbers[${i}] = ${numbers[i]}`);\n if (cache[numbers[i]]) {\n console.log(`${numbers[i]} ALREADY existed in the cache!`);\n cache[numbers[i]] += 1;\n console.log(cache);\n } else {\n console.log(`${numbers[i]} was just added to the cache!`);\n cache[numbers[i]] = 1;\n console.log(cache);\n }\n }\n\n for (let key = 0; key < cache.length; i++) {\n console.log(`cache[key] = ${cache[key]}`);\n if (cache[key] > 1) {\n for (let j = 0; j < lenOfNums; j++) {\n if (key == numbers[j]) {\n console.log(\n `The key, ${key}, has duplicates within the same subsquence!`\n );\n if (key == numbers[j + k - 1]) {\n return 'No';\n }\n } else {\n console.log(\n `The key, ${key}, DOES NOT have any duplicates within the same subsquence!`\n );\n return 'Yes';\n }\n }\n } else {\n console.log(`\\nEach key is a DISTINCT integer in the given array!`);\n console.log(cache);\n return 'Yes';\n }\n }\n } else {\n console.log(\n `The given input array has a length that is not evenly divisible by 'k'.`\n );\n }\n return 'No';\n};\n\nconsole.log(partitionArray(2, [1, 2, 3, 4, 5, 6])); // should return 'Yes'\nconsole.log(partitionArray(3, [4, 8, 8, 8, 6])); // should return 'No'\nconsole.log(partitionArray(3, [4, 8, 8, 8, 6, 7])); // should return 'No'\nconsole.log(partitionArray(2, [1, 2, 3, 1, 4, 1])); // should return 'Yes'\n\n/*----------REFLECTING/ITERATING ON THE PLAN----------\n\n */\n"} {"doc_id": "198c1602cc0fcfa85ca6388b9042a045", "text": "/*\n
Given a binary tree
\n\n\nstruct TreeLinkNode {\n TreeLinkNode *left;\n TreeLinkNode *right;\n TreeLinkNode *next;\n}\n\n\nPopulate each next pointer to point to its next right node. If there is no next right node, the next pointer should be set to NULL.
Initially, all next pointers are set to NULL.
Note:
\n\nExample:
\n\nGiven the following perfect binary tree,
\n\n\n 1\n / \\\n 2 3\n / \\ / \\\n4 5 6 7\n\n\n
After calling your function, the tree should look like:
\n\n\n 1 -> NULL\n / \\\n 2 -> 3 -> NULL\n / \\ / \\\n4->5->6->7 -> NULL\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811
\n\nstruct TreeLinkNode {\n TreeLinkNode *left;\n TreeLinkNode *right;\n TreeLinkNode *next;\n}\n\n\u586b\u5145\u5b83\u7684\u6bcf\u4e2a next \u6307\u9488\uff0c\u8ba9\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u5176\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u3002\u5982\u679c\u627e\u4e0d\u5230\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\uff0c\u5219\u5c06 next \u6307\u9488\u8bbe\u7f6e\u4e3a NULL\u3002
\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709 next \u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a NULL\u3002
\u8bf4\u660e:
\n\n\u793a\u4f8b:
\n\n\u7ed9\u5b9a\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c
\n\n1\n / \\\n 2 3\n / \\ / \\\n4 5 6 7\n\n
\u8c03\u7528\u4f60\u7684\u51fd\u6570\u540e\uff0c\u8be5\u5b8c\u7f8e\u4e8c\u53c9\u6811\u53d8\u4e3a\uff1a
\n\n1 -> NULL\n / \\\n 2 -> 3 -> NULL\n / \\ / \\\n4->5->6->7 -> NULL\n
\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u6811
\n\nstruct TreeLinkNode {\n TreeLinkNode *left;\n TreeLinkNode *right;\n TreeLinkNode *next;\n}\n\n\u586b\u5145\u5b83\u7684\u6bcf\u4e2a next \u6307\u9488\uff0c\u8ba9\u8fd9\u4e2a\u6307\u9488\u6307\u5411\u5176\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\u3002\u5982\u679c\u627e\u4e0d\u5230\u4e0b\u4e00\u4e2a\u53f3\u4fa7\u8282\u70b9\uff0c\u5219\u5c06 next \u6307\u9488\u8bbe\u7f6e\u4e3a NULL\u3002
\u521d\u59cb\u72b6\u6001\u4e0b\uff0c\u6240\u6709 next \u6307\u9488\u90fd\u88ab\u8bbe\u7f6e\u4e3a NULL\u3002
\u8bf4\u660e:
\n\n\u793a\u4f8b:
\n\n\u7ed9\u5b9a\u5b8c\u7f8e\u4e8c\u53c9\u6811\uff0c
\n\n1\n / \\\n 2 3\n / \\ / \\\n4 5 6 7\n\n
\u8c03\u7528\u4f60\u7684\u51fd\u6570\u540e\uff0c\u8be5\u5b8c\u7f8e\u4e8c\u53c9\u6811\u53d8\u4e3a\uff1a
\n\n1 -> NULL\n / \\\n 2 -> 3 -> NULL\n / \\ / \\\n4->5->6->7 -> NULL\n*/\n\n\n/**\n * Definition for binary tree with next pointer.\n * function TreeLinkNode(val) {\n * this.val = val;\n * this.left = this.right = this.next = null;\n * }\n */\n\n/**\n * @param {TreeLinkNode} root\n * @return {void} Do not return anything, modify tree in-place instead.\n */\nvar connect = function(root) {\n \n};"} {"doc_id": "1a16aa04d58de2ba3a062a54f51b6b60", "text": "\"use strict\";\n// Getting Started\n/* function foo(bar: string) {\n return 'Hello, ' + bar\n}\n\nlet baz = 'Alireza'\n\nconsole.log(foo(baz)) */\n/*\n// Common Types\nlet foo: string\nlet bar: boolean\nlet baz: number\nlet qux: string[]\nlet quuz: [number, string]\nlet corge: { [key: number]: string }\nlet grault: Set
Say you have an array for which the ith element is the price of a given stock on day i.
\n\nDesign an algorithm to find the maximum profit. You may complete at most two transactions.
\n\nNote: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
\n\nExample 1:
\n\n\nInput: [3,3,5,0,0,3,1,4]\nOutput: 6\nExplanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.\n Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.\n\n
Example 2:
\n\n\nInput: [1,2,3,4,5]\nOutput: 4\nExplanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.\n Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are\n engaging multiple transactions at the same time. You must sell before buying again.\n\n\n
Example 3:
\n\n\nInput: [7,6,4,3,1]\nOutput: 0\nExplanation: In this case, no transaction is done, i.e. max profit = 0.\n
\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5b83\u7684\u7b2c i \u4e2a\u5143\u7d20\u662f\u4e00\u652f\u7ed9\u5b9a\u7684\u80a1\u7968\u5728\u7b2c i \u5929\u7684\u4ef7\u683c\u3002
\n\n\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002\u4f60\u6700\u591a\u53ef\u4ee5\u5b8c\u6210 \u4e24\u7b14 \u4ea4\u6613\u3002
\n\n\u6ce8\u610f: \u4f60\u4e0d\u80fd\u540c\u65f6\u53c2\u4e0e\u591a\u7b14\u4ea4\u6613\uff08\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\uff09\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: [3,3,5,0,0,3,1,4]\n\u8f93\u51fa: 6\n\u89e3\u91ca: \u5728\u7b2c 4 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 0\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 6 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 3\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 3-0 = 3 \u3002\n \u968f\u540e\uff0c\u5728\u7b2c 7 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 8 \u5929 \uff08\u80a1\u7968\u4ef7\u683c = 4\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 4-1 = 3 \u3002\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: [1,2,3,4,5]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u5728\u7b2c 1 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 5 \u5929 \uff08\u80a1\u7968\u4ef7\u683c = 5\uff09\u7684\u65f6\u5019\u5356\u51fa, \u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 5-1 = 4 \u3002 \n \u6ce8\u610f\u4f60\u4e0d\u80fd\u5728\u7b2c 1 \u5929\u548c\u7b2c 2 \u5929\u63a5\u8fde\u8d2d\u4e70\u80a1\u7968\uff0c\u4e4b\u540e\u518d\u5c06\u5b83\u4eec\u5356\u51fa\u3002 \n \u56e0\u4e3a\u8fd9\u6837\u5c5e\u4e8e\u540c\u65f6\u53c2\u4e0e\u4e86\u591a\u7b14\u4ea4\u6613\uff0c\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\u3002\n\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: [7,6,4,3,1] \n\u8f93\u51fa: 0 \n\u89e3\u91ca: \u5728\u8fd9\u4e2a\u60c5\u51b5\u4e0b, \u6ca1\u6709\u4ea4\u6613\u5b8c\u6210, \u6240\u4ee5\u6700\u5927\u5229\u6da6\u4e3a 0\u3002\n
\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5b83\u7684\u7b2c i \u4e2a\u5143\u7d20\u662f\u4e00\u652f\u7ed9\u5b9a\u7684\u80a1\u7968\u5728\u7b2c i \u5929\u7684\u4ef7\u683c\u3002
\n\n\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002\u4f60\u6700\u591a\u53ef\u4ee5\u5b8c\u6210 \u4e24\u7b14 \u4ea4\u6613\u3002
\n\n\u6ce8\u610f: \u4f60\u4e0d\u80fd\u540c\u65f6\u53c2\u4e0e\u591a\u7b14\u4ea4\u6613\uff08\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\uff09\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: [3,3,5,0,0,3,1,4]\n\u8f93\u51fa: 6\n\u89e3\u91ca: \u5728\u7b2c 4 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 0\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 6 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 3\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 3-0 = 3 \u3002\n \u968f\u540e\uff0c\u5728\u7b2c 7 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 8 \u5929 \uff08\u80a1\u7968\u4ef7\u683c = 4\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 4-1 = 3 \u3002\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: [1,2,3,4,5]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u5728\u7b2c 1 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 5 \u5929 \uff08\u80a1\u7968\u4ef7\u683c = 5\uff09\u7684\u65f6\u5019\u5356\u51fa, \u8fd9\u7b14\u4ea4\u6613\u6240\u80fd\u83b7\u5f97\u5229\u6da6 = 5-1 = 4 \u3002 \n \u6ce8\u610f\u4f60\u4e0d\u80fd\u5728\u7b2c 1 \u5929\u548c\u7b2c 2 \u5929\u63a5\u8fde\u8d2d\u4e70\u80a1\u7968\uff0c\u4e4b\u540e\u518d\u5c06\u5b83\u4eec\u5356\u51fa\u3002 \n \u56e0\u4e3a\u8fd9\u6837\u5c5e\u4e8e\u540c\u65f6\u53c2\u4e0e\u4e86\u591a\u7b14\u4ea4\u6613\uff0c\u4f60\u5fc5\u987b\u5728\u518d\u6b21\u8d2d\u4e70\u524d\u51fa\u552e\u6389\u4e4b\u524d\u7684\u80a1\u7968\u3002\n\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: [7,6,4,3,1] \n\u8f93\u51fa: 0 \n\u89e3\u91ca: \u5728\u8fd9\u4e2a\u60c5\u51b5\u4e0b, \u6ca1\u6709\u4ea4\u6613\u5b8c\u6210, \u6240\u4ee5\u6700\u5927\u5229\u6da6\u4e3a 0\u3002\n*/\n\n\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n \n};"} {"doc_id": "1da7cb263670719bcea288b07867b8a7", "text": "/*\n
We are given two strings, A and B.
A shift on A consists of taking string A and moving the leftmost character to the rightmost position. For example, if A = 'abcde', then it will be 'bcdea' after one shift on A. Return True if and only if A can become B after some number of shifts on A.
\nExample 1:\nInput: A = 'abcde', B = 'cdeab'\nOutput: true\n\nExample 2:\nInput: A = 'abcde', B = 'abced'\nOutput: false\n\n\n
Note:
\n\nA and B will have length at most 100.\u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32, A \u548c B\u3002
A \u7684\u65cb\u8f6c\u64cd\u4f5c\u5c31\u662f\u5c06 A \u6700\u5de6\u8fb9\u7684\u5b57\u7b26\u79fb\u52a8\u5230\u6700\u53f3\u8fb9\u3002 \u4f8b\u5982, \u82e5 A = 'abcde'\uff0c\u5728\u79fb\u52a8\u4e00\u6b21\u4e4b\u540e\u7ed3\u679c\u5c31\u662f'bcdea' \u3002\u5982\u679c\u5728\u82e5\u5e72\u6b21\u65cb\u8f6c\u64cd\u4f5c\u4e4b\u540e\uff0cA \u80fd\u53d8\u6210B\uff0c\u90a3\u4e48\u8fd4\u56deTrue\u3002
\n\u793a\u4f8b 1:\n\u8f93\u5165: A = 'abcde', B = 'cdeab'\n\u8f93\u51fa: true\n\n\u793a\u4f8b 2:\n\u8f93\u5165: A = 'abcde', B = 'abced'\n\u8f93\u51fa: false\n\n
\u6ce8\u610f\uff1a
\n\nA \u548c B \u957f\u5ea6\u4e0d\u8d85\u8fc7 100\u3002\u7ed9\u5b9a\u4e24\u4e2a\u5b57\u7b26\u4e32, A \u548c B\u3002
A \u7684\u65cb\u8f6c\u64cd\u4f5c\u5c31\u662f\u5c06 A \u6700\u5de6\u8fb9\u7684\u5b57\u7b26\u79fb\u52a8\u5230\u6700\u53f3\u8fb9\u3002 \u4f8b\u5982, \u82e5 A = 'abcde'\uff0c\u5728\u79fb\u52a8\u4e00\u6b21\u4e4b\u540e\u7ed3\u679c\u5c31\u662f'bcdea' \u3002\u5982\u679c\u5728\u82e5\u5e72\u6b21\u65cb\u8f6c\u64cd\u4f5c\u4e4b\u540e\uff0cA \u80fd\u53d8\u6210B\uff0c\u90a3\u4e48\u8fd4\u56deTrue\u3002
\n\u793a\u4f8b 1:\n\u8f93\u5165: A = 'abcde', B = 'cdeab'\n\u8f93\u51fa: true\n\n\u793a\u4f8b 2:\n\u8f93\u5165: A = 'abcde', B = 'abced'\n\u8f93\u51fa: false\n\n
\u6ce8\u610f\uff1a
\n\nA \u548c B \u957f\u5ea6\u4e0d\u8d85\u8fc7 100\u3002The Hamming distance between two integers is the number of positions at which the corresponding bits are different.
\n\nNow your job is to find the total Hamming distance between all pairs of the given numbers.
\n\n\nExample:
\n
\nInput: 4, 14, 2\n\nOutput: 6\n\nExplanation: In binary representation, the 4 is 0100, 14 is 1110, and 2 is 0010 (just\nshowing the four bits relevant in this case). So the answer will be:\nHammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.\n\n\n\n
Note:
\n
0 to 10^9\n10^4. \u4e24\u4e2a\u6574\u6570\u7684 \u6c49\u660e\u8ddd\u79bb \u6307\u7684\u662f\u8fd9\u4e24\u4e2a\u6570\u5b57\u7684\u4e8c\u8fdb\u5236\u6570\u5bf9\u5e94\u4f4d\u4e0d\u540c\u7684\u6570\u91cf\u3002
\n\n\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4efb\u610f\u4e24\u4e2a\u6570\u4e4b\u95f4\u6c49\u660e\u8ddd\u79bb\u7684\u603b\u548c\u3002
\n\n\u793a\u4f8b:
\n\n\n\u8f93\u5165: 4, 14, 2\n\n\u8f93\u51fa: 6\n\n\u89e3\u91ca: \u5728\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d\uff0c4\u8868\u793a\u4e3a0100\uff0c14\u8868\u793a\u4e3a1110\uff0c2\u8868\u793a\u4e3a0010\u3002\uff08\u8fd9\u6837\u8868\u793a\u662f\u4e3a\u4e86\u4f53\u73b0\u540e\u56db\u4f4d\u4e4b\u95f4\u5173\u7cfb\uff09\n\u6240\u4ee5\u7b54\u6848\u4e3a\uff1a\nHammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.\n\n\n
\u6ce8\u610f:
\n\n0\u5230 10^9\u300210^4\u3002\u4e24\u4e2a\u6574\u6570\u7684 \u6c49\u660e\u8ddd\u79bb \u6307\u7684\u662f\u8fd9\u4e24\u4e2a\u6570\u5b57\u7684\u4e8c\u8fdb\u5236\u6570\u5bf9\u5e94\u4f4d\u4e0d\u540c\u7684\u6570\u91cf\u3002
\n\n\u8ba1\u7b97\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff0c\u4efb\u610f\u4e24\u4e2a\u6570\u4e4b\u95f4\u6c49\u660e\u8ddd\u79bb\u7684\u603b\u548c\u3002
\n\n\u793a\u4f8b:
\n\n\n\u8f93\u5165: 4, 14, 2\n\n\u8f93\u51fa: 6\n\n\u89e3\u91ca: \u5728\u4e8c\u8fdb\u5236\u8868\u793a\u4e2d\uff0c4\u8868\u793a\u4e3a0100\uff0c14\u8868\u793a\u4e3a1110\uff0c2\u8868\u793a\u4e3a0010\u3002\uff08\u8fd9\u6837\u8868\u793a\u662f\u4e3a\u4e86\u4f53\u73b0\u540e\u56db\u4f4d\u4e4b\u95f4\u5173\u7cfb\uff09\n\u6240\u4ee5\u7b54\u6848\u4e3a\uff1a\nHammingDistance(4, 14) + HammingDistance(4, 2) + HammingDistance(14, 2) = 2 + 2 + 2 = 6.\n\n\n
\u6ce8\u610f:
\n\n0\u5230 10^9\u300210^4\u3002\nGiven an array of integers and an integer k, you need to find the number of unique k-diff pairs in the array. Here a k-diff pair is defined as an integer pair (i, j), where i and j are both numbers in the array and their absolute difference is k.\n
\n\n\nExample 1:
\n
\nInput: [3, 1, 4, 1, 5], k = 2\nOutput: 2\nExplanation: There are two 2-diff pairs in the array, (1, 3) and (3, 5).Although we have two 1s in the input, we should only return the number of unique pairs.\n\n\n\n
Example 2:
\n
\nInput:[1, 2, 3, 4, 5], k = 1\nOutput: 4\nExplanation: There are four 1-diff pairs in the array, (1, 2), (2, 3), (3, 4) and (4, 5).\n\n\n\n
Example 3:
\n
\nInput: [1, 3, 1, 5, 4], k = 0\nOutput: 1\nExplanation: There is one 0-diff pair in the array, (1, 1).\n\n\n\n
Note:
\n
\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u548c\u4e00\u4e2a\u6574\u6570 k, \u4f60\u9700\u8981\u5728\u6570\u7ec4\u91cc\u627e\u5230\u4e0d\u540c\u7684 k-diff \u6570\u5bf9\u3002\u8fd9\u91cc\u5c06 k-diff \u6570\u5bf9\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u6574\u6570\u5bf9 (i, j), \u5176\u4e2d i \u548c j \u90fd\u662f\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\uff0c\u4e14\u4e24\u6570\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u662f k.
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: [3, 1, 4, 1, 5], k = 2\n\u8f93\u51fa: 2\n\u89e3\u91ca: \u6570\u7ec4\u4e2d\u6709\u4e24\u4e2a 2-diff \u6570\u5bf9, (1, 3) \u548c (3, 5)\u3002\n\u5c3d\u7ba1\u6570\u7ec4\u4e2d\u6709\u4e24\u4e2a1\uff0c\u4f46\u6211\u4eec\u53ea\u5e94\u8fd4\u56de\u4e0d\u540c\u7684\u6570\u5bf9\u7684\u6570\u91cf\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165:[1, 2, 3, 4, 5], k = 1\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u6570\u7ec4\u4e2d\u6709\u56db\u4e2a 1-diff \u6570\u5bf9, (1, 2), (2, 3), (3, 4) \u548c (4, 5)\u3002\n\n\n
\u793a\u4f8b 3:
\n\n\n\u8f93\u5165: [1, 3, 1, 5, 4], k = 0\n\u8f93\u51fa: 1\n\u89e3\u91ca: \u6570\u7ec4\u4e2d\u53ea\u6709\u4e00\u4e2a 0-diff \u6570\u5bf9\uff0c(1, 1)\u3002\n\n\n
\u6ce8\u610f:
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u6570\u7ec4\u548c\u4e00\u4e2a\u6574\u6570 k, \u4f60\u9700\u8981\u5728\u6570\u7ec4\u91cc\u627e\u5230\u4e0d\u540c\u7684 k-diff \u6570\u5bf9\u3002\u8fd9\u91cc\u5c06 k-diff \u6570\u5bf9\u5b9a\u4e49\u4e3a\u4e00\u4e2a\u6574\u6570\u5bf9 (i, j), \u5176\u4e2d i \u548c j \u90fd\u662f\u6570\u7ec4\u4e2d\u7684\u6570\u5b57\uff0c\u4e14\u4e24\u6570\u4e4b\u5dee\u7684\u7edd\u5bf9\u503c\u662f k.
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: [3, 1, 4, 1, 5], k = 2\n\u8f93\u51fa: 2\n\u89e3\u91ca: \u6570\u7ec4\u4e2d\u6709\u4e24\u4e2a 2-diff \u6570\u5bf9, (1, 3) \u548c (3, 5)\u3002\n\u5c3d\u7ba1\u6570\u7ec4\u4e2d\u6709\u4e24\u4e2a1\uff0c\u4f46\u6211\u4eec\u53ea\u5e94\u8fd4\u56de\u4e0d\u540c\u7684\u6570\u5bf9\u7684\u6570\u91cf\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165:[1, 2, 3, 4, 5], k = 1\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u6570\u7ec4\u4e2d\u6709\u56db\u4e2a 1-diff \u6570\u5bf9, (1, 2), (2, 3), (3, 4) \u548c (4, 5)\u3002\n\n\n
\u793a\u4f8b 3:
\n\n\n\u8f93\u5165: [1, 3, 1, 5, 4], k = 0\n\u8f93\u51fa: 1\n\u89e3\u91ca: \u6570\u7ec4\u4e2d\u53ea\u6709\u4e00\u4e2a 0-diff \u6570\u5bf9\uff0c(1, 1)\u3002\n\n\n
\u6ce8\u610f:
\n\nThere are N rooms and you start in room 0. Each room has a distinct number in 0, 1, 2, ..., N-1, and each room may have some keys to access the next room.
Formally, each room i has a list of keys rooms[i], and each key rooms[i][j] is an integer in [0, 1, ..., N-1] where N = rooms.length. A key rooms[i][j] = v opens the room with number v.
Initially, all the rooms start locked (except for room 0).
You can walk back and forth between rooms freely.
\n\nReturn true if and only if you can enter every room.
Example 1:
\n\n\nInput: [[1],[2],[3],[]]\nOutput: true\nExplanation: \nWe start in room 0, and pick up key 1.\nWe then go to room 1, and pick up key 2.\nWe then go to room 2, and pick up key 3.\nWe then go to room 3. Since we were able to go to every room, we return true.\n\n\n
Example 2:
\n\n\nInput: [[1,3],[3,0,1],[2],[0]]\nOutput: false\nExplanation: We can't enter the room with number 2.\n\n\n
Note:
\n\n1 <= rooms.length <= 10000 <= rooms[i].length <= 10003000.\u6709 N \u4e2a\u623f\u95f4\uff0c\u5f00\u59cb\u65f6\u4f60\u4f4d\u4e8e 0 \u53f7\u623f\u95f4\u3002\u6bcf\u4e2a\u623f\u95f4\u6709\u4e0d\u540c\u7684\u53f7\u7801\uff1a0\uff0c1\uff0c2\uff0c...\uff0cN-1\uff0c\u5e76\u4e14\u623f\u95f4\u91cc\u53ef\u80fd\u6709\u4e00\u4e9b\u94a5\u5319\u80fd\u4f7f\u4f60\u8fdb\u5165\u4e0b\u4e00\u4e2a\u623f\u95f4\u3002
\u5728\u5f62\u5f0f\u4e0a\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u623f\u95f4 i \u90fd\u6709\u4e00\u4e2a\u94a5\u5319\u5217\u8868 rooms[i]\uff0c\u6bcf\u4e2a\u94a5\u5319 rooms[i][j] \u7531 [0,1\uff0c...\uff0cN-1] \u4e2d\u7684\u4e00\u4e2a\u6574\u6570\u8868\u793a\uff0c\u5176\u4e2d N = rooms.length\u3002 \u94a5\u5319 rooms[i][j] = v \u53ef\u4ee5\u6253\u5f00\u7f16\u53f7\u4e3a v \u7684\u623f\u95f4\u3002
\u6700\u521d\uff0c\u9664 0 \u53f7\u623f\u95f4\u5916\u7684\u5176\u4f59\u6240\u6709\u623f\u95f4\u90fd\u88ab\u9501\u4f4f\u3002
\u4f60\u53ef\u4ee5\u81ea\u7531\u5730\u5728\u623f\u95f4\u4e4b\u95f4\u6765\u56de\u8d70\u52a8\u3002
\n\n\u5982\u679c\u80fd\u8fdb\u5165\u6bcf\u4e2a\u623f\u95f4\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165: [[1],[2],[3],[]]\n\u8f93\u51fa: true\n\u89e3\u91ca: \n\u6211\u4eec\u4ece 0 \u53f7\u623f\u95f4\u5f00\u59cb\uff0c\u62ff\u5230\u94a5\u5319 1\u3002\n\u4e4b\u540e\u6211\u4eec\u53bb 1 \u53f7\u623f\u95f4\uff0c\u62ff\u5230\u94a5\u5319 2\u3002\n\u7136\u540e\u6211\u4eec\u53bb 2 \u53f7\u623f\u95f4\uff0c\u62ff\u5230\u94a5\u5319 3\u3002\n\u6700\u540e\u6211\u4eec\u53bb\u4e86 3 \u53f7\u623f\u95f4\u3002\n\u7531\u4e8e\u6211\u4eec\u80fd\u591f\u8fdb\u5165\u6bcf\u4e2a\u623f\u95f4\uff0c\u6211\u4eec\u8fd4\u56de true\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1a[[1,3],[3,0,1],[2],[0]]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u6211\u4eec\u4e0d\u80fd\u8fdb\u5165 2 \u53f7\u623f\u95f4\u3002\n\n\n
\u63d0\u793a\uff1a
\n\n1 <= rooms.length <= 10000 <= rooms[i].length <= 10003000\u3002\u6709 N \u4e2a\u623f\u95f4\uff0c\u5f00\u59cb\u65f6\u4f60\u4f4d\u4e8e 0 \u53f7\u623f\u95f4\u3002\u6bcf\u4e2a\u623f\u95f4\u6709\u4e0d\u540c\u7684\u53f7\u7801\uff1a0\uff0c1\uff0c2\uff0c...\uff0cN-1\uff0c\u5e76\u4e14\u623f\u95f4\u91cc\u53ef\u80fd\u6709\u4e00\u4e9b\u94a5\u5319\u80fd\u4f7f\u4f60\u8fdb\u5165\u4e0b\u4e00\u4e2a\u623f\u95f4\u3002
\u5728\u5f62\u5f0f\u4e0a\uff0c\u5bf9\u4e8e\u6bcf\u4e2a\u623f\u95f4 i \u90fd\u6709\u4e00\u4e2a\u94a5\u5319\u5217\u8868 rooms[i]\uff0c\u6bcf\u4e2a\u94a5\u5319 rooms[i][j] \u7531 [0,1\uff0c...\uff0cN-1] \u4e2d\u7684\u4e00\u4e2a\u6574\u6570\u8868\u793a\uff0c\u5176\u4e2d N = rooms.length\u3002 \u94a5\u5319 rooms[i][j] = v \u53ef\u4ee5\u6253\u5f00\u7f16\u53f7\u4e3a v \u7684\u623f\u95f4\u3002
\u6700\u521d\uff0c\u9664 0 \u53f7\u623f\u95f4\u5916\u7684\u5176\u4f59\u6240\u6709\u623f\u95f4\u90fd\u88ab\u9501\u4f4f\u3002
\u4f60\u53ef\u4ee5\u81ea\u7531\u5730\u5728\u623f\u95f4\u4e4b\u95f4\u6765\u56de\u8d70\u52a8\u3002
\n\n\u5982\u679c\u80fd\u8fdb\u5165\u6bcf\u4e2a\u623f\u95f4\u8fd4\u56de true\uff0c\u5426\u5219\u8fd4\u56de false\u3002
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165: [[1],[2],[3],[]]\n\u8f93\u51fa: true\n\u89e3\u91ca: \n\u6211\u4eec\u4ece 0 \u53f7\u623f\u95f4\u5f00\u59cb\uff0c\u62ff\u5230\u94a5\u5319 1\u3002\n\u4e4b\u540e\u6211\u4eec\u53bb 1 \u53f7\u623f\u95f4\uff0c\u62ff\u5230\u94a5\u5319 2\u3002\n\u7136\u540e\u6211\u4eec\u53bb 2 \u53f7\u623f\u95f4\uff0c\u62ff\u5230\u94a5\u5319 3\u3002\n\u6700\u540e\u6211\u4eec\u53bb\u4e86 3 \u53f7\u623f\u95f4\u3002\n\u7531\u4e8e\u6211\u4eec\u80fd\u591f\u8fdb\u5165\u6bcf\u4e2a\u623f\u95f4\uff0c\u6211\u4eec\u8fd4\u56de true\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1a[[1,3],[3,0,1],[2],[0]]\n\u8f93\u51fa\uff1afalse\n\u89e3\u91ca\uff1a\u6211\u4eec\u4e0d\u80fd\u8fdb\u5165 2 \u53f7\u623f\u95f4\u3002\n\n\n
\u63d0\u793a\uff1a
\n\n1 <= rooms.length <= 10000 <= rooms[i].length <= 10003000\u3002There are two types of soup: type A and type B. Initially we have N ml of each type of soup. There are four kinds of operations:
When we serve some soup, we give it to someone and we no longer have it. Each turn, we will choose from the four operations with equal probability 0.25. If the remaining volume of soup is not enough to complete the operation, we will serve as much as we can. We stop once we no longer have some quantity of both types of soup.
\n\nNote that we do not have the operation where all 100 ml's of soup B are used first.
\n\nReturn the probability that soup A will be empty first, plus half the probability that A and B become empty at the same time.
\n\n\n\n
\nExample:\nInput: N = 50\nOutput: 0.625\nExplanation: \nIf we choose the first two operations, A will become empty first. For the third operation, A and B will become empty at the same time. For the fourth operation, B will become empty first. So the total probability of A becoming empty first plus half the probability that A and B become empty at the same time, is 0.25 * (1 + 1 + 0.5 + 0) = 0.625.\n\n\n\n
Notes:
\n\n0 <= N <= 10^9. 10^-6 of the true value will be accepted as correct.\u6709 A \u548c B \u4e24\u79cd\u7c7b\u578b\u7684\u6c64\u3002\u4e00\u5f00\u59cb\u6bcf\u79cd\u7c7b\u578b\u7684\u6c64\u6709 N \u6beb\u5347\u3002\u6709\u56db\u79cd\u5206\u914d\u64cd\u4f5c\uff1a
\u5f53\u6211\u4eec\u628a\u6c64\u5206\u914d\u7ed9\u67d0\u4eba\u4e4b\u540e\uff0c\u6c64\u5c31\u6ca1\u6709\u4e86\u3002\u6bcf\u4e2a\u56de\u5408\uff0c\u6211\u4eec\u5c06\u4ece\u56db\u79cd\u6982\u7387\u540c\u4e3a0.25\u7684\u64cd\u4f5c\u4e2d\u8fdb\u884c\u5206\u914d\u9009\u62e9\u3002\u5982\u679c\u6c64\u7684\u5269\u4f59\u91cf\u4e0d\u8db3\u4ee5\u5b8c\u6210\u67d0\u6b21\u64cd\u4f5c\uff0c\u6211\u4eec\u5c06\u5c3d\u53ef\u80fd\u5206\u914d\u3002\u5f53\u4e24\u79cd\u7c7b\u578b\u7684\u6c64\u90fd\u5206\u914d\u5b8c\u65f6\uff0c\u505c\u6b62\u64cd\u4f5c\u3002
\n\n\u6ce8\u610f\u4e0d\u5b58\u5728\u5148\u5206\u914d100 ml\u6c64B\u7684\u64cd\u4f5c\u3002
\n\n\u9700\u8981\u8fd4\u56de\u7684\u503c\uff1a \u6c64A\u5148\u5206\u914d\u5b8c\u7684\u6982\u7387 + \u6c64A\u548c\u6c64B\u540c\u65f6\u5206\u914d\u5b8c\u7684\u6982\u7387 / 2\u3002
\n\n\n\u793a\u4f8b:\n\u8f93\u5165: N = 50\n\u8f93\u51fa: 0.625\n\u89e3\u91ca:\n\u5982\u679c\u6211\u4eec\u9009\u62e9\u524d\u4e24\u4e2a\u64cd\u4f5c\uff0cA\u5c06\u9996\u5148\u53d8\u4e3a\u7a7a\u3002\u5bf9\u4e8e\u7b2c\u4e09\u4e2a\u64cd\u4f5c\uff0cA\u548cB\u4f1a\u540c\u65f6\u53d8\u4e3a\u7a7a\u3002\u5bf9\u4e8e\u7b2c\u56db\u4e2a\u64cd\u4f5c\uff0cB\u5c06\u9996\u5148\u53d8\u4e3a\u7a7a\u3002\n\u6240\u4ee5A\u53d8\u4e3a\u7a7a\u7684\u603b\u6982\u7387\u52a0\u4e0aA\u548cB\u540c\u65f6\u53d8\u4e3a\u7a7a\u7684\u6982\u7387\u7684\u4e00\u534a\u662f 0.25 *(1 + 1 + 0.5 + 0)= 0.625\u3002\n\n\n
\u6ce8\u91ca:
\n\n0 <= N <= 10^9\u3002\u8fd4\u56de\u503c\u5728 10^-6 \u7684\u8303\u56f4\u5c06\u88ab\u8ba4\u4e3a\u662f\u6b63\u786e\u7684\u3002
\u6709 A \u548c B \u4e24\u79cd\u7c7b\u578b\u7684\u6c64\u3002\u4e00\u5f00\u59cb\u6bcf\u79cd\u7c7b\u578b\u7684\u6c64\u6709 N \u6beb\u5347\u3002\u6709\u56db\u79cd\u5206\u914d\u64cd\u4f5c\uff1a
\u5f53\u6211\u4eec\u628a\u6c64\u5206\u914d\u7ed9\u67d0\u4eba\u4e4b\u540e\uff0c\u6c64\u5c31\u6ca1\u6709\u4e86\u3002\u6bcf\u4e2a\u56de\u5408\uff0c\u6211\u4eec\u5c06\u4ece\u56db\u79cd\u6982\u7387\u540c\u4e3a0.25\u7684\u64cd\u4f5c\u4e2d\u8fdb\u884c\u5206\u914d\u9009\u62e9\u3002\u5982\u679c\u6c64\u7684\u5269\u4f59\u91cf\u4e0d\u8db3\u4ee5\u5b8c\u6210\u67d0\u6b21\u64cd\u4f5c\uff0c\u6211\u4eec\u5c06\u5c3d\u53ef\u80fd\u5206\u914d\u3002\u5f53\u4e24\u79cd\u7c7b\u578b\u7684\u6c64\u90fd\u5206\u914d\u5b8c\u65f6\uff0c\u505c\u6b62\u64cd\u4f5c\u3002
\n\n\u6ce8\u610f\u4e0d\u5b58\u5728\u5148\u5206\u914d100 ml\u6c64B\u7684\u64cd\u4f5c\u3002
\n\n\u9700\u8981\u8fd4\u56de\u7684\u503c\uff1a \u6c64A\u5148\u5206\u914d\u5b8c\u7684\u6982\u7387 + \u6c64A\u548c\u6c64B\u540c\u65f6\u5206\u914d\u5b8c\u7684\u6982\u7387 / 2\u3002
\n\n\n\u793a\u4f8b:\n\u8f93\u5165: N = 50\n\u8f93\u51fa: 0.625\n\u89e3\u91ca:\n\u5982\u679c\u6211\u4eec\u9009\u62e9\u524d\u4e24\u4e2a\u64cd\u4f5c\uff0cA\u5c06\u9996\u5148\u53d8\u4e3a\u7a7a\u3002\u5bf9\u4e8e\u7b2c\u4e09\u4e2a\u64cd\u4f5c\uff0cA\u548cB\u4f1a\u540c\u65f6\u53d8\u4e3a\u7a7a\u3002\u5bf9\u4e8e\u7b2c\u56db\u4e2a\u64cd\u4f5c\uff0cB\u5c06\u9996\u5148\u53d8\u4e3a\u7a7a\u3002\n\u6240\u4ee5A\u53d8\u4e3a\u7a7a\u7684\u603b\u6982\u7387\u52a0\u4e0aA\u548cB\u540c\u65f6\u53d8\u4e3a\u7a7a\u7684\u6982\u7387\u7684\u4e00\u534a\u662f 0.25 *(1 + 1 + 0.5 + 0)= 0.625\u3002\n\n\n
\u6ce8\u91ca:
\n\n0 <= N <= 10^9\u3002\u8fd4\u56de\u503c\u5728 10^-6 \u7684\u8303\u56f4\u5c06\u88ab\u8ba4\u4e3a\u662f\u6b63\u786e\u7684\u3002
In a 2D grid of 0s and 1s, we change at most one 0 to a 1.
After, what is the size of the largest island? (An island is a 4-directionally connected group of 1s).
Example 1:
\n\n\nInput: [[1, 0], [0, 1]]\nOutput: 3\nExplanation: Change one 0 to 1 and connect two 1s, then we get an island with area = 3.\n\n\n
Example 2:
\n\n\nInput: [[1, 1], [1, 0]]\nOutput: 4\nExplanation: Change the 0 to 1 and make the island bigger, only one island with area = 1.\n\n
Example 3:
\n\n\nInput: [[1, 1], [1, 1]]\nOutput: 4\nExplanation: Can't change any 0 to 1, only one island with area = 1.\n\n
\n\n
Notes:
\n\n1 <= grid.length = grid[0].length <= 50.0 <= grid[i][j] <= 1.
\u5728\u4e8c\u7ef4\u5730\u56fe\u4e0a\uff0c 0\u4ee3\u8868\u6d77\u6d0b\uff0c 1\u4ee3\u8868\u9646\u5730\uff0c\u6211\u4eec\u6700\u591a\u53ea\u80fd\u5c06\u4e00\u683c 0 \u6d77\u6d0b\u53d8\u6210 1\u53d8\u6210\u9646\u5730\u3002
\u8fdb\u884c\u586b\u6d77\u4e4b\u540e\uff0c\u5730\u56fe\u4e0a\u6700\u5927\u7684\u5c9b\u5c7f\u9762\u79ef\u662f\u591a\u5c11\uff1f\uff08\u4e0a\u3001\u4e0b\u3001\u5de6\u3001\u53f3\u56db\u4e2a\u65b9\u5411\u76f8\u8fde\u7684 1 \u53ef\u5f62\u6210\u5c9b\u5c7f\uff09
\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: [[1, 0], [0, 1]]\n\u8f93\u51fa: 3\n\u89e3\u91ca: \u5c06\u4e00\u683c0\u53d8\u62101\uff0c\u6700\u7ec8\u8fde\u901a\u4e24\u4e2a\u5c0f\u5c9b\u5f97\u5230\u9762\u79ef\u4e3a 3 \u7684\u5c9b\u5c7f\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: [[1, 1], [1, 0]]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u5c06\u4e00\u683c0\u53d8\u62101\uff0c\u5c9b\u5c7f\u7684\u9762\u79ef\u6269\u5927\u4e3a 4\u3002\n\n
\u793a\u4f8b 3:
\n\n\n\u8f93\u5165: [[1, 1], [1, 1]]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u6ca1\u67090\u53ef\u4ee5\u8ba9\u6211\u4eec\u53d8\u62101\uff0c\u9762\u79ef\u4f9d\u7136\u4e3a 4\u3002\n\n
\u8bf4\u660e:
\n\n1 <= grid.length = grid[0].length <= 500 <= grid[i][j] <= 1\u5728\u4e8c\u7ef4\u5730\u56fe\u4e0a\uff0c 0\u4ee3\u8868\u6d77\u6d0b\uff0c 1\u4ee3\u8868\u9646\u5730\uff0c\u6211\u4eec\u6700\u591a\u53ea\u80fd\u5c06\u4e00\u683c 0 \u6d77\u6d0b\u53d8\u6210 1\u53d8\u6210\u9646\u5730\u3002
\u8fdb\u884c\u586b\u6d77\u4e4b\u540e\uff0c\u5730\u56fe\u4e0a\u6700\u5927\u7684\u5c9b\u5c7f\u9762\u79ef\u662f\u591a\u5c11\uff1f\uff08\u4e0a\u3001\u4e0b\u3001\u5de6\u3001\u53f3\u56db\u4e2a\u65b9\u5411\u76f8\u8fde\u7684 1 \u53ef\u5f62\u6210\u5c9b\u5c7f\uff09
\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: [[1, 0], [0, 1]]\n\u8f93\u51fa: 3\n\u89e3\u91ca: \u5c06\u4e00\u683c0\u53d8\u62101\uff0c\u6700\u7ec8\u8fde\u901a\u4e24\u4e2a\u5c0f\u5c9b\u5f97\u5230\u9762\u79ef\u4e3a 3 \u7684\u5c9b\u5c7f\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: [[1, 1], [1, 0]]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u5c06\u4e00\u683c0\u53d8\u62101\uff0c\u5c9b\u5c7f\u7684\u9762\u79ef\u6269\u5927\u4e3a 4\u3002\n\n
\u793a\u4f8b 3:
\n\n\n\u8f93\u5165: [[1, 1], [1, 1]]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u6ca1\u67090\u53ef\u4ee5\u8ba9\u6211\u4eec\u53d8\u62101\uff0c\u9762\u79ef\u4f9d\u7136\u4e3a 4\u3002\n\n
\u8bf4\u660e:
\n\n1 <= grid.length = grid[0].length <= 500 <= grid[i][j] <= 1Sometimes people repeat letters to represent extra feeling, such as "hello" -> "heeellooo", "hi" -> "hiiii". Here, we have groups, of adjacent letters that are all the same character, and adjacent characters to the group are different. A group is extended if that group is length 3 or more, so "e" and "o" would be extended in the first example, and "i" would be extended in the second example. As another example, the groups of "abbcccaaaa" would be "a", "bb", "ccc", and "aaaa"; and "ccc" and "aaaa" are the extended groups of that string.
\n\nFor some given string S, a query word is stretchy if it can be made to be equal to S by extending some groups. Formally, we are allowed to repeatedly choose a group (as defined above) of characters c, and add some number of the same character c to it so that the length of the group is 3 or more. Note that we cannot extend a group of size one like "h" to a group of size two like "hh" - all extensions must leave the group extended - ie., at least 3 characters long.
Given a list of query words, return the number of words that are stretchy.
\n\n\nExample:\nInput: \nS = "heeellooo"\nwords = ["hello", "hi", "helo"]\nOutput: 1\nExplanation: \nWe can extend "e" and "o" in the word "hello" to get "heeellooo".\nWe can't extend "helo" to get "heeellooo" because the group "ll" is not extended.\n\n\n
Notes:
\n\n0 <= len(S) <= 100.0 <= len(words) <= 100.0 <= len(words[i]) <= 100.S and all words in words consist only of lowercase letters\n
\u6709\u65f6\u5019\u4eba\u4eec\u4f1a\u7528\u989d\u5916\u7684\u5b57\u6bcd\u6765\u8868\u793a\u989d\u5916\u7684\u60c5\u611f\uff0c\u6bd4\u5982 "hello" -> "heeellooo", "hi" -> "hiii"\u3002\u6211\u4eec\u5c06\u8fde\u7eed\u7684\u76f8\u540c\u7684\u5b57\u6bcd\u5206\u7ec4\uff0c\u5e76\u4e14\u76f8\u90bb\u7ec4\u7684\u5b57\u6bcd\u90fd\u4e0d\u76f8\u540c\u3002\u6211\u4eec\u5c06\u4e00\u4e2a\u62e5\u6709\u4e09\u4e2a\u6216\u4ee5\u4e0a\u5b57\u6bcd\u7684\u7ec4\u5b9a\u4e49\u4e3a\u6269\u5f20\u72b6\u6001\uff08extended\uff09\uff0c\u5982\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u7684 "e" \u548c" o" \u4ee5\u53ca\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\u7684 "i"\u3002 \u6b64\u5916\uff0c"abbcccaaaa" \u5c06\u6709\u5206\u7ec4 "a" , "bb" , "ccc" , "dddd"\uff1b\u5176\u4e2d "ccc" \u548c "aaaa" \u5904\u4e8e\u6269\u5f20\u72b6\u6001\u3002
\n\n\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5b57\u7b26\u4e32 S \uff0c\u5982\u679c\u53e6\u4e00\u4e2a\u5355\u8bcd\u80fd\u591f\u901a\u8fc7\u5c06\u4e00\u4e9b\u5b57\u6bcd\u7ec4\u6269\u5f20\u4ece\u800c\u4f7f\u5176\u548c S \u76f8\u540c\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u5355\u8bcd\u5b9a\u4e49\u4e3a\u53ef\u6269\u5f20\u7684\uff08stretchy\uff09\u3002\u6211\u4eec\u5141\u8bb8\u9009\u62e9\u4e00\u4e2a\u5b57\u6bcd\u7ec4\uff08\u5982\u5305\u542b\u5b57\u6bcd c \uff09\uff0c\u7136\u540e\u5f80\u5176\u4e2d\u6dfb\u52a0\u76f8\u540c\u7684\u5b57\u6bcd c \u4f7f\u5176\u957f\u5ea6\u8fbe\u5230 3 \u6216\u4ee5\u4e0a\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u4e0d\u80fd\u5c06\u4e00\u4e2a\u53ea\u5305\u542b\u4e00\u4e2a\u5b57\u6bcd\u7684\u5b57\u6bcd\u7ec4\uff0c\u5982 "h"\uff0c\u6269\u5f20\u5230\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5b57\u6bcd\u7684\u7ec4\uff0c\u5982 "hh"\uff1b\u6240\u6709\u7684\u6269\u5f20\u5fc5\u987b\u4f7f\u8be5\u5b57\u6bcd\u7ec4\u53d8\u6210\u6269\u5f20\u72b6\u6001\uff08\u81f3\u5c11\u5305\u542b\u4e09\u4e2a\u5b57\u6bcd\uff09\u3002
\u8f93\u5165\u4e00\u7ec4\u5355\u8bcd\uff0c\u8f93\u51fa\u5176\u4e2d\u53ef\u6269\u5f20\u7684\u5355\u8bcd\u6570\u91cf\u3002
\n\n\n\u793a\u4f8b\uff1a\n\u8f93\u5165\uff1a \nS = "heeellooo"\nwords = ["hello", "hi", "helo"]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\n\u6211\u4eec\u80fd\u901a\u8fc7\u6269\u5f20"hello"\u7684"e"\u548c"o"\u6765\u5f97\u5230"heeellooo"\u3002\n\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7\u6269\u5f20"helo"\u6765\u5f97\u5230"heeellooo"\u56e0\u4e3a"ll"\u4e0d\u5904\u4e8e\u6269\u5f20\u72b6\u6001\u3002\n\n\n
\u8bf4\u660e\uff1a
\n\n0 <= len(S) <= 100\u30020 <= len(words) <= 100\u30020 <= len(words[i]) <= 100\u3002S \u548c\u6240\u6709\u5728 words \u4e2d\u7684\u5355\u8bcd\u90fd\u53ea\u7531\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u3002\u6709\u65f6\u5019\u4eba\u4eec\u4f1a\u7528\u989d\u5916\u7684\u5b57\u6bcd\u6765\u8868\u793a\u989d\u5916\u7684\u60c5\u611f\uff0c\u6bd4\u5982 "hello" -> "heeellooo", "hi" -> "hiii"\u3002\u6211\u4eec\u5c06\u8fde\u7eed\u7684\u76f8\u540c\u7684\u5b57\u6bcd\u5206\u7ec4\uff0c\u5e76\u4e14\u76f8\u90bb\u7ec4\u7684\u5b57\u6bcd\u90fd\u4e0d\u76f8\u540c\u3002\u6211\u4eec\u5c06\u4e00\u4e2a\u62e5\u6709\u4e09\u4e2a\u6216\u4ee5\u4e0a\u5b57\u6bcd\u7684\u7ec4\u5b9a\u4e49\u4e3a\u6269\u5f20\u72b6\u6001\uff08extended\uff09\uff0c\u5982\u7b2c\u4e00\u4e2a\u4f8b\u5b50\u4e2d\u7684 "e" \u548c" o" \u4ee5\u53ca\u7b2c\u4e8c\u4e2a\u4f8b\u5b50\u4e2d\u7684 "i"\u3002 \u6b64\u5916\uff0c"abbcccaaaa" \u5c06\u6709\u5206\u7ec4 "a" , "bb" , "ccc" , "dddd"\uff1b\u5176\u4e2d "ccc" \u548c "aaaa" \u5904\u4e8e\u6269\u5f20\u72b6\u6001\u3002
\n\n\u5bf9\u4e8e\u4e00\u4e2a\u7ed9\u5b9a\u7684\u5b57\u7b26\u4e32 S \uff0c\u5982\u679c\u53e6\u4e00\u4e2a\u5355\u8bcd\u80fd\u591f\u901a\u8fc7\u5c06\u4e00\u4e9b\u5b57\u6bcd\u7ec4\u6269\u5f20\u4ece\u800c\u4f7f\u5176\u548c S \u76f8\u540c\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u5355\u8bcd\u5b9a\u4e49\u4e3a\u53ef\u6269\u5f20\u7684\uff08stretchy\uff09\u3002\u6211\u4eec\u5141\u8bb8\u9009\u62e9\u4e00\u4e2a\u5b57\u6bcd\u7ec4\uff08\u5982\u5305\u542b\u5b57\u6bcd c \uff09\uff0c\u7136\u540e\u5f80\u5176\u4e2d\u6dfb\u52a0\u76f8\u540c\u7684\u5b57\u6bcd c \u4f7f\u5176\u957f\u5ea6\u8fbe\u5230 3 \u6216\u4ee5\u4e0a\u3002\u6ce8\u610f\uff0c\u6211\u4eec\u4e0d\u80fd\u5c06\u4e00\u4e2a\u53ea\u5305\u542b\u4e00\u4e2a\u5b57\u6bcd\u7684\u5b57\u6bcd\u7ec4\uff0c\u5982 "h"\uff0c\u6269\u5f20\u5230\u4e00\u4e2a\u5305\u542b\u4e24\u4e2a\u5b57\u6bcd\u7684\u7ec4\uff0c\u5982 "hh"\uff1b\u6240\u6709\u7684\u6269\u5f20\u5fc5\u987b\u4f7f\u8be5\u5b57\u6bcd\u7ec4\u53d8\u6210\u6269\u5f20\u72b6\u6001\uff08\u81f3\u5c11\u5305\u542b\u4e09\u4e2a\u5b57\u6bcd\uff09\u3002
\u8f93\u5165\u4e00\u7ec4\u5355\u8bcd\uff0c\u8f93\u51fa\u5176\u4e2d\u53ef\u6269\u5f20\u7684\u5355\u8bcd\u6570\u91cf\u3002
\n\n\n\u793a\u4f8b\uff1a\n\u8f93\u5165\uff1a \nS = "heeellooo"\nwords = ["hello", "hi", "helo"]\n\u8f93\u51fa\uff1a1\n\u89e3\u91ca\uff1a\n\u6211\u4eec\u80fd\u901a\u8fc7\u6269\u5f20"hello"\u7684"e"\u548c"o"\u6765\u5f97\u5230"heeellooo"\u3002\n\u6211\u4eec\u4e0d\u80fd\u901a\u8fc7\u6269\u5f20"helo"\u6765\u5f97\u5230"heeellooo"\u56e0\u4e3a"ll"\u4e0d\u5904\u4e8e\u6269\u5f20\u72b6\u6001\u3002\n\n\n
\u8bf4\u660e\uff1a
\n\n0 <= len(S) <= 100\u30020 <= len(words) <= 100\u30020 <= len(words[i]) <= 100\u3002S \u548c\u6240\u6709\u5728 words \u4e2d\u7684\u5355\u8bcd\u90fd\u53ea\u7531\u5c0f\u5199\u5b57\u6bcd\u7ec4\u6210\u3002\nIn LOL world, there is a hero called Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, you need to output the total time that Ashe is in poisoned condition.\n
\n\nYou may assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.
\n\nExample 1:
\n
\nInput: [1,4], 2\nOutput: 4\nExplanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned immediately.\n\n\n\n
This poisoned status will last 2 seconds until the end of time point 2.
And at time point 4, Teemo attacks Ashe again, and causes Ashe to be in poisoned status for another 2 seconds.
So you finally need to output 4.\n
Example 2:
\n
\nInput: [1,2], 2\nOutput: 3\nExplanation: At time point 1, Teemo starts attacking Ashe and makes Ashe be poisoned.\n\n\n\n
This poisoned status will last 2 seconds until the end of time point 2.
However, at the beginning of time point 2, Teemo attacks Ashe again who is already in poisoned status.
Since the poisoned status won't add up together, though the second poisoning attack will still work at time point 2, it will stop at the end of time point 3.
So you finally need to output 3.\n
Note:
\n
\u5728\u300a\u82f1\u96c4\u8054\u76df\u300b\u7684\u4e16\u754c\u4e2d\uff0c\u6709\u4e00\u4e2a\u53eb“\u63d0\u83ab”\u7684\u82f1\u96c4\uff0c\u4ed6\u7684\u653b\u51fb\u53ef\u4ee5\u8ba9\u654c\u65b9\u82f1\u96c4\u827e\u5e0c\uff08\u7f16\u8005\u6ce8\uff1a\u5bd2\u51b0\u5c04\u624b\uff09\u8fdb\u5165\u4e2d\u6bd2\u72b6\u6001\u3002\u73b0\u5728\uff0c\u7ed9\u51fa\u63d0\u83ab\u5bf9\u827e\u5e0c\u7684\u653b\u51fb\u65f6\u95f4\u5e8f\u5217\u548c\u63d0\u83ab\u653b\u51fb\u7684\u4e2d\u6bd2\u6301\u7eed\u65f6\u95f4\uff0c\u4f60\u9700\u8981\u8f93\u51fa\u827e\u5e0c\u7684\u4e2d\u6bd2\u72b6\u6001\u603b\u65f6\u957f\u3002
\n\n\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u63d0\u83ab\u5728\u7ed9\u5b9a\u7684\u65f6\u95f4\u70b9\u8fdb\u884c\u653b\u51fb\uff0c\u5e76\u7acb\u5373\u4f7f\u827e\u5e0c\u5904\u4e8e\u4e2d\u6bd2\u72b6\u6001\u3002
\n\n\u793a\u4f8b1:
\n\n\n\u8f93\u5165: [1,4], 2\n\u8f93\u51fa: 4\n\u539f\u56e0: \u5728\u7b2c1\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u5f00\u59cb\u5bf9\u827e\u5e0c\u8fdb\u884c\u653b\u51fb\u5e76\u4f7f\u5176\u7acb\u5373\u4e2d\u6bd2\u3002\u4e2d\u6bd2\u72b6\u6001\u4f1a\u7ef4\u63012\u79d2\u949f\uff0c\u76f4\u5230\u7b2c2\u79d2\u949f\u7ed3\u675f\u3002\n\u5728\u7b2c4\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u518d\u6b21\u653b\u51fb\u827e\u5e0c\uff0c\u4f7f\u5f97\u827e\u5e0c\u83b7\u5f97\u53e6\u59162\u79d2\u7684\u4e2d\u6bd2\u65f6\u95f4\u3002\n\u6240\u4ee5\u6700\u7ec8\u8f93\u51fa4\u79d2\u3002\n\n\n
\u793a\u4f8b2:
\n\n\n\u8f93\u5165: [1,2], 2\n\u8f93\u51fa: 3\n\u539f\u56e0: \u5728\u7b2c1\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u5f00\u59cb\u5bf9\u827e\u5e0c\u8fdb\u884c\u653b\u51fb\u5e76\u4f7f\u5176\u7acb\u5373\u4e2d\u6bd2\u3002\u4e2d\u6bd2\u72b6\u6001\u4f1a\u7ef4\u63012\u79d2\u949f\uff0c\u76f4\u5230\u7b2c2\u79d2\u949f\u7ed3\u675f\u3002\n\u4f46\u662f\u5728\u7b2c2\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u518d\u6b21\u653b\u51fb\u4e86\u5df2\u7ecf\u5904\u4e8e\u4e2d\u6bd2\u72b6\u6001\u7684\u827e\u5e0c\u3002\n\u7531\u4e8e\u4e2d\u6bd2\u72b6\u6001\u4e0d\u53ef\u53e0\u52a0\uff0c\u63d0\u83ab\u5728\u7b2c2\u79d2\u5f00\u59cb\u65f6\u7684\u8fd9\u6b21\u653b\u51fb\u4f1a\u5728\u7b2c3\u79d2\u949f\u7ed3\u675f\u3002\n\u6240\u4ee5\u6700\u7ec8\u8f93\u51fa3\u3002\n\n\n
\u6ce8\u610f\uff1a
\n\n\u5728\u300a\u82f1\u96c4\u8054\u76df\u300b\u7684\u4e16\u754c\u4e2d\uff0c\u6709\u4e00\u4e2a\u53eb“\u63d0\u83ab”\u7684\u82f1\u96c4\uff0c\u4ed6\u7684\u653b\u51fb\u53ef\u4ee5\u8ba9\u654c\u65b9\u82f1\u96c4\u827e\u5e0c\uff08\u7f16\u8005\u6ce8\uff1a\u5bd2\u51b0\u5c04\u624b\uff09\u8fdb\u5165\u4e2d\u6bd2\u72b6\u6001\u3002\u73b0\u5728\uff0c\u7ed9\u51fa\u63d0\u83ab\u5bf9\u827e\u5e0c\u7684\u653b\u51fb\u65f6\u95f4\u5e8f\u5217\u548c\u63d0\u83ab\u653b\u51fb\u7684\u4e2d\u6bd2\u6301\u7eed\u65f6\u95f4\uff0c\u4f60\u9700\u8981\u8f93\u51fa\u827e\u5e0c\u7684\u4e2d\u6bd2\u72b6\u6001\u603b\u65f6\u957f\u3002
\n\n\u4f60\u53ef\u4ee5\u8ba4\u4e3a\u63d0\u83ab\u5728\u7ed9\u5b9a\u7684\u65f6\u95f4\u70b9\u8fdb\u884c\u653b\u51fb\uff0c\u5e76\u7acb\u5373\u4f7f\u827e\u5e0c\u5904\u4e8e\u4e2d\u6bd2\u72b6\u6001\u3002
\n\n\u793a\u4f8b1:
\n\n\n\u8f93\u5165: [1,4], 2\n\u8f93\u51fa: 4\n\u539f\u56e0: \u5728\u7b2c1\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u5f00\u59cb\u5bf9\u827e\u5e0c\u8fdb\u884c\u653b\u51fb\u5e76\u4f7f\u5176\u7acb\u5373\u4e2d\u6bd2\u3002\u4e2d\u6bd2\u72b6\u6001\u4f1a\u7ef4\u63012\u79d2\u949f\uff0c\u76f4\u5230\u7b2c2\u79d2\u949f\u7ed3\u675f\u3002\n\u5728\u7b2c4\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u518d\u6b21\u653b\u51fb\u827e\u5e0c\uff0c\u4f7f\u5f97\u827e\u5e0c\u83b7\u5f97\u53e6\u59162\u79d2\u7684\u4e2d\u6bd2\u65f6\u95f4\u3002\n\u6240\u4ee5\u6700\u7ec8\u8f93\u51fa4\u79d2\u3002\n\n\n
\u793a\u4f8b2:
\n\n\n\u8f93\u5165: [1,2], 2\n\u8f93\u51fa: 3\n\u539f\u56e0: \u5728\u7b2c1\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u5f00\u59cb\u5bf9\u827e\u5e0c\u8fdb\u884c\u653b\u51fb\u5e76\u4f7f\u5176\u7acb\u5373\u4e2d\u6bd2\u3002\u4e2d\u6bd2\u72b6\u6001\u4f1a\u7ef4\u63012\u79d2\u949f\uff0c\u76f4\u5230\u7b2c2\u79d2\u949f\u7ed3\u675f\u3002\n\u4f46\u662f\u5728\u7b2c2\u79d2\u5f00\u59cb\u65f6\uff0c\u63d0\u83ab\u518d\u6b21\u653b\u51fb\u4e86\u5df2\u7ecf\u5904\u4e8e\u4e2d\u6bd2\u72b6\u6001\u7684\u827e\u5e0c\u3002\n\u7531\u4e8e\u4e2d\u6bd2\u72b6\u6001\u4e0d\u53ef\u53e0\u52a0\uff0c\u63d0\u83ab\u5728\u7b2c2\u79d2\u5f00\u59cb\u65f6\u7684\u8fd9\u6b21\u653b\u51fb\u4f1a\u5728\u7b2c3\u79d2\u949f\u7ed3\u675f\u3002\n\u6240\u4ee5\u6700\u7ec8\u8f93\u51fa3\u3002\n\n\n
\u6ce8\u610f\uff1a
\n\nGiven a linked list, remove the n-th node from the end of list and return its head.
\n\nExample:
\n\n\nGiven linked list: 1->2->3->4->5, and n = 2.\n\nAfter removing the second node from the end, the linked list becomes 1->2->3->5.\n\n\n
Note:
\n\nGiven n will always be valid.
\n\nFollow up:
\n\nCould you do this in one pass?
\n\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c n \u4e2a\u8282\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002
\n\n\u793a\u4f8b\uff1a
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868: 1->2->3->4->5, \u548c n = 2.\n\n\u5f53\u5220\u9664\u4e86\u5012\u6570\u7b2c\u4e8c\u4e2a\u8282\u70b9\u540e\uff0c\u94fe\u8868\u53d8\u4e3a 1->2->3->5.\n\n\n
\u8bf4\u660e\uff1a
\n\n\u7ed9\u5b9a\u7684 n \u4fdd\u8bc1\u662f\u6709\u6548\u7684\u3002
\n\n\u8fdb\u9636\uff1a
\n\n\u4f60\u80fd\u5c1d\u8bd5\u4f7f\u7528\u4e00\u8d9f\u626b\u63cf\u5b9e\u73b0\u5417\uff1f
\n\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868\uff0c\u5220\u9664\u94fe\u8868\u7684\u5012\u6570\u7b2c n \u4e2a\u8282\u70b9\uff0c\u5e76\u4e14\u8fd4\u56de\u94fe\u8868\u7684\u5934\u7ed3\u70b9\u3002
\n\n\u793a\u4f8b\uff1a
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u94fe\u8868: 1->2->3->4->5, \u548c n = 2.\n\n\u5f53\u5220\u9664\u4e86\u5012\u6570\u7b2c\u4e8c\u4e2a\u8282\u70b9\u540e\uff0c\u94fe\u8868\u53d8\u4e3a 1->2->3->5.\n\n\n
\u8bf4\u660e\uff1a
\n\n\u7ed9\u5b9a\u7684 n \u4fdd\u8bc1\u662f\u6709\u6548\u7684\u3002
\n\n\u8fdb\u9636\uff1a
\n\n\u4f60\u80fd\u5c1d\u8bd5\u4f7f\u7528\u4e00\u8d9f\u626b\u63cf\u5b9e\u73b0\u5417\uff1f
\n*/\n\n\n/**\n * Definition for singly-linked list.\n * function ListNode(val) {\n * this.val = val;\n * this.next = null;\n * }\n */\n/**\n * @param {ListNode} head\n * @param {number} n\n * @return {ListNode}\n */\nvar removeNthFromEnd = function(head, n) {\n \n};"} {"doc_id": "2b15f8947c7732527d5d6316cebc3bfc", "text": "\"use strict\";\n\nclass Rectangle {\n set width(w) {\n this._width = w;\n }\n\n get width() {\n return this._width;\n }\n\n set height(h) {\n this._height = h;\n }\n\n get height() {\n return this._height;\n }\n}\n\n// A function that operates on an instance of Rectangle\nfunction f(rectangle) {\n rectangle.width = 5;\n rectangle.height = 4;\n\n // Verify expected result\n if (rectangle.width * rectangle.height !== 20) {\n throw new Error(\"Expected the rectangle's area (width * height) to be 20\");\n }\n}\n\n// A square IS-A rectangle... right?\nclass Square extends Rectangle {\n set width(w) {\n super.width = w;\n\n // Maintain square-ness\n super.height = w;\n }\n\n set height(h) {\n super.height = h;\n\n // Maintain square-ness\n super.width = h;\n }\n}\n\n// But can a rectangle be substituted by a square?\nf(new Square()); // error"} {"doc_id": "2b2f1930b6a3e1599d8075cbcfbbd433", "text": "\"use strict\";\n// String\nvar cadena = \"Alex Ku Dzul\";\n// Number\nvar numero = 2;\n// Boolean\nvar bool = true;\n// Any\nvar cualquiera = \"Permite agregar cualquier cosa, y sobre escribir la variable\";\ncualquiera = 22;\n// Arrays\nvar lenguajes = [\"JS\", \"TS\", \"PHP\", \"PYTHON\"];\nvar years = [2, 2, 3, 4];\n// Multiples tipos de datos \nvar cadenaConNumero = \"Cadena\";\ncadenaConNumero = 22;\nvar letraynumero = \"Alfa numerico \" + 2;\n// let vs var\nvar numero1 = 10;\nvar numero2 = 11;\nif (numero1 == 10) {\n var numero1_1 = 20;\n var numero2_1 = 21;\n console.log(numero1_1, numero2_1);\n}\nconsole.log(numero1, numero2);\nconsole.log(cadena, numero, bool, cualquiera, lenguajes, years, cadenaConNumero, letraynumero);\n"} {"doc_id": "2b467bd8d96b68c61619872442a9fde3", "text": "/*\nA robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).
\n\nThe robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked 'Finish' in the diagram below).
\n\nHow many possible unique paths are there?
\n\n
\nAbove is a 7 x 3 grid. How many possible unique paths are there?
Note: m and n will be at most 100.
\n\nExample 1:
\n\n\nInput: m = 3, n = 2\nOutput: 3\nExplanation:\nFrom the top-left corner, there are a total of 3 ways to reach the bottom-right corner:\n1. Right -> Right -> Down\n2. Right -> Down -> Right\n3. Down -> Right -> Right\n\n\n
Example 2:
\n\n\nInput: m = 7, n = 3\nOutput: 28\n
\u4e00\u4e2a\u673a\u5668\u4eba\u4f4d\u4e8e\u4e00\u4e2a m x n \u7f51\u683c\u7684\u5de6\u4e0a\u89d2 \uff08\u8d77\u59cb\u70b9\u5728\u4e0b\u56fe\u4e2d\u6807\u8bb0\u4e3a“Start” \uff09\u3002
\n\n\u673a\u5668\u4eba\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\u3002\u673a\u5668\u4eba\u8bd5\u56fe\u8fbe\u5230\u7f51\u683c\u7684\u53f3\u4e0b\u89d2\uff08\u5728\u4e0b\u56fe\u4e2d\u6807\u8bb0\u4e3a“Finish”\uff09\u3002
\n\n\u95ee\u603b\u5171\u6709\u591a\u5c11\u6761\u4e0d\u540c\u7684\u8def\u5f84\uff1f
\n\n
\u4f8b\u5982\uff0c\u4e0a\u56fe\u662f\u4e00\u4e2a7 x 3 \u7684\u7f51\u683c\u3002\u6709\u591a\u5c11\u53ef\u80fd\u7684\u8def\u5f84\uff1f
\n\n\u8bf4\u660e\uff1am \u548c n \u7684\u503c\u5747\u4e0d\u8d85\u8fc7 100\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: m = 3, n = 2\n\u8f93\u51fa: 3\n\u89e3\u91ca:\n\u4ece\u5de6\u4e0a\u89d2\u5f00\u59cb\uff0c\u603b\u5171\u6709 3 \u6761\u8def\u5f84\u53ef\u4ee5\u5230\u8fbe\u53f3\u4e0b\u89d2\u3002\n1. \u5411\u53f3 -> \u5411\u53f3 -> \u5411\u4e0b\n2. \u5411\u53f3 -> \u5411\u4e0b -> \u5411\u53f3\n3. \u5411\u4e0b -> \u5411\u53f3 -> \u5411\u53f3\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: m = 7, n = 3\n\u8f93\u51fa: 28\n
\u4e00\u4e2a\u673a\u5668\u4eba\u4f4d\u4e8e\u4e00\u4e2a m x n \u7f51\u683c\u7684\u5de6\u4e0a\u89d2 \uff08\u8d77\u59cb\u70b9\u5728\u4e0b\u56fe\u4e2d\u6807\u8bb0\u4e3a“Start” \uff09\u3002
\n\n\u673a\u5668\u4eba\u6bcf\u6b21\u53ea\u80fd\u5411\u4e0b\u6216\u8005\u5411\u53f3\u79fb\u52a8\u4e00\u6b65\u3002\u673a\u5668\u4eba\u8bd5\u56fe\u8fbe\u5230\u7f51\u683c\u7684\u53f3\u4e0b\u89d2\uff08\u5728\u4e0b\u56fe\u4e2d\u6807\u8bb0\u4e3a“Finish”\uff09\u3002
\n\n\u95ee\u603b\u5171\u6709\u591a\u5c11\u6761\u4e0d\u540c\u7684\u8def\u5f84\uff1f
\n\n
\u4f8b\u5982\uff0c\u4e0a\u56fe\u662f\u4e00\u4e2a7 x 3 \u7684\u7f51\u683c\u3002\u6709\u591a\u5c11\u53ef\u80fd\u7684\u8def\u5f84\uff1f
\n\n\u8bf4\u660e\uff1am \u548c n \u7684\u503c\u5747\u4e0d\u8d85\u8fc7 100\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: m = 3, n = 2\n\u8f93\u51fa: 3\n\u89e3\u91ca:\n\u4ece\u5de6\u4e0a\u89d2\u5f00\u59cb\uff0c\u603b\u5171\u6709 3 \u6761\u8def\u5f84\u53ef\u4ee5\u5230\u8fbe\u53f3\u4e0b\u89d2\u3002\n1. \u5411\u53f3 -> \u5411\u53f3 -> \u5411\u4e0b\n2. \u5411\u53f3 -> \u5411\u4e0b -> \u5411\u53f3\n3. \u5411\u4e0b -> \u5411\u53f3 -> \u5411\u53f3\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: m = 7, n = 3\n\u8f93\u51fa: 28\n*/\n\n\n/**\n * @param {number} m\n * @param {number} n\n * @return {number}\n */\nvar uniquePaths = function(m, n) {\n \n};"} {"doc_id": "2b508f5ba3c17e52d6c04a2c1e3d94cf", "text": "console.log('-------------------------'); \r\nconsole.log('Task 2');\r\n\r\n/*\u0421\u043e\u0437\u0434\u0430\u0439\u0442\u0435 \u043e\u0431\u044a\u0435\u043a\u0442 \u0441 \u0434\u043d\u044f\u043c\u0438 \u043d\u0435\u0434\u0435\u043b\u0438. \u041a\u043b\u044e\u0447\u0430\u043c\u0438 \u0432 \u043d\u0435\u043c \r\n\u0434\u043e\u043b\u0436\u043d\u044b \u0441\u043b\u0443\u0436\u0438\u0442\u044c \u043d\u043e\u043c\u0435\u0440\u0430 \u0434\u043d\u0435\u0439 \u043e\u0442 \u043d\u0430\u0447\u0430\u043b\u0430 \r\n\u043d\u0435\u0434\u0435\u043b\u0438 (\u043f\u043e\u043d\u0435\u0434\u0435\u043b\u044c\u043d\u0438\u043a - \u043f\u0435\u0440\u0432\u044b\u0439 \u0438 \u0442.\u0434.). \r\n\u0412\u044b\u0432\u0435\u0434\u0438\u0442\u0435 \u043d\u0430 \u044d\u043a\u0440\u0430\u043d \u0442\u0435\u043a\u0443\u0449\u0438\u0439 \u0434\u0435\u043d\u044c \u043d\u0435\u0434\u0435\u043b\u0438.*/\r\n\r\nlet number = prompt('\u0412\u0432\u0435\u0434\u0456\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u0434\u043d\u044f \u0442\u0438\u0436\u043d\u044f \u0447\u0438\u0441\u043b\u043e\u043c');\r\nlet week = {\r\n 1: 'monday',\r\n 2: 'tuesday',\r\n 3: 'wednesday',\r\n 4: 'thursday',\r\n 5: 'friday',\r\n 6: 'saturday',\r\n 7: 'sunday',\r\n}\r\n\r\nwhile(number < 1 || number > 7 || isNaN(number) || number === null) {\r\n if(number === null){\r\n console.log('\u0421\u043a\u0430\u0441\u043e\u0432\u0430\u043d\u043e!');\r\n break;\r\n }\r\n console.log('\u041f\u043e\u043c\u0438\u043b\u043a\u0430. \u0412\u0432\u0435\u0434\u0456\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u0434\u043d\u044f \u0442\u0438\u0436\u043d\u044f \u0447\u0438\u0441\u043b\u043e\u043c');\r\n number = prompt(\"\u0412\u0432\u0435\u0434\u0456\u0442\u044c \u043d\u043e\u043c\u0435\u0440 \u0434\u043d\u044f \u0442\u0438\u0436\u043d\u044f \u0447\u0438\u0441\u043b\u043e\u043c\");\r\n}\r\n\r\nif(number >= 1 && number <= 7){\r\n number = Math.floor(number);\r\n console.log(week[number]);\r\n}\r\n"} {"doc_id": "2b9ca386c9a3153fc06df40803bc829b", "text": "// Assignment Code\n\n\n//Variables of different letters, numbers, and symbols\nvar lowerCharArr = [\"a\", \"b\", \"c\", \"d\", \"e\", \"f\", \"g\", \"h\", \"i\", \"j\", \"k\", \"l\", \"m\", \"n\", \"o\", \"p\", \"q\", \"r\", \"s\", \"t\", \"u\", \"v\", \"w\", \"x\", \"y\", \"z\"];\nvar upperCharArr = [\"A\", \"B\", \"C\", \"D\", \"E\", \"F\", \"G\", \"H\", \"I\", \"J\", \"K\", \"L\", \"M\", \"N\", \"O\", \"P\", \"Q\", \"R\", \"S\", \"T\", \"U\", \"V\", \"W\", \"X\", \"Y\", \"Z\"];\nvar numbers = [0,1,2,3,4,5,6,7,8,9];\nvar specialChar = [\"!\", '\"', \"#\", \"$\", \"%\", \"&\", \"'\", \"(\", \")\", \"*\", \"+\", \",\", \"-\", \".\", \"/\", \":\", \";\", \"<\", \"=\", \">\", \"?\", \"@\", \"[\", '\\\\', \"]\", \"^\", \"_\", \"`\", \"{\", \"|\", \"}\", \"~\"];\n\n//Variable for button in document\nvar generateBtn = document.querySelector(\"#generate\");\n\n// Write password to the #password input\nfunction writePassword() {\n var passwordLength = prompt(\"Enter a number of characters between 8 and 128 for the password:\"); \n\n passwordLength = parseInt(passwordLength);\n if (passwordLength >= 8 && passwordLength <= 128) { \n } \n else {\n return alert(\"Invalid response. Please enter a number between 8 and 128 and DO NOT use letters\"); \n }\n//Prompts user to confirm if they want a specific set of characters to use\n var lowerAlpha = confirm(\"Would you want your password to have lowercase letters? (abcdefg....) \\n (OK = Yes) (Cancel = No)\");\n var upperAlpha = confirm(\"Would you want your password to have uppercase letters? (ABCDEFG....) \\n (OK = Yes) (Cancel = No)\");\n var numChar = confirm(\"Would you want your password to have numbers? (1234567890) \\n (OK = Yes) (Cancel = No)\");\n var specialPick = confirm(\"Do you want your password to have special characters? (~!@#$%^....) \\n (OK = Yes) (Cancel = No)\");\n\n//Creates an array for chosen characters called 'userArray' and puts them together\n var userArray = [];\n \n \n if (lowerAlpha === true) {\n userArray = userArray.concat(lowerCharArr);\n }\n if (upperAlpha === true) {\n userArray = userArray.concat(upperCharArr);\n }\n if (numChar === true) {\n userArray = userArray.concat(numbers);\n }\n if (specialPick === true) {\n userArray = userArray.concat(specialChar);\n }\n\n//this function will take the password length and userArray to perform the random math for the password text\n function generatePassword() {\n var passwordText = \"\";\n for (var i = 0; i < passwordLength; i++) {\n passwordText += userArray[Math.floor(Math.random() * userArray.length)];\n }\n return passwordText;\n }\n\n var passwordTextField = document.querySelector(\"#password\");\n\n passwordTextField.value = generatePassword();\n}\n\n// Add event listener for user click to run function to writePassword\ngenerateBtn.addEventListener(\"click\", writePassword);"} {"doc_id": "2bce935933d89d31099053551695b4a9", "text": "/*\n======================\nCounter Char Recursive\n======================\n\n[INSTRUCTION]\ncounterCharRecursive adalah sebuah function recursive yang menerima dua parameter yaitu array dan string.\nfunction ini akan mereturn jumalah char yang sesuai dengan parameter search.\n\n[EXAMPLE]\ninput: ['ada', 'apa', 'ana', 'ada']\nsearch: 'a'\n\nfunction akan mencari jumlah a dalam input\nresult: 8\n\n[RULE]\n1. WAJIB menggunakan metode recursive\n2. HANYA boleh menggunakan SATU looping\n3. dilarang menggunakan indexOf(), find(), filter()\n4. dilarang menggunakan regex\n*/\n\nfunction counterCharRecursive(input, search) {\n // console.log(input);\n if (input.length == 0) {\n return 0;\n }\n\n return finder(input[0], search) + counterCharRecursive(input.slice(1), search);\n\n}\n\n\nfunction finder(elem, a) {\n // console.log(elem);\n if (elem.length == 0) {\n return 0;\n }\n let result = ((elem[0] == a) ? 1 : 0) + finder(elem.slice(1), a);\n // console.log(typeof result);\n return result;\n\n}\n\n\n\n\n\nconsole.log(counterCharRecursive(['ada', 'apa', 'ana', 'ada'], 'a')) // 8\nconsole.log(counterCharRecursive(['xxxxoxoxoxoxoxoxxxooooo'], 'x')) // 12\nconsole.log(counterCharRecursive(['b', 'o', 'l', 'd', 'f', 'o', 'x'], 'l')) // 1\nconsole.log(counterCharRecursive(['terima', 'kasih', 'sudah', 'selalu', 'berjuang'], 's')) // 3```\n\n\n\n// console.log(finder('k0o0ool', 'o'));\n// console.log(finder('ada', 'a'));\n\nconsole.log('=====one recursive function=====');\nfunction counterCharRecursiveOne(input, search) {\n // console.log(input);\n if (input[0].length == 0) {\n // console.log('trimming=>',input);\n input.shift()\n }\n\n if (input.length == 0) {\n return 0;\n }\n let result = 0;\n if (input[0][0] == search) {\n result = 1;\n }\n\n return result + counterCharRecursiveOne([input[0].slice(1)].concat(input.slice(1)), search);\n\n}\n\n\n// function finder(elem, a) {\n// // console.log(elem);\n// if (elem.length == 0) {\n// return 0;\n// }\n// let result = ((elem[0] == a) ? 1 : 0) + finder(elem.slice(1), a);\n// // console.log(typeof result);\n// return result;\n\n// }\n\n\n\n\n\nconsole.log(counterCharRecursiveOne(['ada', 'apa', 'ana', 'ada'], 'a')) // 8\nconsole.log(counterCharRecursiveOne(['xxxxoxoxoxoxoxoxxxooooo'], 'x')) // 12\nconsole.log(counterCharRecursiveOne(['b', 'o', 'l', 'd', 'f', 'o', 'x'], 'l')) // 1\nconsole.log(counterCharRecursiveOne(['terima', 'kasih', 'sudah', 'selalu', 'berjuang'], 's')) // 3```"} {"doc_id": "2d03e66f3419c5c1f16c23af9d5b6058", "text": "/* \n\nStart Date: August 3rd 2020 - 2:30 am\nFinish Date: September 3rd 2021 - 10:02 pm\n\nDescription: \n\nThe sequence of triangle numbers is generated by adding the natural numbers. \nSo the 7th triangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:\n\n1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...\n\nLet us list the factors of the first seven triangle numbers:\n\n 1: 1\n 3: 1,3\n 6: 1,2,3,6\n10: 1,2,5,10\n15: 1,3,5,15\n21: 1,3,7,21\n28: 1,2,4,7,14,28\nWe can see that 28 is the first triangle number to have over five divisors.\n\nWhat is the value of the first triangle number to have over five hundred divisors?\n\nNotes:\n8/03/20 - 2:42 am : I wrote a function that will calculate triangle numbers. Need to check divisors when I'm not as tired.\n8/24/21 - 2:38 pm : I'm not as tired anymore. First issue is that I'm calculating every number consecutively for some reason. \nI'm going to just use a formula to find a specific triangle number. \n9/3/21 - 10:02 pm : Nvm I was still tired. Wrote a quick brute localization algorithm. \n*/\n\n\nfunction findTriangleNumber(nth) {\n let tri_num = nth * (nth + 1) / 2;\n return tri_num;\n}\n\nfunction findFactorCount(num) {\n if (num == 0) return 0;\n if (num == 1) return 1;\n\n let upper_bound = num;\n let factor_count = 2; // 1 and itself\n for (let i = 2; i < upper_bound; i++) {\n if (num % i == 0) {\n upper_bound = num / i;\n if (i != num / i) {\n factor_count += 2;\n }\n else {\n factor_count += 1;\n }\n }\n }\n return factor_count;\n}\n\nfunction bruteLocalization(target) {\n let i = 0;\n let solution = 0;\n while (solution == 0) {\n let num = findTriangleNumber(i)\n let current = findFactorCount(num);\n if (current >= target) {\n console.log(`----------------\\nPointer: ${i}\\nFactors: ${current}\\n----------------`);\n solution = num;\n }\n i++;\n }\n return solution;\n}\n\nconsole.log(`Solution: ${bruteLocalization(500)}`);"} {"doc_id": "2e4833b48ce7cf3cf794d93bb6b55988", "text": "\n// Assignment Code\nlet generateBtn = document.querySelector(\"#generate\");\n\n// Write password to the #password input\nfunction writePassword() {\n let password = generatePassword();\n let passwordText = document.querySelector(\"#password\");\n passwordText.value = password;\n\n}\n\n//Function to generate password based in user inputs\nfunction generatePassword() {\n\n // prompt user for password length\n let userLength = prompt(\"Enter a password length between 8 and 128.\");\n \n // If not a number between 8 and 128, alert user\n // This works to return the user to the main page, so they have to click Generate Password again.\n if (userLength < 8 || userLength > 128 || isNaN(userLength)) {\n alert(\"Please enter a valid selection for password length. Click Generate Password to try again.\");\n pwString = \"\";\n return;\n };\n\n //prompt user for password characters\n let userLowerCase = confirm(\"Would you like LOWER CASE letters in your password? Click OK for YES and Cancel for NO.\");\n let userUpperCase = confirm(\"Would you like UPPER CASE letters in your password? Click OK for YES and Cancel for NO.\");\n let userNumbers = confirm(\"Would you like NUMBERS in your password? Click OK for YES and Cancel for NO.\");\n let userSymbols = confirm(\"Would you like SYMBOLS in your password? Click OK for YES and Cancel for NO.\");\n\n // Strings for password\n let lowerCase = \"abcdefghijklmnopqrstuvwxyz\";\n let upperCase = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\";\n let numbers = \"0123456789\";\n let symbols = \"!*$%@+?:;()^\";\n let pwString = \"\";\n\n // Create pwString based on user inputs\n if (userLowerCase) {\n pwString = pwString + lowerCase;\n }\n\n if (userUpperCase) {\n pwString = pwString + upperCase;\n } \n\n if (userNumbers) {\n pwString = pwString + numbers;\n }\n\n if (userSymbols) {\n pwString = pwString + symbols;\n }\n\n // if pwString has no characters, alert the user\n if (pwString === \"\") {\n alert(\"A password requires some characters. Click Generate Password to try again.\");\n pwString = \"\";\n return;\n }\n\n // generate random string from StackOverflow: \n // https://stackoverflow.com/questions/1349404/generate-random-string-characters-in-javascript\n let userPassword = \"\";\n for (let i = 0; i < userLength; i++) {\n userPassword += pwString.charAt(Math.floor(Math.random() * pwString.length));\n }\n\n // console.log(userPassword);\n // userPWPrompt = alert(\"Your password is \" + userPassword);\n return userPassword;\n}\n\n// Add event listener to generate button\ngenerateBtn.addEventListener(\"click\", writePassword);\n\n// Adding copy password button from w3schools: https://www.w3schools.com/howto/howto_js_copy_clipboard.asp\nfunction copyPassword() {\n /* Get the text field */\n var copyText = document.getElementById(\"password\");\n\n /* Select the text field */\n copyText.select();\n copyText.setSelectionRange(0, 99999); /*For mobile devices*/\n\n /* Copy the text inside the text field */\n document.execCommand(\"copy\");\n\n /* Alert the copied text */\n alert(\"Copied the password: \" + copyText.value);\n}\n"} {"doc_id": "2f441cecf09f41e9cdc20bfb6afc6482", "text": "//Log your name\nconsole.log(\"Abdulmalik Yusuf\");\n\n\n/*Activity One (data types)\n=========================\n\n14 === Number\n-80 === Number\n8.5 === Number\ntrue === Boolean\n(5 < 17) === Boolean\nnull === Undefined\n3.142 === Number\n'Pi' === String\n67+18 === Number\n*/\n\n// Code that converts Integers into Binary numbers\nvar convertToBinary = function(number) {\n var quotient = number;\n var remainder = [];\n\n do {\n remainder.push (quotient % 2);\n quotient = Math.floor(quotient / 2);\n console.log(quotient);\n }\n while (quotient >= 1) {\n console.log(remainder.reverse().join(''));\n }\n}\n\nvar x = 20;\nconvertToBinary();\nconsole.log(x.toString(2));\n\n\n//Code to print student grades\nvar printGrade = function(x) {;\n\nif (x >= 70 && x <= 100) {\n console.log(x+' is A');\n} \nelse if (x >= 50 && x < 70) {\n console.log(x+' is B');\n}\nelse if (x >= 0 && x <= 49) {\n console.log(x+' is C');\n}\nelse {\n console.log('Provide numbers between 0 and 100');\n}\n};\n\nprintGrade();\n\n// Activity (2) Code that returns the number of letter \"B\"\nvar countBs = function (string) {\n var x = \"B\";\n var y = 0;\n for (var i = 0; i < (string.length); i++){\n if (string.charAt(i) === x) {\n y++;\n }\n }\n return y;\n};\n\ncountBs();\n\n\n//Activity (3)\nvar countChar =function(string, letter) {\n var letter = letter;\n var y = 0;\n for (var i = 0; i < (string.length); i++) {\n if (string.charAt(i) === letter) {\n y++;\n }\n }\n \n return y;\n};\n\ncountChar();"} {"doc_id": "2f884675ea92aa7a37457b5f41a39de9", "text": "console.group(\"Arrow Functions / Promesas / Par\u00e1metros / Objetos\");\n// OBJETOS / OBJECTS>>>>\n// (A)>\nlet name = \"Luisa\";\nlet age = 30;\n// Antes ==>\nobj = { name: name, age: age };\nconsole.log(obj);\n// ECMA6>>>\nobj2 = { name, age };\nconsole.log(obj2);\n\n// (B)>\n// Arrow Functions >>\nconst names = [\n { name: `Chris`, age: 30 },\n { name: `Luisa`, age: 21 },\n];\n// Antes==>\nlet listOfNames = names.map(function (item, index) {\n console.log(item.name, index);\n});\n// ECMA6>>>\n// Arrow Functions en variables/method>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>\n//(1)\nlet listOfNamesTwo = names.map((item, index) => console.log(item.name, index));\n\n// (C)> (2)\npeopleTwo = (user) => {\n return user;\n};\n\nconst peopleThree = (name) => {\n return name;\n};\nconsole.log(peopleThree(\"An\u00edbal\"));\n\n// (D)> (3)\nconst listOfNamesFour = (name) => {\n //Code\n};\n\n// (E)> (4)\nconst square = (num) => num * num;\nconsole.log(square(5));\n\n// Promesas / Promise ======================================================================================\n// (1)\nconst hello = () => {\n // Ejecutar Promesa >\n return new Promise((resolve, reject) => {\n //code>\n if (true) {\n resolve(`Hello Resolved`);\n } else {\n reject(`No!!!!!`);\n }\n });\n};\n//EJECUTAR PROMESA=>\nhello()\n .then((response) => console.log(response))\n .catch((error) => console.error(error));\n//> EXERCISE:\nconst friend = (one) => {\n return new Promise((resolve, reject) => {\n if (one === \"Alma\") {\n true;\n resolve(`Yes! I\u00b4ts she!!!`);\n } else {\n reject(`S\u00f3lo es un sue\u00f1o`);\n }\n });\n};\nfriend(\"Alma\")\n .then((response) => console.log(response))\n .catch((error) => console.error(error));\n\nconsole.groupEnd();\n"} {"doc_id": "309351cb44a0b7c92c112e1e033d8202", "text": "class Calculator {\n factor(n) {\n if (n <= 1) {\n return 1;\n } else {\n return this.factor(n - 1) * n;\n }\n }\n}\n\nconsole.log(\"Hello!\");\n\nconst arr = [1, 2, 3];\narr.push(4);\n\nconsole.log(`n = ${arr.length}, arr[0] = ${arr[0]}`);\n\nconst map = {\n a: 2,\n b: 3\n};\nconsole.log(`map['a'] = ${map[\"a\"]}, arr[1] = ${arr[1]}`);\n\nif (arr[0] == 1) {\n console.log(\"TRUE-X\");\n} else {\n console.log(\"FALSE\");\n}\n\nlet sum = 0;\nfor (let i = 0; i < 10; i++) {\n sum += i + 2;\n}\nconsole.log(sum);\n\nconst calc = new Calculator();\nconsole.log(`5! = ${calc.factor(5)}`);"} {"doc_id": "33441382ea29ec6afef3ffb3765e5d8e", "text": "//Problem 7. First larger than neighbours\n//Write a function that returns the index of the first element in array that is larger than its neighbours or -1, if there\ufffds no such element.\n// Use the function from the previous exercise.\n\nvar numbers = [1, 1, 1, 1, 5, 6, 7, 8, 8, 2, 12];\n\nisBiggerThanNeighbours(numbers);\n\nfunction isBiggerThanNeighbours(numbers){\n numbers = numbers || [];\n\n var len = numbers.length,\n position;\n\n for (position = 0; position < len; position += 1) {\n\n if (!(position)) {\n if (numbers[position] > numbers[position + 1]) {\n return console.log('The position of element is: ' + position);\n }\n } else if (position === (len - 1)) {\n if (numbers[position] > numbers[position - 1]) {\n return console.log('The position of element is: ' + position);\n } else {\n return console.log(-1);\n }\n } else {\n if ((numbers[position] > numbers[position - 1]) && (numbers[position] > numbers[position + 1])) {\n return console.log('The position of element is: ' + position);\n }\n }\n }\n}\n\n\n"} {"doc_id": "349a76cf0263f9cce6f50516ea53f367", "text": "var bool1 = true; // true\nvar bool2 = false; // false\nvar bool3 = 1 === 1; // true\nconsole.log(bool1); // prints true\nconsole.log(bool2); // prints false\nconsole.log(bool3); // prints true\nvar bool4 = Boolean(\"true\"); // true\nvar bool5 = Boolean(\"TRUE\"); // true\nvar bool6 = Boolean(\"false\"); // true\nvar bool7 = Boolean(\"FALSE\"); // true\nvar bool8 = Boolean(NaN); // false\nvar bool9 = new Boolean(\"true\").valueOf(); // true\nvar bool10 = new Boolean(\"false\").valueOf(); // true\nvar bool11 = \"true\"; // true\nvar bool12 = \"false\"; // false\nconsole.log(bool4); // prints true \nconsole.log(bool5); // prints false\nconsole.log(bool6); // prints true\nconsole.log(bool7); // prints true\nconsole.log(bool8); // prints false\nconsole.log(bool9); // prints true\nconsole.log(bool10); // prints true\nconsole.log(bool11); // prints true\nconsole.log(bool12); // prints false\nvar bool13 = isTrue(\"true\"); // true\nvar bool14 = isTrue(\"false\"); // false\nfunction isTrue(s) {\n return s.toLocaleLowerCase() === \"true\";\n}\nconsole.log(bool13); // prints true\nconsole.log(bool14); // prints false\n"} {"doc_id": "34babb3d302e22f375f3450ea617d071", "text": "// tyhle prikazy vypisou zpravu do konzole\r\nconsole.log('Ahoj sv\u011bte!');\r\n\r\n/* */\r\nconsole.error('Hele chyba!');\r\nconsole.warn('Upozorneni!');\r\n\r\n\r\n// var, let, const\r\n\r\nlet age = 42;\r\nconsole.log(age);\r\n\r\nconst height = 170;\r\nconsole.log(height);\r\n\r\nage = 12;\r\nconsole.log(age);\r\n\r\n// height = 100;\r\n// console.log(height);\r\n\r\n\r\n// String, Number, Boolean, null, undefined, Symbol\r\n\r\nconst pi = 3.14; // Number - integer, double, float\r\nconst message = 'Ahoj, jmenuji se Pepa a chci se nau\u010dit JavaScript!';\r\n// \"Ahoj, jmenuji se Pepa a chci se nau\u010dit JavaScript!\";\r\n// `Ahoj, jmenuji se Pepa a chci se nau\u010dit JavaScript!`;\r\n\r\nconsole.log(message);\r\n\r\nconst isLive = true;\r\nconst isMarried = false;\r\n\r\nconsole.log(isLive, isMarried);\r\n\r\nconst something = null;\r\nconst somethingElse = undefined;\r\n\r\nconsole.log(something, somethingElse);\r\n\r\nconsole.log(typeof isLive);\r\nconsole.log(typeof message);\r\nconsole.log(typeof pi);\r\n\r\nconsole.log(Symbol('Toto je Symbor, vsichni vidime.'));\r\n\r\n\r\n// Strings / retezce\r\n\r\nconst string1 = 'Ahoj';\r\nconst string2 = 'svete';\r\n// concatenation / zretezeni;\r\nconst string = string1 + ' ' + string2;\r\nconsole.log(string);\r\n\r\nconsole.log('Hodnota PI = ' + pi);\r\n\r\nconst myAge = 28;\r\nconst myName = 'Bachqwecs';\r\nconst str = `Ahoj, jmenuji se ${myName}, a je mi ${myAge} let`;\r\nconsole.log(str);\r\n\r\nconsole.log(myName.length);\r\nconsole.log(myName.toUpperCase());\r\nconsole.log(myName.toLowerCase());\r\n\r\n\r\nconsole.log(myName.slice(0, 2));\r\nconsole.log(myName.slice(1, 3));\r\nconsole.log(myName.split(''));\r\nconsole.log(myName.split('c'));\r\n\r\n\r\n// pole / arrays\r\nconst fruits = ['ananas', 'banana', 'melon', 'strawberry', null, undefined, false];\r\nconsole.log('We have ' + fruits.length + ' fruits');\r\n\r\nconst numbers = [0, 1, 2, 3, 4];\r\nconsole.log(numbers);\r\n\r\nconsole.log(fruits[0]);\r\nconsole.log(fruits[1]);\r\nconsole.log(fruits[2]);\r\n\r\nconsole.log('------------------------------')\r\nfor (let i = 0; i < fruits.length; i++) {\r\n console.log(fruits[i]);\r\n}\r\n\r\nconsole.log('------------------------------')\r\nlet i = 0;\r\nwhile (i < fruits.length) {\r\n console.log(i, fruits[i]);\r\n i++;\r\n}\r\n\r\nfruits.push('orange');\r\nfruits.pop();\r\nconsole.log(fruits);\r\nfruits.shift();\r\nconsole.log(fruits);\r\nfruits.unshift('apple');\r\nconsole.log(fruits);\r\n\r\n\r\n// objects\r\nconst person = {\r\n age: 42,\r\n nationality: 'Czech Republic',\r\n name: 'Pepa',\r\n surname: 'Mach',\r\n};\r\nconsole.log(person.name + ' ' + person.surname);\r\n\r\nconst people = [\r\n {\r\n age: 12,\r\n name: 'David',\r\n },\r\n {\r\n age: 22,\r\n name: 'Jane',\r\n },\r\n {\r\n age: 16,\r\n name: 'Carl',\r\n },\r\n {\r\n age: 72,\r\n name: 'George',\r\n },\r\n];\r\n\r\n\r\nfor (let i = 0; i < people.length; i++) {\r\n console.log(people[i].name + ' ' + people[i].age);\r\n}\r\n\r\n/*people.forEach((person) => {\r\n console.log(person);\r\n});*/\r\n\r\n\r\nconsole.log('-------')\r\nfor (const person of people) {\r\n console.log(person);\r\n}\r\n\r\n// conditionals, if else\r\n\r\nif (isMarried) {\r\n console.log(123);\r\n} else {\r\n console.log(456);\r\n}\r\n\r\nfunction mojeFunkce(a, b) {\r\n return a + b;\r\n}\r\n\r\nconst tvojeFunkce = (a, b) => {\r\n return a + b;\r\n}\r\n\r\n\r\n// functions\r\n// const add = function () {};\r\nconst add = (a, b) => {\r\n // const result = a + b;\r\n // return result;\r\n return a + b;\r\n};\r\n\r\nconsole.log(add(2, 3));\r\n\r\n\r\n"} {"doc_id": "350829d6161560daf02bf61982778472", "text": "/*\n
\nEquations are given in the format A / B = k, where A and B are variables represented as strings, and k is a real number (floating point number). Given some queries, return the answers. If the answer does not exist, return -1.0.\n
Example:
\nGiven a / b = 2.0, b / c = 3.0.
queries are: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ? .
return [6.0, 0.5, -1.0, 1.0, -1.0 ].\n
\nThe input is: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries , where equations.size() == values.size(), and the values are positive. This represents the equations. Return vector<double>.\n
According to the example above:\n
equations = [ [\"a\", \"b\"], [\"b\", \"c\"] ],\nvalues = [2.0, 3.0],\nqueries = [ [\"a\", \"c\"], [\"b\", \"a\"], [\"a\", \"e\"], [\"a\", \"a\"], [\"x\", \"x\"] ].\n\n\n
\nThe input is always valid. You may assume that evaluating the queries will result in no division by zero and there is no contradiction.\n
\u7ed9\u51fa\u65b9\u7a0b\u5f0f A / B = k, \u5176\u4e2d A \u548c B \u5747\u4e3a\u4ee3\u8868\u5b57\u7b26\u4e32\u7684\u53d8\u91cf\uff0c k \u662f\u4e00\u4e2a\u6d6e\u70b9\u578b\u6570\u5b57\u3002\u6839\u636e\u5df2\u77e5\u65b9\u7a0b\u5f0f\u6c42\u89e3\u95ee\u9898\uff0c\u5e76\u8fd4\u56de\u8ba1\u7b97\u7ed3\u679c\u3002\u5982\u679c\u7ed3\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1.0\u3002
\u793a\u4f8b :
\n\u7ed9\u5b9a a / b = 2.0, b / c = 3.0
\n\u95ee\u9898: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
\n\u8fd4\u56de [6.0, 0.5, -1.0, 1.0, -1.0 ]
\u8f93\u5165\u4e3a: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries(\u65b9\u7a0b\u5f0f\uff0c\u65b9\u7a0b\u5f0f\u7ed3\u679c\uff0c\u95ee\u9898\u65b9\u7a0b\u5f0f)\uff0c \u5176\u4e2d equations.size() == values.size()\uff0c\u5373\u65b9\u7a0b\u5f0f\u7684\u957f\u5ea6\u4e0e\u65b9\u7a0b\u5f0f\u7ed3\u679c\u957f\u5ea6\u76f8\u7b49\uff08\u7a0b\u5f0f\u4e0e\u7ed3\u679c\u4e00\u4e00\u5bf9\u5e94\uff09\uff0c\u5e76\u4e14\u7ed3\u679c\u503c\u5747\u4e3a\u6b63\u6570\u3002\u4ee5\u4e0a\u4e3a\u65b9\u7a0b\u5f0f\u7684\u63cf\u8ff0\u3002 \u8fd4\u56devector<double>\u7c7b\u578b\u3002
\u57fa\u4e8e\u4e0a\u8ff0\u4f8b\u5b50\uff0c\u8f93\u5165\u5982\u4e0b\uff1a
\n\n\nequations(\u65b9\u7a0b\u5f0f) = [ ["a", "b"], ["b", "c"] ],\nvalues(\u65b9\u7a0b\u5f0f\u7ed3\u679c) = [2.0, 3.0],\nqueries(\u95ee\u9898\u65b9\u7a0b\u5f0f) = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. \n\n\n
\u8f93\u5165\u603b\u662f\u6709\u6548\u7684\u3002\u4f60\u53ef\u4ee5\u5047\u8bbe\u9664\u6cd5\u8fd0\u7b97\u4e2d\u4e0d\u4f1a\u51fa\u73b0\u9664\u6570\u4e3a0\u7684\u60c5\u51b5\uff0c\u4e14\u4e0d\u5b58\u5728\u4efb\u4f55\u77db\u76fe\u7684\u7ed3\u679c\u3002
\n\u7ed9\u51fa\u65b9\u7a0b\u5f0f A / B = k, \u5176\u4e2d A \u548c B \u5747\u4e3a\u4ee3\u8868\u5b57\u7b26\u4e32\u7684\u53d8\u91cf\uff0c k \u662f\u4e00\u4e2a\u6d6e\u70b9\u578b\u6570\u5b57\u3002\u6839\u636e\u5df2\u77e5\u65b9\u7a0b\u5f0f\u6c42\u89e3\u95ee\u9898\uff0c\u5e76\u8fd4\u56de\u8ba1\u7b97\u7ed3\u679c\u3002\u5982\u679c\u7ed3\u679c\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de -1.0\u3002
\u793a\u4f8b :
\n\u7ed9\u5b9a a / b = 2.0, b / c = 3.0
\n\u95ee\u9898: a / c = ?, b / a = ?, a / e = ?, a / a = ?, x / x = ?
\n\u8fd4\u56de [6.0, 0.5, -1.0, 1.0, -1.0 ]
\u8f93\u5165\u4e3a: vector<pair<string, string>> equations, vector<double>& values, vector<pair<string, string>> queries(\u65b9\u7a0b\u5f0f\uff0c\u65b9\u7a0b\u5f0f\u7ed3\u679c\uff0c\u95ee\u9898\u65b9\u7a0b\u5f0f)\uff0c \u5176\u4e2d equations.size() == values.size()\uff0c\u5373\u65b9\u7a0b\u5f0f\u7684\u957f\u5ea6\u4e0e\u65b9\u7a0b\u5f0f\u7ed3\u679c\u957f\u5ea6\u76f8\u7b49\uff08\u7a0b\u5f0f\u4e0e\u7ed3\u679c\u4e00\u4e00\u5bf9\u5e94\uff09\uff0c\u5e76\u4e14\u7ed3\u679c\u503c\u5747\u4e3a\u6b63\u6570\u3002\u4ee5\u4e0a\u4e3a\u65b9\u7a0b\u5f0f\u7684\u63cf\u8ff0\u3002 \u8fd4\u56devector<double>\u7c7b\u578b\u3002
\u57fa\u4e8e\u4e0a\u8ff0\u4f8b\u5b50\uff0c\u8f93\u5165\u5982\u4e0b\uff1a
\n\n\nequations(\u65b9\u7a0b\u5f0f) = [ ["a", "b"], ["b", "c"] ],\nvalues(\u65b9\u7a0b\u5f0f\u7ed3\u679c) = [2.0, 3.0],\nqueries(\u95ee\u9898\u65b9\u7a0b\u5f0f) = [ ["a", "c"], ["b", "a"], ["a", "e"], ["a", "a"], ["x", "x"] ]. \n\n\n
\u8f93\u5165\u603b\u662f\u6709\u6548\u7684\u3002\u4f60\u53ef\u4ee5\u5047\u8bbe\u9664\u6cd5\u8fd0\u7b97\u4e2d\u4e0d\u4f1a\u51fa\u73b0\u9664\u6570\u4e3a0\u7684\u60c5\u51b5\uff0c\u4e14\u4e0d\u5b58\u5728\u4efb\u4f55\u77db\u76fe\u7684\u7ed3\u679c\u3002
\n*/\n\n\n/**\n * @param {string[][]} equations\n * @param {number[]} values\n * @param {string[][]} queries\n * @return {number[]}\n */\nvar calcEquation = function(equations, values, queries) {\n \n};"} {"doc_id": "35325bcea0123ea621ebcf6994dda010", "text": "/*\nGiven the running logs of n functions that are executed in a nonpreemptive single threaded CPU, find the exclusive time of these functions.
\n\nEach function has a unique id, start from 0 to n-1. A function may be called recursively or by another function.
\n\nA log is a string has this format : function_id:start_or_end:timestamp. For example, \"0:start:0\" means function 0 starts from the very beginning of time 0. \"0:end:0\" means function 0 ends to the very end of time 0.
Exclusive time of a function is defined as the time spent within this function, the time spent by calling other functions should not be considered as this function's exclusive time. You should return the exclusive time of each function sorted by their function id.
\n\nExample 1:
\n
\nInput:\nn = 2\nlogs = \n[\"0:start:0\",\n \"1:start:2\",\n \"1:end:5\",\n \"0:end:6\"]\nOutput:[3, 4]\nExplanation:\nFunction 0 starts at time 0, then it executes 2 units of time and reaches the end of time 1. \nNow function 0 calls function 1, function 1 starts at time 2, executes 4 units of time and end at time 5.\nFunction 0 is running again at time 6, and also end at the time 6, thus executes 1 unit of time. \nSo function 0 totally execute 2 + 1 = 3 units of time, and function 1 totally execute 4 units of time.\n\n\n\n
Note:
\n
\u7ed9\u51fa\u4e00\u4e2a\u975e\u62a2\u5360\u5355\u7ebf\u7a0bCPU\u7684 n \u4e2a\u51fd\u6570\u8fd0\u884c\u65e5\u5fd7\uff0c\u627e\u5230\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u3002
\n\n\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684 Id\uff0c\u4ece 0 \u5230 n-1\uff0c\u51fd\u6570\u53ef\u80fd\u4f1a\u9012\u5f52\u8c03\u7528\u6216\u8005\u88ab\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u3002
\n\n\u65e5\u5fd7\u662f\u5177\u6709\u4ee5\u4e0b\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1afunction_id\uff1astart_or_end\uff1atimestamp\u3002\u4f8b\u5982\uff1a"0:start:0" \u8868\u793a\u51fd\u6570 0 \u4ece 0 \u65f6\u523b\u5f00\u59cb\u8fd0\u884c\u3002"0:end:0" \u8868\u793a\u51fd\u6570 0 \u5728 0 \u65f6\u523b\u7ed3\u675f\u3002
\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u5b9a\u4e49\u662f\u5728\u8be5\u65b9\u6cd5\u4e2d\u82b1\u8d39\u7684\u65f6\u95f4\uff0c\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u82b1\u8d39\u7684\u65f6\u95f4\u4e0d\u7b97\u8be5\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u3002\u4f60\u9700\u8981\u6839\u636e\u51fd\u6570\u7684 Id \u6709\u5e8f\u5730\u8fd4\u56de\u6bcf\u4e2a\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165:\nn = 2\nlogs = \n["0:start:0",\n "1:start:2",\n "1:end:5",\n "0:end:6"]\n\u8f93\u51fa:[3, 4]\n\u8bf4\u660e\uff1a\n\u51fd\u6570 0 \u5728\u65f6\u523b 0 \u5f00\u59cb\uff0c\u5728\u6267\u884c\u4e86 2\u4e2a\u65f6\u95f4\u5355\u4f4d\u7ed3\u675f\u4e8e\u65f6\u523b 1\u3002\n\u73b0\u5728\u51fd\u6570 0 \u8c03\u7528\u51fd\u6570 1\uff0c\u51fd\u6570 1 \u5728\u65f6\u523b 2 \u5f00\u59cb\uff0c\u6267\u884c 4 \u4e2a\u65f6\u95f4\u5355\u4f4d\u540e\u7ed3\u675f\u4e8e\u65f6\u523b 5\u3002\n\u51fd\u6570 0 \u518d\u6b21\u5728\u65f6\u523b 6 \u5f00\u59cb\u6267\u884c\uff0c\u5e76\u5728\u65f6\u523b 6 \u7ed3\u675f\u8fd0\u884c\uff0c\u4ece\u800c\u6267\u884c\u4e86 1 \u4e2a\u65f6\u95f4\u5355\u4f4d\u3002\n\u6240\u4ee5\u51fd\u6570 0 \u603b\u5171\u7684\u6267\u884c\u4e86 2 +1 =3 \u4e2a\u65f6\u95f4\u5355\u4f4d\uff0c\u51fd\u6570 1 \u603b\u5171\u6267\u884c\u4e86 4 \u4e2a\u65f6\u95f4\u5355\u4f4d\u3002\n\n\n
\u8bf4\u660e\uff1a
\n\n\u7ed9\u51fa\u4e00\u4e2a\u975e\u62a2\u5360\u5355\u7ebf\u7a0bCPU\u7684 n \u4e2a\u51fd\u6570\u8fd0\u884c\u65e5\u5fd7\uff0c\u627e\u5230\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u3002
\n\n\u6bcf\u4e2a\u51fd\u6570\u90fd\u6709\u4e00\u4e2a\u552f\u4e00\u7684 Id\uff0c\u4ece 0 \u5230 n-1\uff0c\u51fd\u6570\u53ef\u80fd\u4f1a\u9012\u5f52\u8c03\u7528\u6216\u8005\u88ab\u5176\u4ed6\u51fd\u6570\u8c03\u7528\u3002
\n\n\u65e5\u5fd7\u662f\u5177\u6709\u4ee5\u4e0b\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff1afunction_id\uff1astart_or_end\uff1atimestamp\u3002\u4f8b\u5982\uff1a"0:start:0" \u8868\u793a\u51fd\u6570 0 \u4ece 0 \u65f6\u523b\u5f00\u59cb\u8fd0\u884c\u3002"0:end:0" \u8868\u793a\u51fd\u6570 0 \u5728 0 \u65f6\u523b\u7ed3\u675f\u3002
\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u5b9a\u4e49\u662f\u5728\u8be5\u65b9\u6cd5\u4e2d\u82b1\u8d39\u7684\u65f6\u95f4\uff0c\u8c03\u7528\u5176\u4ed6\u51fd\u6570\u82b1\u8d39\u7684\u65f6\u95f4\u4e0d\u7b97\u8be5\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u3002\u4f60\u9700\u8981\u6839\u636e\u51fd\u6570\u7684 Id \u6709\u5e8f\u5730\u8fd4\u56de\u6bcf\u4e2a\u51fd\u6570\u7684\u72ec\u5360\u65f6\u95f4\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165:\nn = 2\nlogs = \n["0:start:0",\n "1:start:2",\n "1:end:5",\n "0:end:6"]\n\u8f93\u51fa:[3, 4]\n\u8bf4\u660e\uff1a\n\u51fd\u6570 0 \u5728\u65f6\u523b 0 \u5f00\u59cb\uff0c\u5728\u6267\u884c\u4e86 2\u4e2a\u65f6\u95f4\u5355\u4f4d\u7ed3\u675f\u4e8e\u65f6\u523b 1\u3002\n\u73b0\u5728\u51fd\u6570 0 \u8c03\u7528\u51fd\u6570 1\uff0c\u51fd\u6570 1 \u5728\u65f6\u523b 2 \u5f00\u59cb\uff0c\u6267\u884c 4 \u4e2a\u65f6\u95f4\u5355\u4f4d\u540e\u7ed3\u675f\u4e8e\u65f6\u523b 5\u3002\n\u51fd\u6570 0 \u518d\u6b21\u5728\u65f6\u523b 6 \u5f00\u59cb\u6267\u884c\uff0c\u5e76\u5728\u65f6\u523b 6 \u7ed3\u675f\u8fd0\u884c\uff0c\u4ece\u800c\u6267\u884c\u4e86 1 \u4e2a\u65f6\u95f4\u5355\u4f4d\u3002\n\u6240\u4ee5\u51fd\u6570 0 \u603b\u5171\u7684\u6267\u884c\u4e86 2 +1 =3 \u4e2a\u65f6\u95f4\u5355\u4f4d\uff0c\u51fd\u6570 1 \u603b\u5171\u6267\u884c\u4e86 4 \u4e2a\u65f6\u95f4\u5355\u4f4d\u3002\n\n\n
\u8bf4\u660e\uff1a
\n\nTo every action, there is an equal and opposite reaction.
\n // b.We have two integer sequences A and B of the same non-zero length.
We are allowed to swap elements A[i] and B[i]. Note that both elements are in the same index position in their respective sequences.
At the end of some number of swaps, A and B are both strictly increasing. (A sequence is strictly increasing if and only if A[0] < A[1] < A[2] < ... < A[A.length - 1].)
Given A and B, return the minimum number of swaps to make both sequences strictly increasing. It is guaranteed that the given input always makes it possible.
\n\n\nExample:\nInput: A = [1,3,5,4], B = [1,2,3,7]\nOutput: 1\nExplanation: \nSwap A[3] and B[3]. Then the sequences are:\nA = [1, 3, 5, 7] and B = [1, 2, 3, 4]\nwhich are both strictly increasing.\n\n\n
Note:
\n\nA, B are arrays with the same length, and that length will be in the range [1, 1000].A[i], B[i] are integer values in the range [0, 2000].\u6211\u4eec\u6709\u4e24\u4e2a\u957f\u5ea6\u76f8\u7b49\u4e14\u4e0d\u4e3a\u7a7a\u7684\u6574\u578b\u6570\u7ec4 A \u548c B \u3002
\u6211\u4eec\u53ef\u4ee5\u4ea4\u6362 A[i] \u548c B[i] \u7684\u5143\u7d20\u3002\u6ce8\u610f\u8fd9\u4e24\u4e2a\u5143\u7d20\u5728\u5404\u81ea\u7684\u5e8f\u5217\u4e2d\u5e94\u8be5\u5904\u4e8e\u76f8\u540c\u7684\u4f4d\u7f6e\u3002
\u5728\u4ea4\u6362\u8fc7\u4e00\u4e9b\u5143\u7d20\u4e4b\u540e\uff0c\u6570\u7ec4 A \u548c B \u90fd\u5e94\u8be5\u662f\u4e25\u683c\u9012\u589e\u7684\uff08\u6570\u7ec4\u4e25\u683c\u9012\u589e\u7684\u6761\u4ef6\u4ec5\u4e3aA[0] < A[1] < A[2] < ... < A[A.length - 1]\uff09\u3002
\u7ed9\u5b9a\u6570\u7ec4 A \u548c B \uff0c\u8bf7\u8fd4\u56de\u4f7f\u5f97\u4e24\u4e2a\u6570\u7ec4\u5747\u4fdd\u6301\u4e25\u683c\u9012\u589e\u72b6\u6001\u7684\u6700\u5c0f\u4ea4\u6362\u6b21\u6570\u3002\u5047\u8bbe\u7ed9\u5b9a\u7684\u8f93\u5165\u603b\u662f\u6709\u6548\u7684\u3002
\n\u793a\u4f8b:\n\u8f93\u5165: A = [1,3,5,4], B = [1,2,3,7]\n\u8f93\u51fa: 1\n\u89e3\u91ca: \n\u4ea4\u6362 A[3] \u548c B[3] \u540e\uff0c\u4e24\u4e2a\u6570\u7ec4\u5982\u4e0b:\nA = [1, 3, 5, 7] \uff0c B = [1, 2, 3, 4]\n\u4e24\u4e2a\u6570\u7ec4\u5747\u4e3a\u4e25\u683c\u9012\u589e\u7684\u3002\n\n
\u6ce8\u610f:
\n\nA, B \u4e24\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u603b\u662f\u76f8\u7b49\u7684\uff0c\u4e14\u957f\u5ea6\u7684\u8303\u56f4\u4e3a [1, 1000]\u3002A[i], B[i] \u5747\u4e3a [0, 2000]\u533a\u95f4\u5185\u7684\u6574\u6570\u3002\u6211\u4eec\u6709\u4e24\u4e2a\u957f\u5ea6\u76f8\u7b49\u4e14\u4e0d\u4e3a\u7a7a\u7684\u6574\u578b\u6570\u7ec4 A \u548c B \u3002
\u6211\u4eec\u53ef\u4ee5\u4ea4\u6362 A[i] \u548c B[i] \u7684\u5143\u7d20\u3002\u6ce8\u610f\u8fd9\u4e24\u4e2a\u5143\u7d20\u5728\u5404\u81ea\u7684\u5e8f\u5217\u4e2d\u5e94\u8be5\u5904\u4e8e\u76f8\u540c\u7684\u4f4d\u7f6e\u3002
\u5728\u4ea4\u6362\u8fc7\u4e00\u4e9b\u5143\u7d20\u4e4b\u540e\uff0c\u6570\u7ec4 A \u548c B \u90fd\u5e94\u8be5\u662f\u4e25\u683c\u9012\u589e\u7684\uff08\u6570\u7ec4\u4e25\u683c\u9012\u589e\u7684\u6761\u4ef6\u4ec5\u4e3aA[0] < A[1] < A[2] < ... < A[A.length - 1]\uff09\u3002
\u7ed9\u5b9a\u6570\u7ec4 A \u548c B \uff0c\u8bf7\u8fd4\u56de\u4f7f\u5f97\u4e24\u4e2a\u6570\u7ec4\u5747\u4fdd\u6301\u4e25\u683c\u9012\u589e\u72b6\u6001\u7684\u6700\u5c0f\u4ea4\u6362\u6b21\u6570\u3002\u5047\u8bbe\u7ed9\u5b9a\u7684\u8f93\u5165\u603b\u662f\u6709\u6548\u7684\u3002
\n\u793a\u4f8b:\n\u8f93\u5165: A = [1,3,5,4], B = [1,2,3,7]\n\u8f93\u51fa: 1\n\u89e3\u91ca: \n\u4ea4\u6362 A[3] \u548c B[3] \u540e\uff0c\u4e24\u4e2a\u6570\u7ec4\u5982\u4e0b:\nA = [1, 3, 5, 7] \uff0c B = [1, 2, 3, 4]\n\u4e24\u4e2a\u6570\u7ec4\u5747\u4e3a\u4e25\u683c\u9012\u589e\u7684\u3002\n\n
\u6ce8\u610f:
\n\nA, B \u4e24\u4e2a\u6570\u7ec4\u7684\u957f\u5ea6\u603b\u662f\u76f8\u7b49\u7684\uff0c\u4e14\u957f\u5ea6\u7684\u8303\u56f4\u4e3a [1, 1000]\u3002A[i], B[i] \u5747\u4e3a [0, 2000]\u533a\u95f4\u5185\u7684\u6574\u6570\u3002Given a non-negative integer num represented as a string, remove k digits from the number so that the new number is the smallest possible.\n
\n\nNote:
\n
Example 1:\n
\nInput: num = \"1432219\", k = 3\nOutput: \"1219\"\nExplanation: Remove the three digits 4, 3, and 2 to form the new number 1219 which is the smallest.\n\n\n\n
Example 2:\n
\nInput: num = \"10200\", k = 1\nOutput: \"200\"\nExplanation: Remove the leading 1 and the number is 200. Note that the output must not contain leading zeroes.\n\n\n\n
Example 3:\n
\nInput: num = \"10\", k = 2\nOutput: \"0\"\nExplanation: Remove all the digits from the number and it is left with nothing which is 0.\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u4ee5\u5b57\u7b26\u4e32\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570 num\uff0c\u79fb\u9664\u8fd9\u4e2a\u6570\u4e2d\u7684 k \u4f4d\u6570\u5b57\uff0c\u4f7f\u5f97\u5269\u4e0b\u7684\u6570\u5b57\u6700\u5c0f\u3002
\n\n\u6ce8\u610f:
\n\n\u793a\u4f8b 1 :
\n\n\n\u8f93\u5165: num = "1432219", k = 3\n\u8f93\u51fa: "1219"\n\u89e3\u91ca: \u79fb\u9664\u6389\u4e09\u4e2a\u6570\u5b57 4, 3, \u548c 2 \u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u6700\u5c0f\u7684\u6570\u5b57 1219\u3002\n\n\n
\u793a\u4f8b 2 :
\n\n\n\u8f93\u5165: num = "10200", k = 1\n\u8f93\u51fa: "200"\n\u89e3\u91ca: \u79fb\u6389\u9996\u4f4d\u7684 1 \u5269\u4e0b\u7684\u6570\u5b57\u4e3a 200. \u6ce8\u610f\u8f93\u51fa\u4e0d\u80fd\u6709\u4efb\u4f55\u524d\u5bfc\u96f6\u3002\n\n\n
\u793a\u4f8b 3 :
\n\n\n\u8f93\u5165: num = "10", k = 2\n\u8f93\u51fa: "0"\n\u89e3\u91ca: \u4ece\u539f\u6570\u5b57\u79fb\u9664\u6240\u6709\u7684\u6570\u5b57\uff0c\u5269\u4f59\u4e3a\u7a7a\u5c31\u662f0\u3002\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u4ee5\u5b57\u7b26\u4e32\u8868\u793a\u7684\u975e\u8d1f\u6574\u6570 num\uff0c\u79fb\u9664\u8fd9\u4e2a\u6570\u4e2d\u7684 k \u4f4d\u6570\u5b57\uff0c\u4f7f\u5f97\u5269\u4e0b\u7684\u6570\u5b57\u6700\u5c0f\u3002
\n\n\u6ce8\u610f:
\n\n\u793a\u4f8b 1 :
\n\n\n\u8f93\u5165: num = "1432219", k = 3\n\u8f93\u51fa: "1219"\n\u89e3\u91ca: \u79fb\u9664\u6389\u4e09\u4e2a\u6570\u5b57 4, 3, \u548c 2 \u5f62\u6210\u4e00\u4e2a\u65b0\u7684\u6700\u5c0f\u7684\u6570\u5b57 1219\u3002\n\n\n
\u793a\u4f8b 2 :
\n\n\n\u8f93\u5165: num = "10200", k = 1\n\u8f93\u51fa: "200"\n\u89e3\u91ca: \u79fb\u6389\u9996\u4f4d\u7684 1 \u5269\u4e0b\u7684\u6570\u5b57\u4e3a 200. \u6ce8\u610f\u8f93\u51fa\u4e0d\u80fd\u6709\u4efb\u4f55\u524d\u5bfc\u96f6\u3002\n\n\n
\u793a\u4f8b 3 :
\n\n\n\u8f93\u5165: num = "10", k = 2\n\u8f93\u51fa: "0"\n\u89e3\u91ca: \u4ece\u539f\u6570\u5b57\u79fb\u9664\u6240\u6709\u7684\u6570\u5b57\uff0c\u5269\u4f59\u4e3a\u7a7a\u5c31\u662f0\u3002\n\n*/\n\n\n/**\n * @param {string} num\n * @param {number} k\n * @return {string}\n */\nvar removeKdigits = function(num, k) {\n \n};"} {"doc_id": "377cdde18ed4c6d2abb1fe93bb062385", "text": "/*\n
Let's call any (contiguous) subarray B (of A) a mountain if the following properties hold:
\n\nB.length >= 30 < i < B.length - 1 such that B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1](Note that B could be any subarray of A, including the entire array A.)
\n\nGiven an array A of integers, return the length of the longest mountain.
Return 0 if there is no mountain.
\n\n
Example 1:
\n\n\nInput: [2,1,4,7,3,2,5]\nOutput: 5\nExplanation: The largest mountain is [1,4,7,3,2] which has length 5.\n\n\n
Example 2:
\n\n\nInput: [2,2,2]\nOutput: 0\nExplanation: There is no mountain.\n\n\n
\n\n
Note:
\n\n0 <= A.length <= 100000 <= A[i] <= 10000\u6211\u4eec\u628a\u6570\u7ec4 A \u4e2d\u7b26\u5408\u4e0b\u5217\u5c5e\u6027\u7684\u4efb\u610f\u8fde\u7eed\u5b50\u6570\u7ec4 B \u79f0\u4e3a “\u5c71\u8109”\uff1a
\n\nB.length >= 30 < i < B.length - 1 \u4f7f\u5f97 B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]\uff08\u6ce8\u610f\uff1aB \u53ef\u4ee5\u662f A \u7684\u4efb\u610f\u5b50\u6570\u7ec4\uff0c\u5305\u62ec\u6574\u4e2a\u6570\u7ec4 A\u3002\uff09
\n\n\u7ed9\u51fa\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 A\uff0c\u8fd4\u56de\u6700\u957f “\u5c71\u8109” \u7684\u957f\u5ea6\u3002
\u5982\u679c\u4e0d\u542b\u6709 “\u5c71\u8109” \u5219\u8fd4\u56de 0\u3002
\n\n
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1a[2,1,4,7,3,2,5]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u6700\u957f\u7684 “\u5c71\u8109” \u662f [1,4,7,3,2]\uff0c\u957f\u5ea6\u4e3a 5\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1a[2,2,2]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u4e0d\u542b “\u5c71\u8109”\u3002\n\n\n
\n\n
\u63d0\u793a\uff1a
\n\n0 <= A.length <= 100000 <= A[i] <= 10000\u6211\u4eec\u628a\u6570\u7ec4 A \u4e2d\u7b26\u5408\u4e0b\u5217\u5c5e\u6027\u7684\u4efb\u610f\u8fde\u7eed\u5b50\u6570\u7ec4 B \u79f0\u4e3a “\u5c71\u8109”\uff1a
\n\nB.length >= 30 < i < B.length - 1 \u4f7f\u5f97 B[0] < B[1] < ... B[i-1] < B[i] > B[i+1] > ... > B[B.length - 1]\uff08\u6ce8\u610f\uff1aB \u53ef\u4ee5\u662f A \u7684\u4efb\u610f\u5b50\u6570\u7ec4\uff0c\u5305\u62ec\u6574\u4e2a\u6570\u7ec4 A\u3002\uff09
\n\n\u7ed9\u51fa\u4e00\u4e2a\u6574\u6570\u6570\u7ec4 A\uff0c\u8fd4\u56de\u6700\u957f “\u5c71\u8109” \u7684\u957f\u5ea6\u3002
\u5982\u679c\u4e0d\u542b\u6709 “\u5c71\u8109” \u5219\u8fd4\u56de 0\u3002
\n\n
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1a[2,1,4,7,3,2,5]\n\u8f93\u51fa\uff1a5\n\u89e3\u91ca\uff1a\u6700\u957f\u7684 “\u5c71\u8109” \u662f [1,4,7,3,2]\uff0c\u957f\u5ea6\u4e3a 5\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1a[2,2,2]\n\u8f93\u51fa\uff1a0\n\u89e3\u91ca\uff1a\u4e0d\u542b “\u5c71\u8109”\u3002\n\n\n
\n\n
\u63d0\u793a\uff1a
\n\n0 <= A.length <= 100000 <= A[i] <= 10000Given an array of integers nums, write a method that returns the \"pivot\" index of this array.\n
\nWe define the pivot index as the index where the sum of the numbers to the left of the index is equal to the sum of the numbers to the right of the index.\n
\nIf no such index exists, we should return -1. If there are multiple pivot indexes, you should return the left-most pivot index.\n
\n\nExample 1:
\n
\nInput: \nnums = [1, 7, 3, 6, 5, 6]\nOutput: 3\nExplanation: \nThe sum of the numbers to the left of index 3 (nums[3] = 6) is equal to the sum of numbers to the right of index 3.\nAlso, 3 is the first index where this occurs.\n\n\n\n
Example 2:
\n
\nInput: \nnums = [1, 2, 3]\nOutput: -1\nExplanation: \nThere is no index that satisfies the conditions in the problem statement.\n\n\n\n
Note:\n
nums will be in the range [0, 10000].nums[i] will be an integer in the range [-1000, 1000].\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u7684\u6570\u7ec4 nums\uff0c\u8bf7\u7f16\u5199\u4e00\u4e2a\u80fd\u591f\u8fd4\u56de\u6570\u7ec4“\u4e2d\u5fc3\u7d22\u5f15”\u7684\u65b9\u6cd5\u3002
\u6211\u4eec\u662f\u8fd9\u6837\u5b9a\u4e49\u6570\u7ec4\u4e2d\u5fc3\u7d22\u5f15\u7684\uff1a\u6570\u7ec4\u4e2d\u5fc3\u7d22\u5f15\u7684\u5de6\u4fa7\u6240\u6709\u5143\u7d20\u76f8\u52a0\u7684\u548c\u7b49\u4e8e\u53f3\u4fa7\u6240\u6709\u5143\u7d20\u76f8\u52a0\u7684\u548c\u3002
\n\n\u5982\u679c\u6570\u7ec4\u4e0d\u5b58\u5728\u4e2d\u5fc3\u7d22\u5f15\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u8fd4\u56de -1\u3002\u5982\u679c\u6570\u7ec4\u6709\u591a\u4e2a\u4e2d\u5fc3\u7d22\u5f15\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u8fd4\u56de\u6700\u9760\u8fd1\u5de6\u8fb9\u7684\u90a3\u4e00\u4e2a\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: \nnums = [1, 7, 3, 6, 5, 6]\n\u8f93\u51fa: 3\n\u89e3\u91ca: \n\u7d22\u5f153 (nums[3] = 6) \u7684\u5de6\u4fa7\u6570\u4e4b\u548c(1 + 7 + 3 = 11)\uff0c\u4e0e\u53f3\u4fa7\u6570\u4e4b\u548c(5 + 6 = 11)\u76f8\u7b49\u3002\n\u540c\u65f6, 3 \u4e5f\u662f\u7b2c\u4e00\u4e2a\u7b26\u5408\u8981\u6c42\u7684\u4e2d\u5fc3\u7d22\u5f15\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: \nnums = [1, 2, 3]\n\u8f93\u51fa: -1\n\u89e3\u91ca: \n\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u6ee1\u8db3\u6b64\u6761\u4ef6\u7684\u4e2d\u5fc3\u7d22\u5f15\u3002\n\n
\u8bf4\u660e:
\n\nnums \u7684\u957f\u5ea6\u8303\u56f4\u4e3a [0, 10000]\u3002nums[i] \u5c06\u4f1a\u662f\u4e00\u4e2a\u8303\u56f4\u5728 [-1000, 1000]\u7684\u6574\u6570\u3002\u7ed9\u5b9a\u4e00\u4e2a\u6574\u6570\u7c7b\u578b\u7684\u6570\u7ec4 nums\uff0c\u8bf7\u7f16\u5199\u4e00\u4e2a\u80fd\u591f\u8fd4\u56de\u6570\u7ec4“\u4e2d\u5fc3\u7d22\u5f15”\u7684\u65b9\u6cd5\u3002
\u6211\u4eec\u662f\u8fd9\u6837\u5b9a\u4e49\u6570\u7ec4\u4e2d\u5fc3\u7d22\u5f15\u7684\uff1a\u6570\u7ec4\u4e2d\u5fc3\u7d22\u5f15\u7684\u5de6\u4fa7\u6240\u6709\u5143\u7d20\u76f8\u52a0\u7684\u548c\u7b49\u4e8e\u53f3\u4fa7\u6240\u6709\u5143\u7d20\u76f8\u52a0\u7684\u548c\u3002
\n\n\u5982\u679c\u6570\u7ec4\u4e0d\u5b58\u5728\u4e2d\u5fc3\u7d22\u5f15\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u8fd4\u56de -1\u3002\u5982\u679c\u6570\u7ec4\u6709\u591a\u4e2a\u4e2d\u5fc3\u7d22\u5f15\uff0c\u90a3\u4e48\u6211\u4eec\u5e94\u8be5\u8fd4\u56de\u6700\u9760\u8fd1\u5de6\u8fb9\u7684\u90a3\u4e00\u4e2a\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: \nnums = [1, 7, 3, 6, 5, 6]\n\u8f93\u51fa: 3\n\u89e3\u91ca: \n\u7d22\u5f153 (nums[3] = 6) \u7684\u5de6\u4fa7\u6570\u4e4b\u548c(1 + 7 + 3 = 11)\uff0c\u4e0e\u53f3\u4fa7\u6570\u4e4b\u548c(5 + 6 = 11)\u76f8\u7b49\u3002\n\u540c\u65f6, 3 \u4e5f\u662f\u7b2c\u4e00\u4e2a\u7b26\u5408\u8981\u6c42\u7684\u4e2d\u5fc3\u7d22\u5f15\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: \nnums = [1, 2, 3]\n\u8f93\u51fa: -1\n\u89e3\u91ca: \n\u6570\u7ec4\u4e2d\u4e0d\u5b58\u5728\u6ee1\u8db3\u6b64\u6761\u4ef6\u7684\u4e2d\u5fc3\u7d22\u5f15\u3002\n\n
\u8bf4\u660e:
\n\nnums \u7684\u957f\u5ea6\u8303\u56f4\u4e3a [0, 10000]\u3002nums[i] \u5c06\u4f1a\u662f\u4e00\u4e2a\u8303\u56f4\u5728 [-1000, 1000]\u7684\u6574\u6570\u3002We are playing the Guess Game. The game is as follows:
\n\n
I pick a number from 1 to n. You have to guess which number I picked.
\n\nEvery time you guess wrong, I'll tell you whether the number I picked is higher or lower.
\n\nHowever, when you guess a particular number x, and you guess wrong, you pay $x. You win the game when you guess the number I picked.
\n\n\nExample:\n
\nn = 10, I pick 8.\n\nFirst round: You guess 5, I tell you that it's higher. You pay $5.\nSecond round: You guess 7, I tell you that it's higher. You pay $7.\nThird round: You guess 9, I tell you that it's lower. You pay $9.\n\nGame over. 8 is the number I picked.\n\nYou end up paying $5 + $7 + $9 = $21.\n\n\n\n
Given a particular n ≥ 1, find out how much money you need to have to guarantee a win.
\n\nCredits:
Special thanks to @agave and @StefanPochmann for adding this problem and creating all test cases.
\u6211\u4eec\u6b63\u5728\u73a9\u4e00\u4e2a\u731c\u6570\u6e38\u620f\uff0c\u6e38\u620f\u89c4\u5219\u5982\u4e0b\uff1a
\n\n\u6211\u4ece 1 \u5230 n \u4e4b\u95f4\u9009\u62e9\u4e00\u4e2a\u6570\u5b57\uff0c\u4f60\u6765\u731c\u6211\u9009\u4e86\u54ea\u4e2a\u6570\u5b57\u3002
\n\n\u6bcf\u6b21\u4f60\u731c\u9519\u4e86\uff0c\u6211\u90fd\u4f1a\u544a\u8bc9\u4f60\uff0c\u6211\u9009\u7684\u6570\u5b57\u6bd4\u4f60\u7684\u5927\u4e86\u6216\u8005\u5c0f\u4e86\u3002
\n\n\u7136\u800c\uff0c\u5f53\u4f60\u731c\u4e86\u6570\u5b57 x \u5e76\u4e14\u731c\u9519\u4e86\u7684\u65f6\u5019\uff0c\u4f60\u9700\u8981\u652f\u4ed8\u91d1\u989d\u4e3a x \u7684\u73b0\u91d1\u3002\u76f4\u5230\u4f60\u731c\u5230\u6211\u9009\u7684\u6570\u5b57\uff0c\u4f60\u624d\u7b97\u8d62\u5f97\u4e86\u8fd9\u4e2a\u6e38\u620f\u3002
\n\n\u793a\u4f8b:
\n\n\nn = 10, \u6211\u9009\u62e9\u4e868.\n\n\u7b2c\u4e00\u8f6e: \u4f60\u731c\u6211\u9009\u62e9\u7684\u6570\u5b57\u662f5\uff0c\u6211\u4f1a\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5927\u4e00\u4e9b\uff0c\u7136\u540e\u4f60\u9700\u8981\u652f\u4ed85\u5757\u3002\n\u7b2c\u4e8c\u8f6e: \u4f60\u731c\u662f7\uff0c\u6211\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5927\u4e00\u4e9b\uff0c\u4f60\u652f\u4ed87\u5757\u3002\n\u7b2c\u4e09\u8f6e: \u4f60\u731c\u662f9\uff0c\u6211\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5c0f\u4e00\u4e9b\uff0c\u4f60\u652f\u4ed89\u5757\u3002\n\n\u6e38\u620f\u7ed3\u675f\u30028 \u5c31\u662f\u6211\u9009\u7684\u6570\u5b57\u3002\n\n\u4f60\u6700\u7ec8\u8981\u652f\u4ed8 5 + 7 + 9 = 21 \u5757\u94b1\u3002\n\n\n
\u7ed9\u5b9a\u4e00\u4e2a n ≥ 1\uff0c\u8ba1\u7b97\u4f60\u81f3\u5c11\u9700\u8981\u62e5\u6709\u591a\u5c11\u73b0\u91d1\u624d\u80fd\u786e\u4fdd\u4f60\u80fd\u8d62\u5f97\u8fd9\u4e2a\u6e38\u620f\u3002
\n\n\u81f4\u8c22:
\n\n\u7279\u522b\u611f\u8c22 @agave \u548c @StefanPochmann \u6dfb\u52a0\u4e86\u8fd9\u9053\u9898\u76ee\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\u3002
\n\u6211\u4eec\u6b63\u5728\u73a9\u4e00\u4e2a\u731c\u6570\u6e38\u620f\uff0c\u6e38\u620f\u89c4\u5219\u5982\u4e0b\uff1a
\n\n\u6211\u4ece 1 \u5230 n \u4e4b\u95f4\u9009\u62e9\u4e00\u4e2a\u6570\u5b57\uff0c\u4f60\u6765\u731c\u6211\u9009\u4e86\u54ea\u4e2a\u6570\u5b57\u3002
\n\n\u6bcf\u6b21\u4f60\u731c\u9519\u4e86\uff0c\u6211\u90fd\u4f1a\u544a\u8bc9\u4f60\uff0c\u6211\u9009\u7684\u6570\u5b57\u6bd4\u4f60\u7684\u5927\u4e86\u6216\u8005\u5c0f\u4e86\u3002
\n\n\u7136\u800c\uff0c\u5f53\u4f60\u731c\u4e86\u6570\u5b57 x \u5e76\u4e14\u731c\u9519\u4e86\u7684\u65f6\u5019\uff0c\u4f60\u9700\u8981\u652f\u4ed8\u91d1\u989d\u4e3a x \u7684\u73b0\u91d1\u3002\u76f4\u5230\u4f60\u731c\u5230\u6211\u9009\u7684\u6570\u5b57\uff0c\u4f60\u624d\u7b97\u8d62\u5f97\u4e86\u8fd9\u4e2a\u6e38\u620f\u3002
\n\n\u793a\u4f8b:
\n\n\nn = 10, \u6211\u9009\u62e9\u4e868.\n\n\u7b2c\u4e00\u8f6e: \u4f60\u731c\u6211\u9009\u62e9\u7684\u6570\u5b57\u662f5\uff0c\u6211\u4f1a\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5927\u4e00\u4e9b\uff0c\u7136\u540e\u4f60\u9700\u8981\u652f\u4ed85\u5757\u3002\n\u7b2c\u4e8c\u8f6e: \u4f60\u731c\u662f7\uff0c\u6211\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5927\u4e00\u4e9b\uff0c\u4f60\u652f\u4ed87\u5757\u3002\n\u7b2c\u4e09\u8f6e: \u4f60\u731c\u662f9\uff0c\u6211\u544a\u8bc9\u4f60\uff0c\u6211\u7684\u6570\u5b57\u66f4\u5c0f\u4e00\u4e9b\uff0c\u4f60\u652f\u4ed89\u5757\u3002\n\n\u6e38\u620f\u7ed3\u675f\u30028 \u5c31\u662f\u6211\u9009\u7684\u6570\u5b57\u3002\n\n\u4f60\u6700\u7ec8\u8981\u652f\u4ed8 5 + 7 + 9 = 21 \u5757\u94b1\u3002\n\n\n
\u7ed9\u5b9a\u4e00\u4e2a n ≥ 1\uff0c\u8ba1\u7b97\u4f60\u81f3\u5c11\u9700\u8981\u62e5\u6709\u591a\u5c11\u73b0\u91d1\u624d\u80fd\u786e\u4fdd\u4f60\u80fd\u8d62\u5f97\u8fd9\u4e2a\u6e38\u620f\u3002
\n\n\u81f4\u8c22:
\n\n\u7279\u522b\u611f\u8c22 @agave \u548c @StefanPochmann \u6dfb\u52a0\u4e86\u8fd9\u9053\u9898\u76ee\uff0c\u5e76\u4e14\u63d0\u4f9b\u4e86\u6240\u6709\u6d4b\u8bd5\u7528\u4f8b\u3002
\n*/\n\n\n/**\n * @param {number} n\n * @return {number}\n */\nvar getMoneyAmount = function(n) {\n \n};"} {"doc_id": "39936ff2b5270fa7298dad87fb91cbd9", "text": "\"use strict\";\n/*\n * @Description: \u53cd\u8f6c\u5b57\u7b26\u4e32\n * @Author: Moriaty\n * @Date: 2020-09-01 20:34:56\n * @Last modified by: Moriaty\n * @LastEditTime: 2020-09-01 20:41:34\n */\n/**\n * \u8f93\u5165\uff1a[\"h\",\"e\",\"l\",\"l\",\"o\"]\n * \u8f93\u51fa\uff1a[\"o\",\"l\",\"l\",\"e\",\"h\"]\n *\n * \u6765\u6e90\uff1a\u529b\u6263\uff08LeetCode\uff09\n * \u94fe\u63a5\uff1ahttps://leetcode-cn.com/problems/reverse-string\n */\n/**\n * @description: \u7f16\u5199\u4e00\u4e2a\u51fd\u6570\uff0c\u5176\u4f5c\u7528\u662f\u5c06\u8f93\u5165\u7684\u5b57\u7b26\u4e32\u53cd\u8f6c\u8fc7\u6765\u3002\u8f93\u5165\u5b57\u7b26\u4e32\u4ee5\u5b57\u7b26\u6570\u7ec4 char[] \u7684\u5f62\u5f0f\u7ed9\u51fa\u3002\n * \u4e0d\u8981\u7ed9\u53e6\u5916\u7684\u6570\u7ec4\u5206\u914d\u989d\u5916\u7684\u7a7a\u95f4\uff0c\u4f60\u5fc5\u987b\u539f\u5730\u4fee\u6539\u8f93\u5165\u6570\u7ec4\u3001\u4f7f\u7528 O(1) \u7684\u989d\u5916\u7a7a\u95f4\u89e3\u51b3\u8fd9\u4e00\u95ee\u9898\u3002\n * \u4f60\u53ef\u4ee5\u5047\u8bbe\u6570\u7ec4\u4e2d\u7684\u6240\u6709\u5b57\u7b26\u90fd\u662f ASCII \u7801\u8868\u4e2d\u7684\u53ef\u6253\u5370\u5b57\u7b26\n * @param {string[]} s \u5b57\u7b26\u6570\u7ec4\n */\nfunction reverseString(s) {\n // \u5de6\u53f3\u58f0\u660e\u4e24\u6307\u9488\n let left = 0, right = s.length - 1;\n while (left < right) {\n [s[left], s[right]] = [s[right], s[left]];\n ++left;\n --right;\n }\n}\n;\nconsole.log(reverseString([\"h\", \"e\", \"l\", \"l\", \"o\"])); // [\"o\",\"l\",\"l\",\"e\",\"h\"]\n"} {"doc_id": "39a79d650e3f06cb5f2690ee36c6f5f7", "text": "/**\n * Declare a class BuildComputer \n * along with all its methods to help user \n * add different components and attributes \n * to the computer they are building\n */\n\nclass BuildComputer{\n\tconstructor(){\n\t\tthis.computer = {};\n\t}\n\n\t//add a name for the computer\n\taddName(name){ \n\t\tthis.computer.name = name;\n\t}\n\n\t//add a brand name\n\taddBrand(brand){ \n\t\tthis.computer.brand = brand;\n\t}\n\n\t// add memory capacity\n\taddMemory(capacity){ \n\t\tthis.computer.capacity = `${capacity} GB DDR4 RAM`;\n\t}\n\n\t// add display size \n\taddDisplay(length){ \n\t\tthis.computer.length = `${length}-inch`;\n\t}\n\n\t// add storage capacity\n\taddStorage(storageCapacity){ \n\t\tthis.computer.storageCapacity = `${storageCapacity} GB SSD`;\n\t}\n\n\t//add an address for shipping\n\taddShippingAddress(address){ \n\t\tthis.computer.shippingAddress = address;\n\t}\n\n\t// return the computer object for shipping\n\tshipComputer(){ \n\t\treturn this;\n\t}\n}\n\n\n//instantiate build class\nlet build = new BuildComputer(); \n\n//using build class we can add components to our computer\nbuild.addName('MacBook Pro');\nbuild.addBrand('Apple');\nbuild.addMemory(16);\nbuild.addDisplay(13.3);\nbuild.addStorage(250);\nbuild.addShippingAddress('Iris Watson P.O. Box 283 8562 Fusce Rd.');\n\n\nconsole.log(build.shipComputer());\n// log object"} {"doc_id": "39e3daa939f43dba5d1438faee3132f2", "text": "/*\n ================\n Maze Runner\n ================\n\n [INSTRUCTIONS]\n Maze Runner adalah suatu function yang dibuat untuk mengetahui, siapa yang\n akan memenangkan perlombaan maze runner.\n\n [EXAMPLE]\n var persons = ['Andi', 'Betty', 'Ryan', 'Danang'];\n var tracks = ['AABBAABB', 'ABAB', 'BBBBBBAA', 'AAAB'];\n pemenangnya apabila jumlah A dan B atau sebaliknya berupa perbandingan 1 : 3\n Ryan => 2 A dan 6 B => 1 : 3\n Danang => 3 A dan 1 B => 3 : 1\n output yang diharapkan adalah ['Ryan', 'Danang'];\n\n [RULE]\n - Tidak boleh menggunakan built in function selain .push() dan .unshift()\n*/\n\nfunction mazeRunner(people, solves) {\n var winners = [];\n for (var i = 0; i < solves.length; i++) {\n var checked = checkAB(solves[i]);\n if (checked) {\n winners.push(people[i]);\n }\n }\n if (winners.length === 0) {\n return \"Tidak ada pemenangnya\";\n } else {\n return winners;\n }\n};\n\nfunction checkAB (string) {\n var countA = 0;\n var countB = 0;\n for (var i = 0; i < string.length; i++) {\n if (string[i] === \"A\") {\n countA++;\n } else if (string[i] === \"B\") {\n countB++;\n }\n }\n if ( (countA / countB === 3) || (countB / countA) === 3 ) {\n return true;\n } else {\n return false;\n }\n}\n\n// TEST CASES\nvar persons = ['Andi', 'Betty', 'Ryan', 'Danang'];\nvar tracks = ['AABBAABB', 'ABAB', 'BBBBBBAA', 'AAAB'];\nconsole.log(mazeRunner(persons, tracks));\n// [ 'Ryan', 'Danang' ]\nvar persons2 = ['Andi', 'Betty'];\nvar tracks2 = ['AAB', ''];\nconsole.log(mazeRunner(persons2, tracks2));\n// Tidak ada pemenangnya\nvar persons3 = ['Andi', 'Betty'];\nvar tracks3 = ['AAAB', ''];\nconsole.log(mazeRunner(persons3, tracks3));\n// [ 'Andi' ]"} {"doc_id": "3a41e31fc4683a8f185ff7dd4823f807", "text": "/*\nA password is considered strong if below conditions are all met:
\n\nWrite a function strongPasswordChecker(s), that takes a string s as input, and return the MINIMUM change required to make s a strong password. If s is already strong, return 0.
\n\nInsertion, deletion or replace of any one character are all considered as one change.
\u4e00\u4e2a\u5f3a\u5bc6\u7801\u5e94\u6ee1\u8db3\u4ee5\u4e0b\u6240\u6709\u6761\u4ef6\uff1a
\n\n\u7f16\u5199\u51fd\u6570 strongPasswordChecker(s)\uff0cs \u4ee3\u8868\u8f93\u5165\u5b57\u7b26\u4e32\uff0c\u5982\u679c s \u5df2\u7ecf\u7b26\u5408\u5f3a\u5bc6\u7801\u6761\u4ef6\uff0c\u5219\u8fd4\u56de0\uff1b\u5426\u5219\u8fd4\u56de\u8981\u5c06 s \u4fee\u6539\u4e3a\u6ee1\u8db3\u5f3a\u5bc6\u7801\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u6240\u9700\u8981\u8fdb\u884c\u4fee\u6539\u7684\u6700\u5c0f\u6b65\u6570\u3002
\n\n\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u4efb\u4e00\u5b57\u7b26\u90fd\u7b97\u4f5c\u4e00\u6b21\u4fee\u6539\u3002
\n\u4e00\u4e2a\u5f3a\u5bc6\u7801\u5e94\u6ee1\u8db3\u4ee5\u4e0b\u6240\u6709\u6761\u4ef6\uff1a
\n\n\u7f16\u5199\u51fd\u6570 strongPasswordChecker(s)\uff0cs \u4ee3\u8868\u8f93\u5165\u5b57\u7b26\u4e32\uff0c\u5982\u679c s \u5df2\u7ecf\u7b26\u5408\u5f3a\u5bc6\u7801\u6761\u4ef6\uff0c\u5219\u8fd4\u56de0\uff1b\u5426\u5219\u8fd4\u56de\u8981\u5c06 s \u4fee\u6539\u4e3a\u6ee1\u8db3\u5f3a\u5bc6\u7801\u6761\u4ef6\u7684\u5b57\u7b26\u4e32\u6240\u9700\u8981\u8fdb\u884c\u4fee\u6539\u7684\u6700\u5c0f\u6b65\u6570\u3002
\n\n\u63d2\u5165\u3001\u5220\u9664\u3001\u66ff\u6362\u4efb\u4e00\u5b57\u7b26\u90fd\u7b97\u4f5c\u4e00\u6b21\u4fee\u6539\u3002
\n*/\n\n\n/**\n * @param {string} s\n * @return {number}\n */\nvar strongPasswordChecker = function(s) {\n \n};"} {"doc_id": "3ba2b0aef86096b75bf7fb25b487c46b", "text": "/*\n\nYou are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k. \n
\n\nDefine a pair (u,v) which consists of one element from the first array and one element from the second array.
\n\nFind the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.\n
\n\nExample 1:
\n
\nGiven nums1 = [1,7,11], nums2 = [2,4,6], k = 3\n\nReturn: [1,2],[1,4],[1,6]\n\nThe first 3 pairs are returned from the sequence:\n[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]\n\n\n\n
Example 2:
\n
\nGiven nums1 = [1,1,2], nums2 = [1,2,3], k = 2\n\nReturn: [1,1],[1,1]\n\nThe first 2 pairs are returned from the sequence:\n[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]\n\n\n\n
Example 3:
\n
\nGiven nums1 = [1,2], nums2 = [3], k = 3 \n\nReturn: [1,3],[2,3]\n\nAll possible pairs are returned from the sequence:\n[1,3],[2,3]\n\n\n\n
Credits:
Special thanks to @elmirap and @StefanPochmann for adding this problem and creating all test cases.
\u7ed9\u5b9a\u4e24\u4e2a\u4ee5\u5347\u5e8f\u6392\u5217\u7684\u6574\u5f62\u6570\u7ec4 nums1 \u548c nums2, \u4ee5\u53ca\u4e00\u4e2a\u6574\u6570 k\u3002
\n\n\u5b9a\u4e49\u4e00\u5bf9\u503c (u,v)\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5143\u7d20\u6765\u81ea nums1\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6765\u81ea nums2\u3002
\n\n\u627e\u5230\u548c\u6700\u5c0f\u7684 k \u5bf9\u6570\u5b57 (u1,v1), (u2,v2) ... (uk,vk)\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u7ed9\u51fa\uff1a nums1 = [1,7,11], nums2 = [2,4,6], k = 3\n\n\u8fd4\u56de\uff1a [1,2],[1,4],[1,6]\n\n\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u524d 3 \u5bf9\u6570\uff1a\n[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]\n\n\n
\u793a\u4f8b 2:
\n\n\n\u7ed9\u51fa\uff1anums1 = [1,1,2], nums2 = [1,2,3], k = 2\n\n\u8fd4\u56de\uff1a [1,1],[1,1]\n\n\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u524d 2 \u5bf9\u6570\uff1a\n[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]\n\n\n
\u793a\u4f8b 3:
\n\n\n\u7ed9\u51fa\uff1anums1 = [1,2], nums2 = [3], k = 3 \n\n\u8fd4\u56de\uff1a [1,3],[2,3]\n\n\u4e5f\u53ef\u80fd\u5e8f\u5217\u4e2d\u6240\u6709\u7684\u6570\u5bf9\u90fd\u88ab\u8fd4\u56de:\n[1,3],[2,3]\n\n\n
\u81f4\u8c22:
\n\u7279\u522b\u611f\u8c22 @elmirap \u548c @StefanPochmann \u6dfb\u52a0\u8fd9\u4e2a\u95ee\u9898\u5e76\u521b\u5efa\u6240\u6709\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002
\u7ed9\u5b9a\u4e24\u4e2a\u4ee5\u5347\u5e8f\u6392\u5217\u7684\u6574\u5f62\u6570\u7ec4 nums1 \u548c nums2, \u4ee5\u53ca\u4e00\u4e2a\u6574\u6570 k\u3002
\n\n\u5b9a\u4e49\u4e00\u5bf9\u503c (u,v)\uff0c\u5176\u4e2d\u7b2c\u4e00\u4e2a\u5143\u7d20\u6765\u81ea nums1\uff0c\u7b2c\u4e8c\u4e2a\u5143\u7d20\u6765\u81ea nums2\u3002
\n\n\u627e\u5230\u548c\u6700\u5c0f\u7684 k \u5bf9\u6570\u5b57 (u1,v1), (u2,v2) ... (uk,vk)\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u7ed9\u51fa\uff1a nums1 = [1,7,11], nums2 = [2,4,6], k = 3\n\n\u8fd4\u56de\uff1a [1,2],[1,4],[1,6]\n\n\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u524d 3 \u5bf9\u6570\uff1a\n[1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]\n\n\n
\u793a\u4f8b 2:
\n\n\n\u7ed9\u51fa\uff1anums1 = [1,1,2], nums2 = [1,2,3], k = 2\n\n\u8fd4\u56de\uff1a [1,1],[1,1]\n\n\u8fd4\u56de\u5e8f\u5217\u4e2d\u7684\u524d 2 \u5bf9\u6570\uff1a\n[1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]\n\n\n
\u793a\u4f8b 3:
\n\n\n\u7ed9\u51fa\uff1anums1 = [1,2], nums2 = [3], k = 3 \n\n\u8fd4\u56de\uff1a [1,3],[2,3]\n\n\u4e5f\u53ef\u80fd\u5e8f\u5217\u4e2d\u6240\u6709\u7684\u6570\u5bf9\u90fd\u88ab\u8fd4\u56de:\n[1,3],[2,3]\n\n\n
\u81f4\u8c22:
\n\u7279\u522b\u611f\u8c22 @elmirap \u548c @StefanPochmann \u6dfb\u52a0\u8fd9\u4e2a\u95ee\u9898\u5e76\u521b\u5efa\u6240\u6709\u7684\u6d4b\u8bd5\u7528\u4f8b\u3002
Koko loves to eat bananas. There are N piles of bananas, the i-th pile has piles[i] bananas. The guards have gone and will come back in H hours.
Koko can decide her bananas-per-hour eating speed of K. Each hour, she chooses some pile of bananas, and eats K bananas from that pile. If the pile has less than K bananas, she eats all of them instead, and won't eat any more bananas during this hour.
Koko likes to eat slowly, but still wants to finish eating all the bananas before the guards come back.
\n\nReturn the minimum integer K such that she can eat all the bananas within H hours.
\n\n
Example 1:
\n\n\nInput: piles = [3,6,7,11], H = 8\nOutput: 4\n\n\n
Example 2:
\n\n\nInput: piles = [30,11,23,4,20], H = 5\nOutput: 30\n\n\n
Example 3:
\n\n\nInput: piles = [30,11,23,4,20], H = 6\nOutput: 23\n\n\n
\n\n
Note:
\n\n1 <= piles.length <= 10^4piles.length <= H <= 10^91 <= piles[i] <= 10^9\u73c2\u73c2\u559c\u6b22\u5403\u9999\u8549\u3002\u8fd9\u91cc\u6709 N \u5806\u9999\u8549\uff0c\u7b2c i \u5806\u4e2d\u6709 piles[i] \u6839\u9999\u8549\u3002\u8b66\u536b\u5df2\u7ecf\u79bb\u5f00\u4e86\uff0c\u5c06\u5728 H \u5c0f\u65f6\u540e\u56de\u6765\u3002
\u73c2\u73c2\u53ef\u4ee5\u51b3\u5b9a\u5979\u5403\u9999\u8549\u7684\u901f\u5ea6 K \uff08\u5355\u4f4d\uff1a\u6839/\u5c0f\u65f6\uff09\u3002\u6bcf\u4e2a\u5c0f\u65f6\uff0c\u5979\u5c06\u4f1a\u9009\u62e9\u4e00\u5806\u9999\u8549\uff0c\u4ece\u4e2d\u5403\u6389 K \u6839\u3002\u5982\u679c\u8fd9\u5806\u9999\u8549\u5c11\u4e8e K \u6839\uff0c\u5979\u5c06\u5403\u6389\u8fd9\u5806\u7684\u6240\u6709\u9999\u8549\uff0c\u7136\u540e\u8fd9\u4e00\u5c0f\u65f6\u5185\u4e0d\u4f1a\u518d\u5403\u66f4\u591a\u7684\u9999\u8549\u3002
\u73c2\u73c2\u559c\u6b22\u6162\u6162\u5403\uff0c\u4f46\u4ecd\u7136\u60f3\u5728\u8b66\u536b\u56de\u6765\u524d\u5403\u6389\u6240\u6709\u7684\u9999\u8549\u3002
\n\n\u8fd4\u56de\u5979\u53ef\u4ee5\u5728 H \u5c0f\u65f6\u5185\u5403\u6389\u6240\u6709\u9999\u8549\u7684\u6700\u5c0f\u901f\u5ea6 K\uff08K \u4e3a\u6574\u6570\uff09\u3002
\n\n
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165: piles = [3,6,7,11], H = 8\n\u8f93\u51fa: 4\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165: piles = [30,11,23,4,20], H = 5\n\u8f93\u51fa: 30\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165: piles = [30,11,23,4,20], H = 6\n\u8f93\u51fa: 23\n\n\n
\n\n
\u63d0\u793a\uff1a
\n\n1 <= piles.length <= 10^4piles.length <= H <= 10^91 <= piles[i] <= 10^9\u73c2\u73c2\u559c\u6b22\u5403\u9999\u8549\u3002\u8fd9\u91cc\u6709 N \u5806\u9999\u8549\uff0c\u7b2c i \u5806\u4e2d\u6709 piles[i] \u6839\u9999\u8549\u3002\u8b66\u536b\u5df2\u7ecf\u79bb\u5f00\u4e86\uff0c\u5c06\u5728 H \u5c0f\u65f6\u540e\u56de\u6765\u3002
\u73c2\u73c2\u53ef\u4ee5\u51b3\u5b9a\u5979\u5403\u9999\u8549\u7684\u901f\u5ea6 K \uff08\u5355\u4f4d\uff1a\u6839/\u5c0f\u65f6\uff09\u3002\u6bcf\u4e2a\u5c0f\u65f6\uff0c\u5979\u5c06\u4f1a\u9009\u62e9\u4e00\u5806\u9999\u8549\uff0c\u4ece\u4e2d\u5403\u6389 K \u6839\u3002\u5982\u679c\u8fd9\u5806\u9999\u8549\u5c11\u4e8e K \u6839\uff0c\u5979\u5c06\u5403\u6389\u8fd9\u5806\u7684\u6240\u6709\u9999\u8549\uff0c\u7136\u540e\u8fd9\u4e00\u5c0f\u65f6\u5185\u4e0d\u4f1a\u518d\u5403\u66f4\u591a\u7684\u9999\u8549\u3002
\u73c2\u73c2\u559c\u6b22\u6162\u6162\u5403\uff0c\u4f46\u4ecd\u7136\u60f3\u5728\u8b66\u536b\u56de\u6765\u524d\u5403\u6389\u6240\u6709\u7684\u9999\u8549\u3002
\n\n\u8fd4\u56de\u5979\u53ef\u4ee5\u5728 H \u5c0f\u65f6\u5185\u5403\u6389\u6240\u6709\u9999\u8549\u7684\u6700\u5c0f\u901f\u5ea6 K\uff08K \u4e3a\u6574\u6570\uff09\u3002
\n\n
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165: piles = [3,6,7,11], H = 8\n\u8f93\u51fa: 4\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165: piles = [30,11,23,4,20], H = 5\n\u8f93\u51fa: 30\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165: piles = [30,11,23,4,20], H = 6\n\u8f93\u51fa: 23\n\n\n
\n\n
\u63d0\u793a\uff1a
\n\n1 <= piles.length <= 10^4piles.length <= H <= 10^91 <= piles[i] <= 10^9Remember the story of Little Match Girl? By now, you know exactly what matchsticks the little match girl has, please find out a way you can make one square by using up all those matchsticks. You should not break any stick, but you can link them up, and each matchstick must be used exactly one time.
\n\nYour input will be several matchsticks the girl has, represented with their stick length. Your output will either be true or false, to represent whether you could make one square using all the matchsticks the little match girl has.
\n\nExample 1:
\n
\nInput: [1,1,2,2,2]\nOutput: true\n\nExplanation: You can form a square with length 2, one side of the square came two sticks with length 1.\n\n\n\n
Example 2:
\n
\nInput: [3,3,3,3,4]\nOutput: false\n\nExplanation: You cannot find a way to form a square with all the matchsticks.\n\n\n\n
Note:
\n
0 to 10^9.\n15.\u8fd8\u8bb0\u5f97\u7ae5\u8bdd\u300a\u5356\u706b\u67f4\u7684\u5c0f\u5973\u5b69\u300b\u5417\uff1f\u73b0\u5728\uff0c\u4f60\u77e5\u9053\u5c0f\u5973\u5b69\u6709\u591a\u5c11\u6839\u706b\u67f4\uff0c\u8bf7\u627e\u51fa\u4e00\u79cd\u80fd\u4f7f\u7528\u6240\u6709\u706b\u67f4\u62fc\u6210\u4e00\u4e2a\u6b63\u65b9\u5f62\u7684\u65b9\u6cd5\u3002\u4e0d\u80fd\u6298\u65ad\u706b\u67f4\uff0c\u53ef\u4ee5\u628a\u706b\u67f4\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u4e14\u6bcf\u6839\u706b\u67f4\u90fd\u8981\u7528\u5230\u3002
\n\n\u8f93\u5165\u4e3a\u5c0f\u5973\u5b69\u62e5\u6709\u706b\u67f4\u7684\u6570\u76ee\uff0c\u6bcf\u6839\u706b\u67f4\u7528\u5176\u957f\u5ea6\u8868\u793a\u3002\u8f93\u51fa\u5373\u4e3a\u662f\u5426\u80fd\u7528\u6240\u6709\u7684\u706b\u67f4\u62fc\u6210\u6b63\u65b9\u5f62\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: [1,1,2,2,2]\n\u8f93\u51fa: true\n\n\u89e3\u91ca: \u80fd\u62fc\u6210\u4e00\u4e2a\u8fb9\u957f\u4e3a2\u7684\u6b63\u65b9\u5f62\uff0c\u6bcf\u8fb9\u4e24\u6839\u706b\u67f4\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: [3,3,3,3,4]\n\u8f93\u51fa: false\n\n\u89e3\u91ca: \u4e0d\u80fd\u7528\u6240\u6709\u706b\u67f4\u62fc\u6210\u4e00\u4e2a\u6b63\u65b9\u5f62\u3002\n\n\n
\u6ce8\u610f:
\n\n0 \u5230 10^9\u4e4b\u95f4\u3002\u8fd8\u8bb0\u5f97\u7ae5\u8bdd\u300a\u5356\u706b\u67f4\u7684\u5c0f\u5973\u5b69\u300b\u5417\uff1f\u73b0\u5728\uff0c\u4f60\u77e5\u9053\u5c0f\u5973\u5b69\u6709\u591a\u5c11\u6839\u706b\u67f4\uff0c\u8bf7\u627e\u51fa\u4e00\u79cd\u80fd\u4f7f\u7528\u6240\u6709\u706b\u67f4\u62fc\u6210\u4e00\u4e2a\u6b63\u65b9\u5f62\u7684\u65b9\u6cd5\u3002\u4e0d\u80fd\u6298\u65ad\u706b\u67f4\uff0c\u53ef\u4ee5\u628a\u706b\u67f4\u8fde\u63a5\u8d77\u6765\uff0c\u5e76\u4e14\u6bcf\u6839\u706b\u67f4\u90fd\u8981\u7528\u5230\u3002
\n\n\u8f93\u5165\u4e3a\u5c0f\u5973\u5b69\u62e5\u6709\u706b\u67f4\u7684\u6570\u76ee\uff0c\u6bcf\u6839\u706b\u67f4\u7528\u5176\u957f\u5ea6\u8868\u793a\u3002\u8f93\u51fa\u5373\u4e3a\u662f\u5426\u80fd\u7528\u6240\u6709\u7684\u706b\u67f4\u62fc\u6210\u6b63\u65b9\u5f62\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: [1,1,2,2,2]\n\u8f93\u51fa: true\n\n\u89e3\u91ca: \u80fd\u62fc\u6210\u4e00\u4e2a\u8fb9\u957f\u4e3a2\u7684\u6b63\u65b9\u5f62\uff0c\u6bcf\u8fb9\u4e24\u6839\u706b\u67f4\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: [3,3,3,3,4]\n\u8f93\u51fa: false\n\n\u89e3\u91ca: \u4e0d\u80fd\u7528\u6240\u6709\u706b\u67f4\u62fc\u6210\u4e00\u4e2a\u6b63\u65b9\u5f62\u3002\n\n\n
\u6ce8\u610f:
\n\n0 \u5230 10^9\u4e4b\u95f4\u3002Given the root node of a binary search tree (BST) and a value to be inserted into the tree, insert the value into the BST. Return the root node of the BST after the insertion. It is guaranteed that the new value does not exist in the original BST.
\n\nNote that there may exist multiple valid ways for the insertion, as long as the tree remains a BST after insertion. You can return any of them.
\n\nFor example,
\n\n\nGiven the tree:\n 4\n / \\\n 2 7\n / \\\n 1 3\nAnd the value to insert: 5\n\n\n
You can return this binary search tree:
\n\n\n 4\n / \\\n 2 7\n / \\ /\n 1 3 5\n\n\n
This tree is also valid:
\n\n\n 5\n / \\\n 2 7\n / \\ \n 1 3\n \\\n 4\n
\u7ed9\u5b9a\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09\u7684\u6839\u8282\u70b9\u548c\u8981\u63d2\u5165\u6811\u4e2d\u7684\u503c\uff0c\u5c06\u503c\u63d2\u5165\u4e8c\u53c9\u641c\u7d22\u6811\u3002 \u8fd4\u56de\u63d2\u5165\u540e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6839\u8282\u70b9\u3002 \u4fdd\u8bc1\u539f\u59cb\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u5b58\u5728\u65b0\u503c\u3002
\n\n\u6ce8\u610f\uff0c\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u6709\u6548\u7684\u63d2\u5165\u65b9\u5f0f\uff0c\u53ea\u8981\u6811\u5728\u63d2\u5165\u540e\u4ecd\u4fdd\u6301\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811\u5373\u53ef\u3002 \u4f60\u53ef\u4ee5\u8fd4\u56de\u4efb\u610f\u6709\u6548\u7684\u7ed3\u679c\u3002
\n\n\u4f8b\u5982,
\n\n\n\u7ed9\u5b9a\u4e8c\u53c9\u641c\u7d22\u6811:\n\n 4\n / \\\n 2 7\n / \\\n 1 3\n\n\u548c \u63d2\u5165\u7684\u503c: 5\n\n\n
\u4f60\u53ef\u4ee5\u8fd4\u56de\u8fd9\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811:
\n\n\n 4\n / \\\n 2 7\n / \\ /\n 1 3 5\n\n\n
\u6216\u8005\u8fd9\u4e2a\u6811\u4e5f\u662f\u6709\u6548\u7684:
\n\n\n 5\n / \\\n 2 7\n / \\ \n 1 3\n \\\n 4\n\n
\u7ed9\u5b9a\u4e8c\u53c9\u641c\u7d22\u6811\uff08BST\uff09\u7684\u6839\u8282\u70b9\u548c\u8981\u63d2\u5165\u6811\u4e2d\u7684\u503c\uff0c\u5c06\u503c\u63d2\u5165\u4e8c\u53c9\u641c\u7d22\u6811\u3002 \u8fd4\u56de\u63d2\u5165\u540e\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6839\u8282\u70b9\u3002 \u4fdd\u8bc1\u539f\u59cb\u4e8c\u53c9\u641c\u7d22\u6811\u4e2d\u4e0d\u5b58\u5728\u65b0\u503c\u3002
\n\n\u6ce8\u610f\uff0c\u53ef\u80fd\u5b58\u5728\u591a\u79cd\u6709\u6548\u7684\u63d2\u5165\u65b9\u5f0f\uff0c\u53ea\u8981\u6811\u5728\u63d2\u5165\u540e\u4ecd\u4fdd\u6301\u4e3a\u4e8c\u53c9\u641c\u7d22\u6811\u5373\u53ef\u3002 \u4f60\u53ef\u4ee5\u8fd4\u56de\u4efb\u610f\u6709\u6548\u7684\u7ed3\u679c\u3002
\n\n\u4f8b\u5982,
\n\n\n\u7ed9\u5b9a\u4e8c\u53c9\u641c\u7d22\u6811:\n\n 4\n / \\\n 2 7\n / \\\n 1 3\n\n\u548c \u63d2\u5165\u7684\u503c: 5\n\n\n
\u4f60\u53ef\u4ee5\u8fd4\u56de\u8fd9\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811:
\n\n\n 4\n / \\\n 2 7\n / \\ /\n 1 3 5\n\n\n
\u6216\u8005\u8fd9\u4e2a\u6811\u4e5f\u662f\u6709\u6548\u7684:
\n\n\n 5\n / \\\n 2 7\n / \\ \n 1 3\n \\\n 4\n\n*/\n\n\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/**\n * @param {TreeNode} root\n * @param {number} val\n * @return {TreeNode}\n */\nvar insertIntoBST = function(root, val) {\n \n};"} {"doc_id": "492b12bd8b84adb57129557de9d3b9f1", "text": "export default ` Let's continue with the earlier example where we displayed company profile of \\`APPL\\`. Let's say I want to display the Company Profile for \\`FB\\` at some other place in the application. Since we have two variables \\`ticker\\` and \\`companyProfileInfo\\` hard-coded inside this component should I copy and paste the entire component to some other place and replace the \\`ticker\\` and \\`companyProfileInfo\\` to be that of \\`FB\\`?\n\n Well, no. Remember React is about building reusable Components. So we want to build a component called \\`CompanyProfile\\` that can be reused for any company ticker.\n\n Let's look at the \\`CompanyProfile\\` component again. Here we have two variables \\`ticker\\` and \\`companyProfileInfo\\`. So what if instead of hard coding the values of these two variables here inside the component, we could pass those values to this component instead?\n\nIf we were to pass these values to this component from outside, then this Component will not be tied to one Company ticker. We can pass in \\`XYZ\\` and it's profile info to this component and it should be able to render the profile for \\`XYZ\\` or any other company for that matter. This component becomes truly reusable. Wonderful, that's what we want. \n\n\\`\\`\\`jsx\nfunction CompanyProfile(props) {\n //Instead of storing these variables we want to pass\n //these values to this component from outside.\n const ticker = //pass the value for this variable from outside\n const companyProfileInfo = //pass the value for this variable from outside\n return (\n
This question is the same as "Max Chunks to Make Sorted" except the integers of the given array are not necessarily distinct, the input array could be up to length 2000, and the elements could be up to 10**8.
Given an array arr of integers (not necessarily distinct), we split the array into some number of "chunks" (partitions), and individually sort each chunk. After concatenating them, the result equals the sorted array.
What is the most number of chunks we could have made?
\n\nExample 1:
\n\n\nInput: arr = [5,4,3,2,1]\nOutput: 1\nExplanation:\nSplitting into two or more chunks will not return the required result.\nFor example, splitting into [5, 4], [3, 2, 1] will result in [4, 5, 1, 2, 3], which isn't sorted.\n\n\n
Example 2:
\n\n\nInput: arr = [2,1,3,4,4]\nOutput: 4\nExplanation:\nWe can split into two chunks, such as [2, 1], [3, 4, 4].\nHowever, splitting into [2, 1], [3], [4], [4] is the highest number of chunks possible.\n\n\n
Note:
\n\narr will have length in range [1, 2000].arr[i] will be an integer in range [0, 10**8].
\u8fd9\u4e2a\u95ee\u9898\u548c“\u6700\u591a\u80fd\u5b8c\u6210\u6392\u5e8f\u7684\u5757”\u76f8\u4f3c\uff0c\u4f46\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u91cd\u590d\uff0c\u8f93\u5165\u6570\u7ec4\u6700\u5927\u957f\u5ea6\u4e3a2000\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u6700\u5927\u4e3a10**8\u3002
arr\u662f\u4e00\u4e2a\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u6574\u6570\u6570\u7ec4\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u6570\u7ec4\u5206\u5272\u6210\u51e0\u4e2a“\u5757”\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5757\u5206\u522b\u8fdb\u884c\u6392\u5e8f\u3002\u4e4b\u540e\u518d\u8fde\u63a5\u8d77\u6765\uff0c\u4f7f\u5f97\u8fde\u63a5\u7684\u7ed3\u679c\u548c\u6309\u5347\u5e8f\u6392\u5e8f\u540e\u7684\u539f\u6570\u7ec4\u76f8\u540c\u3002
\u6211\u4eec\u6700\u591a\u80fd\u5c06\u6570\u7ec4\u5206\u6210\u591a\u5c11\u5757\uff1f
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: arr = [5,4,3,2,1]\n\u8f93\u51fa: 1\n\u89e3\u91ca:\n\u5c06\u6570\u7ec4\u5206\u62102\u5757\u6216\u8005\u66f4\u591a\u5757\uff0c\u90fd\u65e0\u6cd5\u5f97\u5230\u6240\u9700\u7684\u7ed3\u679c\u3002\n\u4f8b\u5982\uff0c\u5206\u6210 [5, 4], [3, 2, 1] \u7684\u7ed3\u679c\u662f [4, 5, 1, 2, 3]\uff0c\u8fd9\u4e0d\u662f\u6709\u5e8f\u7684\u6570\u7ec4\u3002 \n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: arr = [2,1,3,4,4]\n\u8f93\u51fa: 4\n\u89e3\u91ca:\n\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u5206\u6210\u4e24\u5757\uff0c\u4f8b\u5982 [2, 1], [3, 4, 4]\u3002\n\u7136\u800c\uff0c\u5206\u6210 [2, 1], [3], [4], [4] \u53ef\u4ee5\u5f97\u5230\u6700\u591a\u7684\u5757\u6570\u3002 \n\n\n
\u6ce8\u610f:
\n\narr\u7684\u957f\u5ea6\u5728[1, 2000]\u4e4b\u95f4\u3002arr[i]\u7684\u5927\u5c0f\u5728[0, 10**8]\u4e4b\u95f4\u3002\u8fd9\u4e2a\u95ee\u9898\u548c“\u6700\u591a\u80fd\u5b8c\u6210\u6392\u5e8f\u7684\u5757”\u76f8\u4f3c\uff0c\u4f46\u7ed9\u5b9a\u6570\u7ec4\u4e2d\u7684\u5143\u7d20\u53ef\u4ee5\u91cd\u590d\uff0c\u8f93\u5165\u6570\u7ec4\u6700\u5927\u957f\u5ea6\u4e3a2000\uff0c\u5176\u4e2d\u7684\u5143\u7d20\u6700\u5927\u4e3a10**8\u3002
arr\u662f\u4e00\u4e2a\u53ef\u80fd\u5305\u542b\u91cd\u590d\u5143\u7d20\u7684\u6574\u6570\u6570\u7ec4\uff0c\u6211\u4eec\u5c06\u8fd9\u4e2a\u6570\u7ec4\u5206\u5272\u6210\u51e0\u4e2a“\u5757”\uff0c\u5e76\u5c06\u8fd9\u4e9b\u5757\u5206\u522b\u8fdb\u884c\u6392\u5e8f\u3002\u4e4b\u540e\u518d\u8fde\u63a5\u8d77\u6765\uff0c\u4f7f\u5f97\u8fde\u63a5\u7684\u7ed3\u679c\u548c\u6309\u5347\u5e8f\u6392\u5e8f\u540e\u7684\u539f\u6570\u7ec4\u76f8\u540c\u3002
\u6211\u4eec\u6700\u591a\u80fd\u5c06\u6570\u7ec4\u5206\u6210\u591a\u5c11\u5757\uff1f
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: arr = [5,4,3,2,1]\n\u8f93\u51fa: 1\n\u89e3\u91ca:\n\u5c06\u6570\u7ec4\u5206\u62102\u5757\u6216\u8005\u66f4\u591a\u5757\uff0c\u90fd\u65e0\u6cd5\u5f97\u5230\u6240\u9700\u7684\u7ed3\u679c\u3002\n\u4f8b\u5982\uff0c\u5206\u6210 [5, 4], [3, 2, 1] \u7684\u7ed3\u679c\u662f [4, 5, 1, 2, 3]\uff0c\u8fd9\u4e0d\u662f\u6709\u5e8f\u7684\u6570\u7ec4\u3002 \n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165: arr = [2,1,3,4,4]\n\u8f93\u51fa: 4\n\u89e3\u91ca:\n\u6211\u4eec\u53ef\u4ee5\u628a\u5b83\u5206\u6210\u4e24\u5757\uff0c\u4f8b\u5982 [2, 1], [3, 4, 4]\u3002\n\u7136\u800c\uff0c\u5206\u6210 [2, 1], [3], [4], [4] \u53ef\u4ee5\u5f97\u5230\u6700\u591a\u7684\u5757\u6570\u3002 \n\n\n
\u6ce8\u610f:
\n\narr\u7684\u957f\u5ea6\u5728[1, 2000]\u4e4b\u95f4\u3002arr[i]\u7684\u5927\u5c0f\u5728[0, 10**8]\u4e4b\u95f4\u3002Compare two version numbers version1 and version2.
\nIf version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the . character.
\nThe . character does not represent a decimal point and is used to separate number sequences.
\nFor instance, 2.5 is not "two and a half" or "half way to version three", it is the fifth second-level revision of the second first-level revision.
Here is an example of version numbers ordering:
\n\n\n0.1 < 1.1 < 1.2 < 13.37\n\n
Credits:
\nSpecial thanks to @ts for adding this problem and creating all test cases.
\u6bd4\u8f83\u4e24\u4e2a\u7248\u672c\u53f7 version1 \u548c version2\u3002
\n\u5982\u679c version1 > version2 \u8fd4\u56de 1\uff0c\u5982\u679c version1 < version2 \u8fd4\u56de -1\uff0c \u9664\u6b64\u4e4b\u5916\u8fd4\u56de 0\u3002
\u4f60\u53ef\u4ee5\u5047\u8bbe\u7248\u672c\u5b57\u7b26\u4e32\u975e\u7a7a\uff0c\u5e76\u4e14\u53ea\u5305\u542b\u6570\u5b57\u548c . \u5b57\u7b26\u3002
. \u5b57\u7b26\u4e0d\u4ee3\u8868\u5c0f\u6570\u70b9\uff0c\u800c\u662f\u7528\u4e8e\u5206\u9694\u6570\u5b57\u5e8f\u5217\u3002
\u4f8b\u5982\uff0c2.5 \u4e0d\u662f“\u4e24\u4e2a\u534a”\uff0c\u4e5f\u4e0d\u662f“\u5dee\u4e00\u534a\u5230\u4e09”\uff0c\u800c\u662f\u7b2c\u4e8c\u7248\u4e2d\u7684\u7b2c\u4e94\u4e2a\u5c0f\u7248\u672c\u3002
\u793a\u4f8b 1:
\n\n\u8f93\u5165:\n\nversion1= "0.1",version2= "1.1"\n\u8f93\u51fa: -1
\u793a\u4f8b 2:
\n\n\u8f93\u5165:\n\nversion1= "1.0.1",version2= "1"\n\u8f93\u51fa: 1
\u793a\u4f8b 3:
\n\n\u8f93\u5165:\nversion1= "7.5.2.4",version2= "7.5.3"\n\u8f93\u51fa: -1
\u6bd4\u8f83\u4e24\u4e2a\u7248\u672c\u53f7 version1 \u548c version2\u3002
\n\u5982\u679c version1 > version2 \u8fd4\u56de 1\uff0c\u5982\u679c version1 < version2 \u8fd4\u56de -1\uff0c \u9664\u6b64\u4e4b\u5916\u8fd4\u56de 0\u3002
\u4f60\u53ef\u4ee5\u5047\u8bbe\u7248\u672c\u5b57\u7b26\u4e32\u975e\u7a7a\uff0c\u5e76\u4e14\u53ea\u5305\u542b\u6570\u5b57\u548c . \u5b57\u7b26\u3002
. \u5b57\u7b26\u4e0d\u4ee3\u8868\u5c0f\u6570\u70b9\uff0c\u800c\u662f\u7528\u4e8e\u5206\u9694\u6570\u5b57\u5e8f\u5217\u3002
\u4f8b\u5982\uff0c2.5 \u4e0d\u662f“\u4e24\u4e2a\u534a”\uff0c\u4e5f\u4e0d\u662f“\u5dee\u4e00\u534a\u5230\u4e09”\uff0c\u800c\u662f\u7b2c\u4e8c\u7248\u4e2d\u7684\u7b2c\u4e94\u4e2a\u5c0f\u7248\u672c\u3002
\u793a\u4f8b 1:
\n\n\u8f93\u5165:\n\nversion1= "0.1",version2= "1.1"\n\u8f93\u51fa: -1
\u793a\u4f8b 2:
\n\n\u8f93\u5165:\n\nversion1= "1.0.1",version2= "1"\n\u8f93\u51fa: 1
\u793a\u4f8b 3:
\n\n\u8f93\u5165:\n*/\n\n\n/**\n * @param {string} version1\n * @param {string} version2\n * @return {number}\n */\nvar compareVersion = function(version1, version2) {\n \n};"} {"doc_id": "4e09975fcbfdcae5759e4ede78889962", "text": "/*\n# Word Count\n\nWrite a program that given a phrase can count the occurrences of each word in that phrase.\n\nFor example for the input `\"olly olly in come free\"`\n\n```plain\nolly: 2\nin: 1\ncome: 1\nfree: 1\n```\n\n### Instructions\n1. Review the description, and create a count method that returns an object.\n2. Once you have a passing test suite, add your code to your daily-challenges repo\n3. Link us to your word-count.js file on Slack.\n\n*/\n\n// will return an object with format: {word : wordcount}\nfunction count(string) {\n const output = {};\n\t// var ray = string.split(/\\s+|\\n+/); // split based on whitespace (tabs, space, etc.)\n const ray = string.trim().split(/[\\s\\n\\t]+/); // split based on whitespace (tabs, space, etc.)\n\n\t// init every word as a key for the output\n for (let word of ray) {\n if (ray.length !== 0) {\n // get current word\n // word = ray[i];\n // lowercase just to keep the count case-insensitive\n word = word.toLowerCase();\n output[word] = 0; // init to 0 at first\n }\n }\n\n\t// increase count for each word\n for (let word of ray) {\n // word = ray[i]; // get current word;\n word = word.toLowerCase(); // lowercase just to keep the count case-insensitive\n\n console.log(`${output[word]}`);\n output[word]++; // otherwise just add one to the count\n console.log(word);\n }\n\n console.log('');\n console.log('Results: ');\n\n\t// output for sanity check and debugging\n for (const word of Object.keys(output)) {\n console.log(`${word}`);\n }\n\n console.log('--------: ');\n\t// console.log(`--------: `);\n return output;\n}\n\ncount('word');\n\n// / -- do not edit below ---\n\nxdescribe('count()', () => {\n it('counts one word', () => {\n const expectedCounts = { word: 1 };\n expect(count('word')).toEqual(expectedCounts);\n });\n\n it('counts one of each', () => {\n const expectedCounts = { one: 1, of: 1, each: 1 };\n expect(count('one of each')).toEqual(expectedCounts);\n });\n\n it('counts multiple occurrences', () => {\n const expectedCounts = { one: 1, fish: 4, two: 1, red: 1, blue: 1 };\n expect(count('one fish two fish red fish blue fish')).toEqual(expectedCounts);\n });\n\n it('includes punctuation', () => {\n const expectedCounts = { car: 1, ':': 2, carpet: 1, as: 1, java: 1, 'javascript!!&@$%^&': 1 };\n expect(count('car : carpet as java : javascript!!&@$%^&')).toEqual(expectedCounts);\n });\n\n it('includes numbers', () => {\n const expectedCounts = { testing: 2, 1: 1, 2: 1 };\n expect(count('testing 1 2 testing')).toEqual(expectedCounts);\n });\n\n it('normalizes to lowercase', () => {\n const expectedCounts = { go: 3 };\n expect(count('go Go GO')).toEqual(expectedCounts);\n });\n\n it('counts properly international characters', () => {\n const expectedCounts = { '\u00a1hola!': 1, '\u00bfqu\u00e9': 1, 'tal?': 1, '\u043f\u0440\u0438\u0432\u0435\u0442!': 1 };\n expect(count('\u00a1Hola! \u00bfQu\u00e9 tal? \u041f\u0440\u0438\u0432\u0435\u0442!')).toEqual(expectedCounts);\n });\n\n it('counts multiline', () => {\n const expectedCounts = { hello: 1, world: 1 };\n expect(count('hello\\nworld')).toEqual(expectedCounts);\n });\n\n it('counts tabs', () => {\n const expectedCounts = { hello: 1, world: 1 };\n expect(count('hello\\tworld')).toEqual(expectedCounts);\n });\n\n it('counts multiple spaces as one', () => {\n const expectedCounts = { hello: 1, world: 1 };\n expect(count('hello world')).toEqual(expectedCounts);\n });\n\n it('does not count leading or trailing whitespace', () => {\n const expectedCounts = { introductory: 1, course: 1 };\n expect(count('\\t\\tIntroductory Course ')).toEqual(expectedCounts);\n });\n\n it('handles properties that exist on Object\u2019s prototype', () => {\n const expectedCounts = {\n reserved: 1, words: 1, like: 1, constructor: 1, and: 1, tostring: 1, 'ok?': 1 };\n expect(count('reserved words like constructor and toString ok?')).toEqual(expectedCounts);\n });\n});\n"} {"doc_id": "4e6d7105a9c4b39940371f4764e16b86", "text": "/*\nversion1= "7.5.2.4",version2= "7.5.3"\n\u8f93\u51fa: -1
Given a Binary Search Tree (BST) with the root node root, return the minimum difference between the values of any two different nodes in the tree.
Example :
\n\n\nInput: root = [4,2,6,1,3,null,null]\nOutput: 1\nExplanation:\nNote that root is a TreeNode object, not an array.\n\nThe given tree [4,2,6,1,3,null,null] is represented by the following diagram:\n\n 4\n / \\\n 2 6\n / \\ \n 1 3 \n\nwhile the minimum difference in this tree is 1, it occurs between node 1 and node 2, also between node 3 and node 2.\n\n\n
Note:
\n\n100.\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6839\u7ed3\u70b9 root, \u8fd4\u56de\u6811\u4e2d\u4efb\u610f\u4e24\u8282\u70b9\u7684\u5dee\u7684\u6700\u5c0f\u503c\u3002
\u793a\u4f8b\uff1a
\n\n\n\u8f93\u5165: root = [4,2,6,1,3,null,null]\n\u8f93\u51fa: 1\n\u89e3\u91ca:\n\u6ce8\u610f\uff0croot\u662f\u6811\u7ed3\u70b9\u5bf9\u8c61(TreeNode object)\uff0c\u800c\u4e0d\u662f\u6570\u7ec4\u3002\n\n\u7ed9\u5b9a\u7684\u6811 [4,2,6,1,3,null,null] \u53ef\u8868\u793a\u4e3a\u4e0b\u56fe:\n\n 4\n / \\\n 2 6\n / \\ \n 1 3 \n\n\u6700\u5c0f\u7684\u5dee\u503c\u662f 1, \u5b83\u662f\u8282\u70b91\u548c\u8282\u70b92\u7684\u5dee\u503c, \u4e5f\u662f\u8282\u70b93\u548c\u8282\u70b92\u7684\u5dee\u503c\u3002\n\n
\u6ce8\u610f\uff1a
\n\n2 \u5230 100\u3002\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u53c9\u641c\u7d22\u6811\u7684\u6839\u7ed3\u70b9 root, \u8fd4\u56de\u6811\u4e2d\u4efb\u610f\u4e24\u8282\u70b9\u7684\u5dee\u7684\u6700\u5c0f\u503c\u3002
\u793a\u4f8b\uff1a
\n\n\n\u8f93\u5165: root = [4,2,6,1,3,null,null]\n\u8f93\u51fa: 1\n\u89e3\u91ca:\n\u6ce8\u610f\uff0croot\u662f\u6811\u7ed3\u70b9\u5bf9\u8c61(TreeNode object)\uff0c\u800c\u4e0d\u662f\u6570\u7ec4\u3002\n\n\u7ed9\u5b9a\u7684\u6811 [4,2,6,1,3,null,null] \u53ef\u8868\u793a\u4e3a\u4e0b\u56fe:\n\n 4\n / \\\n 2 6\n / \\ \n 1 3 \n\n\u6700\u5c0f\u7684\u5dee\u503c\u662f 1, \u5b83\u662f\u8282\u70b91\u548c\u8282\u70b92\u7684\u5dee\u503c, \u4e5f\u662f\u8282\u70b93\u548c\u8282\u70b92\u7684\u5dee\u503c\u3002\n\n
\u6ce8\u610f\uff1a
\n\n2 \u5230 100\u3002Given a 2D matrix matrix, find the sum of the elements inside the rectangle defined by its upper left corner (row1, col1) and lower right corner (row2, col2).
\n\n\n
\nThe above rectangle (with the red border) is defined by (row1, col1) = (2, 1) and (row2, col2) = (4, 3), which contains sum = 8.\n
Example:
\n
\nGiven matrix = [\n [3, 0, 1, 4, 2],\n [5, 6, 3, 2, 1],\n [1, 2, 0, 1, 5],\n [4, 1, 0, 1, 7],\n [1, 0, 3, 0, 5]\n]\n\nsumRegion(2, 1, 4, 3) -> 8\nsumRegion(1, 1, 2, 2) -> 11\nsumRegion(1, 2, 2, 4) -> 12\n\n\n\n
Note:
\n
\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff0c\u8ba1\u7b97\u5176\u5b50\u77e9\u5f62\u8303\u56f4\u5185\u5143\u7d20\u7684\u603b\u548c\uff0c\u8be5\u5b50\u77e9\u9635\u7684\u5de6\u4e0a\u89d2\u4e3a (row1, col1) \uff0c\u53f3\u4e0b\u89d2\u4e3a (row2, col2)\u3002
\n\n
\n\u4e0a\u56fe\u5b50\u77e9\u9635\u5de6\u4e0a\u89d2 (row1, col1) = (2, 1) \uff0c\u53f3\u4e0b\u89d2(row2, col2) = (4, 3)\uff0c\u8be5\u5b50\u77e9\u5f62\u5185\u5143\u7d20\u7684\u603b\u548c\u4e3a 8\u3002
\u793a\u4f8b:
\n\n\u7ed9\u5b9a matrix = [\n [3, 0, 1, 4, 2],\n [5, 6, 3, 2, 1],\n [1, 2, 0, 1, 5],\n [4, 1, 0, 1, 7],\n [1, 0, 3, 0, 5]\n]\n\nsumRegion(2, 1, 4, 3) -> 8\nsumRegion(1, 1, 2, 2) -> 11\nsumRegion(1, 2, 2, 4) -> 12\n\n\n
\u8bf4\u660e:
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u4e8c\u7ef4\u77e9\u9635\uff0c\u8ba1\u7b97\u5176\u5b50\u77e9\u5f62\u8303\u56f4\u5185\u5143\u7d20\u7684\u603b\u548c\uff0c\u8be5\u5b50\u77e9\u9635\u7684\u5de6\u4e0a\u89d2\u4e3a (row1, col1) \uff0c\u53f3\u4e0b\u89d2\u4e3a (row2, col2)\u3002
\n\n
\n\u4e0a\u56fe\u5b50\u77e9\u9635\u5de6\u4e0a\u89d2 (row1, col1) = (2, 1) \uff0c\u53f3\u4e0b\u89d2(row2, col2) = (4, 3)\uff0c\u8be5\u5b50\u77e9\u5f62\u5185\u5143\u7d20\u7684\u603b\u548c\u4e3a 8\u3002
\u793a\u4f8b:
\n\n\u7ed9\u5b9a matrix = [\n [3, 0, 1, 4, 2],\n [5, 6, 3, 2, 1],\n [1, 2, 0, 1, 5],\n [4, 1, 0, 1, 7],\n [1, 0, 3, 0, 5]\n]\n\nsumRegion(2, 1, 4, 3) -> 8\nsumRegion(1, 1, 2, 2) -> 11\nsumRegion(1, 2, 2, 4) -> 12\n\n\n
\u8bf4\u660e:
\n\nGiven a balanced parentheses string S, compute the score of the string based on the following rule:
() has score 1AB has score A + B, where A and B are balanced parentheses strings.(A) has score 2 * A, where A is a balanced parentheses string.\n\n
Example 1:
\n\n\nInput: "()"\nOutput: 1\n\n\n
Example 2:
\n\n\nInput: "(())"\nOutput: 2\n\n\n
Example 3:
\n\n\nInput: "()()"\nOutput: 2\n\n\n
Example 4:
\n\n\nInput: "(()(()))"\nOutput: 6\n\n\n
\n\n
Note:
\n\nS is a balanced parentheses string, containing only ( and ).2 <= S.length <= 50\u7ed9\u5b9a\u4e00\u4e2a\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32 S\uff0c\u6309\u4e0b\u8ff0\u89c4\u5219\u8ba1\u7b97\u8be5\u5b57\u7b26\u4e32\u7684\u5206\u6570\uff1a
() \u5f97 1 \u5206\u3002AB \u5f97 A + B \u5206\uff0c\u5176\u4e2d A \u548c B \u662f\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32\u3002(A) \u5f97 2 * A \u5206\uff0c\u5176\u4e2d A \u662f\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32\u3002\n\n
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1a "()"\n\u8f93\u51fa\uff1a 1\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1a "(())"\n\u8f93\u51fa\uff1a 2\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1a "()()"\n\u8f93\u51fa\uff1a 2\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\u8f93\u5165\uff1a "(()(()))"\n\u8f93\u51fa\uff1a 6\n\n\n
\n\n
\u63d0\u793a\uff1a
\n\nS \u662f\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32\uff0c\u4e14\u53ea\u542b\u6709 ( \u548c ) \u30022 <= S.length <= 50\u7ed9\u5b9a\u4e00\u4e2a\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32 S\uff0c\u6309\u4e0b\u8ff0\u89c4\u5219\u8ba1\u7b97\u8be5\u5b57\u7b26\u4e32\u7684\u5206\u6570\uff1a
() \u5f97 1 \u5206\u3002AB \u5f97 A + B \u5206\uff0c\u5176\u4e2d A \u548c B \u662f\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32\u3002(A) \u5f97 2 * A \u5206\uff0c\u5176\u4e2d A \u662f\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32\u3002\n\n
\u793a\u4f8b 1\uff1a
\n\n\u8f93\u5165\uff1a "()"\n\u8f93\u51fa\uff1a 1\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\u8f93\u5165\uff1a "(())"\n\u8f93\u51fa\uff1a 2\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\u8f93\u5165\uff1a "()()"\n\u8f93\u51fa\uff1a 2\n\n\n
\u793a\u4f8b 4\uff1a
\n\n\u8f93\u5165\uff1a "(()(()))"\n\u8f93\u51fa\uff1a 6\n\n\n
\n\n
\u63d0\u793a\uff1a
\n\nS \u662f\u5e73\u8861\u62ec\u53f7\u5b57\u7b26\u4e32\uff0c\u4e14\u53ea\u542b\u6709 ( \u548c ) \u30022 <= S.length <= 50Give a string s, count the number of non-empty (contiguous) substrings that have the same number of 0's and 1's, and all the 0's and all the 1's in these substrings are grouped consecutively. \n
Substrings that occur multiple times are counted the number of times they occur.
\n\nExample 1:
\n
\nInput: \"00110011\"\nOutput: 6\nExplanation: There are 6 substrings that have equal number of consecutive 1's and 0's: \"0011\", \"01\", \"1100\", \"10\", \"0011\", and \"01\".\n\n\n\n
Notice that some of these substrings repeat and are counted the number of times they occur.\n
Also, \"00110011\" is not a valid substring because all the 0's (and 1's) are not grouped together.\n
Example 2:
\n
\nInput: \"10101\"\nOutput: 4\nExplanation: There are 4 substrings: \"10\", \"01\", \"10\", \"01\" that have equal number of consecutive 1's and 0's.\n\n\n\n
Note:\n
s.length will be between 1 and 50,000.s will only consist of \"0\" or \"1\" characters.\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s\uff0c\u8ba1\u7b97\u5177\u6709\u76f8\u540c\u6570\u91cf0\u548c1\u7684\u975e\u7a7a(\u8fde\u7eed)\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u91cf\uff0c\u5e76\u4e14\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u67090\u548c\u6240\u67091\u90fd\u662f\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u3002
\u91cd\u590d\u51fa\u73b0\u7684\u5b50\u4e32\u8981\u8ba1\u7b97\u5b83\u4eec\u51fa\u73b0\u7684\u6b21\u6570\u3002
\n\n\u793a\u4f8b 1 :
\n\n\n\u8f93\u5165: "00110011"\n\u8f93\u51fa: 6\n\u89e3\u91ca: \u67096\u4e2a\u5b50\u4e32\u5177\u6709\u76f8\u540c\u6570\u91cf\u7684\u8fde\u7eed1\u548c0\uff1a“0011”\uff0c“01”\uff0c“1100”\uff0c“10”\uff0c“0011” \u548c “01”\u3002\n\n\u8bf7\u6ce8\u610f\uff0c\u4e00\u4e9b\u91cd\u590d\u51fa\u73b0\u7684\u5b50\u4e32\u8981\u8ba1\u7b97\u5b83\u4eec\u51fa\u73b0\u7684\u6b21\u6570\u3002\n\n\u53e6\u5916\uff0c“00110011”\u4e0d\u662f\u6709\u6548\u7684\u5b50\u4e32\uff0c\u56e0\u4e3a\u6240\u6709\u76840\uff08\u548c1\uff09\u6ca1\u6709\u7ec4\u5408\u5728\u4e00\u8d77\u3002\n\n\n
\u793a\u4f8b 2 :
\n\n\n\u8f93\u5165: "10101"\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u67094\u4e2a\u5b50\u4e32\uff1a“10”\uff0c“01”\uff0c“10”\uff0c“01”\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u6570\u91cf\u7684\u8fde\u7eed1\u548c0\u3002\n\n\n
\u6ce8\u610f\uff1a
\n\ns.length \u57281\u523050,000\u4e4b\u95f4\u3002s \u53ea\u5305\u542b“0”\u6216“1”\u5b57\u7b26\u3002\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32 s\uff0c\u8ba1\u7b97\u5177\u6709\u76f8\u540c\u6570\u91cf0\u548c1\u7684\u975e\u7a7a(\u8fde\u7eed)\u5b50\u5b57\u7b26\u4e32\u7684\u6570\u91cf\uff0c\u5e76\u4e14\u8fd9\u4e9b\u5b50\u5b57\u7b26\u4e32\u4e2d\u7684\u6240\u67090\u548c\u6240\u67091\u90fd\u662f\u7ec4\u5408\u5728\u4e00\u8d77\u7684\u3002
\u91cd\u590d\u51fa\u73b0\u7684\u5b50\u4e32\u8981\u8ba1\u7b97\u5b83\u4eec\u51fa\u73b0\u7684\u6b21\u6570\u3002
\n\n\u793a\u4f8b 1 :
\n\n\n\u8f93\u5165: "00110011"\n\u8f93\u51fa: 6\n\u89e3\u91ca: \u67096\u4e2a\u5b50\u4e32\u5177\u6709\u76f8\u540c\u6570\u91cf\u7684\u8fde\u7eed1\u548c0\uff1a“0011”\uff0c“01”\uff0c“1100”\uff0c“10”\uff0c“0011” \u548c “01”\u3002\n\n\u8bf7\u6ce8\u610f\uff0c\u4e00\u4e9b\u91cd\u590d\u51fa\u73b0\u7684\u5b50\u4e32\u8981\u8ba1\u7b97\u5b83\u4eec\u51fa\u73b0\u7684\u6b21\u6570\u3002\n\n\u53e6\u5916\uff0c“00110011”\u4e0d\u662f\u6709\u6548\u7684\u5b50\u4e32\uff0c\u56e0\u4e3a\u6240\u6709\u76840\uff08\u548c1\uff09\u6ca1\u6709\u7ec4\u5408\u5728\u4e00\u8d77\u3002\n\n\n
\u793a\u4f8b 2 :
\n\n\n\u8f93\u5165: "10101"\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u67094\u4e2a\u5b50\u4e32\uff1a“10”\uff0c“01”\uff0c“10”\uff0c“01”\uff0c\u5b83\u4eec\u5177\u6709\u76f8\u540c\u6570\u91cf\u7684\u8fde\u7eed1\u548c0\u3002\n\n\n
\u6ce8\u610f\uff1a
\n\ns.length \u57281\u523050,000\u4e4b\u95f4\u3002s \u53ea\u5305\u542b“0”\u6216“1”\u5b57\u7b26\u3002You are given coins of different denominations and a total amount of money. Write a function to compute the number of combinations that make up that amount. You may assume that you have infinite number of each kind of coin.
\n\nNote: You can assume that
\n\n\n\n
Example 1:
\n\n\nInput: amount = 5, coins = [1, 2, 5]\nOutput: 4\nExplanation: there are four ways to make up the amount:\n5=5\n5=2+2+1\n5=2+1+1+1\n5=1+1+1+1+1\n\n\n
\n\n
Example 2:
\n\n\nInput: amount = 3, coins = [2]\nOutput: 0\nExplanation: the amount of 3 cannot be made up just with coins of 2.\n\n\n
\n\n
Example 3:
\n\n\nInput: amount = 10, coins = [10] \nOutput: 1\n\n\n
\n
\u7ed9\u5b9a\u4e0d\u540c\u9762\u989d\u7684\u786c\u5e01\u548c\u4e00\u4e2a\u603b\u91d1\u989d\u3002\u5199\u51fa\u51fd\u6570\u6765\u8ba1\u7b97\u53ef\u4ee5\u51d1\u6210\u603b\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u3002\u5047\u8bbe\u6bcf\u4e00\u79cd\u9762\u989d\u7684\u786c\u5e01\u6709\u65e0\u9650\u4e2a\u3002
\n\n\u6ce8\u610f: \u4f60\u53ef\u4ee5\u5047\u8bbe
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: amount = 5, coins = [1, 2, 5]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u6709\u56db\u79cd\u65b9\u5f0f\u53ef\u4ee5\u51d1\u6210\u603b\u91d1\u989d:\n5=5\n5=2+2+1\n5=2+1+1+1\n5=1+1+1+1+1\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: amount = 3, coins = [2]\n\u8f93\u51fa: 0\n\u89e3\u91ca: \u53ea\u7528\u9762\u989d2\u7684\u786c\u5e01\u4e0d\u80fd\u51d1\u6210\u603b\u91d1\u989d3\u3002\n\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: amount = 10, coins = [10] \n\u8f93\u51fa: 1\n\n
\u7ed9\u5b9a\u4e0d\u540c\u9762\u989d\u7684\u786c\u5e01\u548c\u4e00\u4e2a\u603b\u91d1\u989d\u3002\u5199\u51fa\u51fd\u6570\u6765\u8ba1\u7b97\u53ef\u4ee5\u51d1\u6210\u603b\u91d1\u989d\u7684\u786c\u5e01\u7ec4\u5408\u6570\u3002\u5047\u8bbe\u6bcf\u4e00\u79cd\u9762\u989d\u7684\u786c\u5e01\u6709\u65e0\u9650\u4e2a\u3002
\n\n\u6ce8\u610f: \u4f60\u53ef\u4ee5\u5047\u8bbe
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: amount = 5, coins = [1, 2, 5]\n\u8f93\u51fa: 4\n\u89e3\u91ca: \u6709\u56db\u79cd\u65b9\u5f0f\u53ef\u4ee5\u51d1\u6210\u603b\u91d1\u989d:\n5=5\n5=2+2+1\n5=2+1+1+1\n5=1+1+1+1+1\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: amount = 3, coins = [2]\n\u8f93\u51fa: 0\n\u89e3\u91ca: \u53ea\u7528\u9762\u989d2\u7684\u786c\u5e01\u4e0d\u80fd\u51d1\u6210\u603b\u91d1\u989d3\u3002\n\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: amount = 10, coins = [10] \n\u8f93\u51fa: 1\n\n*/\n\n\n/**\n * @param {number} amount\n * @param {number[]} coins\n * @return {number}\n */\nvar change = function(amount, coins) {\n \n};"} {"doc_id": "58be8805e68af9c695f0671d209b8f55", "text": "\ufeff/*Write a script that shows the sign (+, - or 0) of the product of three real numbers, without calculating it.\nUse a sequence of if operators.\nExamples:\n\na\tb\t c\t result\n5\t2\t 2\t +\n-2\t-2\t 1\t +\n-2\t4\t 3\t -\n0\t-2.5\t4\t 0\n-1\t-0.5\t-5.1\t- */\n\nconsole.log('============================================');\nconsole.log('02.Multiplication sign');\nconsole.log('---------------------------------');\n\nfunction showSign(a, b, c) {\n var numbers = [a, b ,c],\n minusCount = 0,\n i,\n len = numbers.length;\n\n if (a === 0 || b === 0 || c === 0) {\n console.log('a= ' + a + ', b= ' + b + ', c= ' + c + ' --> ' + '0');\n }\n else {\n for (i = 0; i < len; i += 1) {\n if (numbers[i] < 0) {\n minusCount += 1;\n }\n }\n if (minusCount % 2 === 0) {\n console.log('a= ' + a + ', b= ' + b + ', c= ' + c + ' --> ' + '+');\n }\n else {\n console.log('a= ' + a + ', b= ' + b + ', c= ' + c + ' --> ' + '-');\n }\n }\n}\n\nshowSign(5, 2, 2);\nshowSign(-2, -2, 1);\nshowSign(-2, 4, 3);\nshowSign(0, -2.5, 4);\nshowSign(-1, -0.5, -5.1);"} {"doc_id": "5922f9c187ace7321f23cd7395479a77", "text": "var mapOnes = {\n 0: \"\",\n 1: \"One\",\n 2: \"Two\",\n 3: \"Three\",\n 4: \"Four\",\n 5: \"Five\",\n 6: \"Six\",\n 7: \"Seven\",\n 8: \"Eight\",\n 9: \"Nine\",\n}\n\nvar mapTens = {\n 10: \"Ten\",\n 11: \"Eleven\",\n 12: \"Twelve\",\n 13: \"Thirteen\",\n 14: \"Fourteen\",\n 15: \"Fifteen\",\n 16: \"Sixteen\",\n 17: \"Seventeen\",\n 18: \"Eighteen\",\n 19: \"Nineteen\",\n}\n\nvar mapTenths = {\n 2: \"Twenty\",\n 3: \"Thirty\",\n 4: \"Forty\",\n 5: \"Fifty\",\n 6: \"Sixty\",\n 7: \"Seventy\",\n 8: \"Eighty\",\n 9: \"Ninety\",\n}\n\nfunction int(val) {\n return parseInt(val);\n}\n\nfunction str(val) {\n return val.toString();\n}\n\n\n// Convert ones number to word.\n// ----------\n// Parameters\n// ----------\n// num: int\n// Single digit integer number\nfunction convertOnes(num) {\n if (str(num).length > 1) {\n throw new Error(\"Must have at most 1 digit\");\n }\n\n num = int(num);\n return mapOnes[num];\n}\n\n\n// Convert tenths number to word.\n// ----------\n// Parameters\n// ----------\n// num: int\n// Double digit integer number\nfunction convertTenths(num) {\n if (str(num).length > 2) {\n throw new Error(\"Must have at most 2 digits\");\n }\n\n num = int(num);\n var bases = \"\";\n\n // less than 10\n if (num < 10) {\n return mapOnes[num];\n }\n\n\n // 10-19\n if (10 <= num && num < 20) {\n return mapTens[num];\n }\n\n // 20-99\n var firstNum = mapTenths[int(str(num)[0])];\n var secondNum = mapOnes[int(str(num)[1])];\n\n if (!secondNum) {\n return firstNum;\n }\n\n return firstNum + \" \" + secondNum;\n}\n\n\n// Given hundredth, tenth and one integer number for base (e.g. Billion, Million), return converted word\n// ----------\n// Parameters\n// ----------\n// hundredth: int\n// Hundredth number\n// tenth: int\n// Tenth number\n// one: int\n// One number\n// base: string\n// Base value\nfunction getDollar(hundredth, tenth, one, base) {\n var dollarWord = \"\";\n if (hundredth) {\n dollarWord += convertOnes(hundredth) + \" Hundred\";\n }\n\n // Add \"And\" if there's numbers after hundredths\n if (hundredth && (tenth || one)) {\n dollarWord += \" And \";\n }\n\n if (tenth || one) {\n dollarWord += convertTenths(int(str(tenth) + str(one)));\n }\n\n if (base) {\n dollarWord += \" \" + base;\n }\n\n return dollarWord;\n}\n\n\nfunction getBillion(hundredth, tenth, one) {\n return getDollar(hundredth, tenth, one, \"Billion\");\n}\n\n\nfunction getMillion(hundredth, tenth, one) {\n return getDollar(hundredth, tenth, one, \"Million\");\n}\n\n\nfunction getThousand(hundredth, tenth, one) {\n return getDollar(hundredth, tenth, one, \"Thousand\");\n}\n\n\nfunction getOne(hundredth, tenth, one) {\n return getDollar(hundredth, tenth, one, \"\");\n}\n\n// Given tenth and one integer number (for cent), return converted word\n// ----------\n// Parameters\n// ----------\n// tenth: int\n// Tenth number\n// one: int\n// One number\nfunction getCent(tenth, one) {\n var centWord = \"\";\n if (tenth || one) {\n centWord += convertTenths(int(str(tenth) + str(one)));\n }\n\n if (centWord) {\n centWord = \"Cents \" + centWord + \" \";\n }\n\n return centWord;\n}\n\n\nfunction getIndex(val, index, defaultVal) {\n defaultVal = defaultVal || 0;\n if (val[index] === undefined) {\n return defaultVal;\n } else {\n return val[index];\n }\n}\n\n\n// Given a max 3 character number, extract and return hundredth, tenth and one value.\n// Return array in format [int, int, int] for values [hundredth, tenth, one]\n// ----------\n// Parameters\n// ----------\n// num: string\n// Number in string\n//\nfunction extract(num) {\n var hundredth = 0;\n var tenth = 0;\n var one = 0;\n if (num.length === 3) {\n hundredth = int(num[0]);\n tenth = int(num[1]);\n one = int(num[2]);\n }\n\n if (num.length === 2) {\n tenth = int(num[0]);\n one = int(num[1]);\n }\n\n if (num.length === 1) {\n one = int(num[0]);\n }\n\n return [hundredth, tenth, one];\n}\n\n\n// Generate word for dollar\n// ----------\n// Parameters\n// ----------\n// num: string\n// Dollar number in string\nfunction generateDollarWord(num) {\n var word = \"\";\n var billionNum;\n var millionNum;\n var thousandNum;\n var oneNum;\n var hundredth;\n var tenth;\n var one;\n var extractArr;\n\n // at least 1 billion\n if (num.length > 9) {\n billionNum = int(num.slice(0, num.length - 9));\n num = str(int(num) - (billionNum * int(1e9)));\n extractArr = extract(str(billionNum));\n hundredth = extractArr[0];\n tenth = extractArr[1];\n one = extractArr[2];\n word += getBillion(hundredth, tenth, one) + \" \";\n }\n\n // at least 1 million\n if (num.length > 6) {\n millionNum = int(num.slice(0, num.length - 6));\n num = str(int(num) - (millionNum * int(1e6)));\n extractArr = extract(str(millionNum));\n hundredth = extractArr[0];\n tenth = extractArr[1];\n one = extractArr[2];\n word += getMillion(hundredth, tenth, one) + \" \";\n }\n\n // at least 1 thousand\n if (num.length > 3) {\n thousandNum = int(num.slice(0, num.length - 3));\n num = str(int(num) - (thousandNum * int(1e3)));\n extractArr = extract(str(thousandNum));\n hundredth = extractArr[0];\n tenth = extractArr[1];\n one = extractArr[2];\n word += getThousand(hundredth, tenth, one) + \" \";\n }\n\n // at least 1\n if (int(num) && num.length > 0) {\n oneNum = int(num.slice(0, num.length));\n num = str(int(num) - oneNum);\n extractArr = extract(str(oneNum));\n\n hundredth = extractArr[0];\n tenth = extractArr[1];\n one = extractArr[2];\n word += getOne(hundredth, tenth, one) + \" \";\n }\n\n return word;\n}\n\n\n// Generate word for cent\n// ----------\n// Parameters\n// ----------\n// num: string\n// Cent number in string\nfunction generateCentWord(num) {\n var word = \"\";\n var hundredth;\n var tenth;\n var one;\n var extractArr;\n\n extractArr = extract(str(num));\n hundredth = extractArr[0];\n tenth = extractArr[1];\n one = extractArr[2];\n word += getCent(tenth, one);\n return word;\n}\n\n\nfunction validate(amt) {\n if (amt === \"\") {\n throw new Error(\"Please enter a valid amount\");\n }\n\n // amt MUST be in string to avoid accidental round off\n if (parseFloat(amt) > parseFloat(1e11)) {\n throw new Error(\"Please enter an amount smaller than 100 billion\");\n }\n\n if (getIndex(amt.split(\".\"), 1, \"\").length > 2) {\n throw new Error(\"Please enter an amount within 2 decimal place\");\n }\n}\n\n\nfunction generateWord(amt) {\n var amtList;\n var dollarAmt;\n var centAmt;\n var dollarWord;\n var centWord;\n\n // remove commas and spaces from word\n amt = amt.replace(/,/g, \"\").replace(/ /g, \"\");\n validate(amt);\n\n amt = str(parseFloat(amt).toFixed(2));\n amtList = amt.split(\".\");\n dollarAmt = getIndex(amtList, 0);\n centAmt = getIndex(amtList, 1);\n\n dollarWord = generateDollarWord(dollarAmt);\n centWord = generateCentWord(centAmt);\n\n if (!dollarWord) {\n return centWord + \"Only\";\n }\n\n if (!centWord) {\n return dollarWord + \"Only\";\n }\n\n return dollarWord + \"And \" + centWord + \"Only\";\n}\n\n\nmodule.exports = generateWord;\n"} {"doc_id": "5ae4431c332848180c072d670ab95a66", "text": "// This is an input class. Do not edit.\nclass BST {\n constructor(value) {\n this.value = value;\n this.left = null;\n this.right = null;\n }\n}\n\n// Solution 1\n// O(n) time / O(n) space\n// n - number of nodes in the tree\n\nconst findKthLargestValueInBST = (tree, k) => {\n const sortedNodeValues = [];\n inOrderTraverse(tree, sortedNodeValues);\n return sortedNodeValues[sortedNodeValues.length - k];\n};\n\nconst inOrderTraverse = (node, sortedNodeValues) => {\n if (node === null) return;\n\n inOrderTraverse(node.left, sortedNodeValues);\n sortedNodeValues.push(node.value);\n inOrderTraverse(node.right, sortedNodeValues);\n};\n\n// Solution 2\n// O(n) time / O(1) space\n// n - number of nodes in the tree\n\nclass TreeInfo {\n constructor(numberOfNodesVisited, latestVisitedNodeValue) {\n this.numberOfNodesVisited = numberOfNodesVisited;\n this.latestVisitedNodeValue = latestVisitedNodeValue;\n }\n}\n\nconst findKthLargestValueInBST = (tree, k) => {\n const treeInfo = new TreeInfo(0, -1);\n reverseInOrderTraverse(tree, k, treeInfo);\n return treeInfo.latestVisitedNodeValue;\n};\n\nconst reverseInOrderTraverse = (node, k, treeInfo) => {\n if (node === null || treeInfo.numberOfNodesVisited >= k) return;\n\n reverseInOrderTraverse(node.right, k, treeInfo);\n if (treeInfo.numberOfNodesVisited < k) {\n treeInfo.numberOfNodesVisited++;\n treeInfo.latestVisitedNodeValue = node.value;\n reverseInOrderTraverse(node.left, k, treeInfo);\n }\n};\n"} {"doc_id": "5b14e665706d1efc9611fba580097fdd", "text": "/*\n
We partition a row of numbers A into at most K adjacent (non-empty) groups, then our score is the sum of the average of each group. What is the largest score we can achieve?
Note that our partition must use every number in A, and that scores are not necessarily integers.
\n\n\nExample:\nInput: \nA = [9,1,2,3,9]\nK = 3\nOutput: 20\nExplanation: \nThe best choice is to partition A into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.\nWe could have also partitioned A into [9, 1], [2], [3, 9], for example.\nThat partition would lead to a score of 5 + 2 + 6 = 13, which is worse.\n\n\n
\n\n
Note:
\n\n1 <= A.length <= 100.1 <= A[i] <= 10000.1 <= K <= A.length.10^-6 of the correct answer will be accepted as correct.\u6211\u4eec\u5c06\u7ed9\u5b9a\u7684\u6570\u7ec4 A \u5206\u6210 K \u4e2a\u76f8\u90bb\u7684\u975e\u7a7a\u5b50\u6570\u7ec4 \uff0c\u6211\u4eec\u7684\u5206\u6570\u7531\u6bcf\u4e2a\u5b50\u6570\u7ec4\u5185\u7684\u5e73\u5747\u503c\u7684\u603b\u548c\u6784\u6210\u3002\u8ba1\u7b97\u6211\u4eec\u6240\u80fd\u5f97\u5230\u7684\u6700\u5927\u5206\u6570\u662f\u591a\u5c11\u3002
\u6ce8\u610f\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 A \u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u4e14\u5206\u6570\u4e0d\u4e00\u5b9a\u9700\u8981\u662f\u6574\u6570\u3002
\n\n\n\u793a\u4f8b:\n\u8f93\u5165: \nA = [9,1,2,3,9]\nK = 3\n\u8f93\u51fa: 20\n\u89e3\u91ca: \nA \u7684\u6700\u4f18\u5206\u7ec4\u662f[9], [1, 2, 3], [9]. \u5f97\u5230\u7684\u5206\u6570\u662f 9 + (1 + 2 + 3) / 3 + 9 = 20.\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u628a A \u5206\u6210[9, 1], [2], [3, 9].\n\u8fd9\u6837\u7684\u5206\u7ec4\u5f97\u5230\u7684\u5206\u6570\u4e3a 5 + 2 + 6 = 13, \u4f46\u4e0d\u662f\u6700\u5927\u503c.\n\n\n
\u8bf4\u660e:
\n\n1 <= A.length <= 100.1 <= A[i] <= 10000.1 <= K <= A.length.10^-6 \u5185\u88ab\u89c6\u4e3a\u662f\u6b63\u786e\u7684\u3002\u6211\u4eec\u5c06\u7ed9\u5b9a\u7684\u6570\u7ec4 A \u5206\u6210 K \u4e2a\u76f8\u90bb\u7684\u975e\u7a7a\u5b50\u6570\u7ec4 \uff0c\u6211\u4eec\u7684\u5206\u6570\u7531\u6bcf\u4e2a\u5b50\u6570\u7ec4\u5185\u7684\u5e73\u5747\u503c\u7684\u603b\u548c\u6784\u6210\u3002\u8ba1\u7b97\u6211\u4eec\u6240\u80fd\u5f97\u5230\u7684\u6700\u5927\u5206\u6570\u662f\u591a\u5c11\u3002
\u6ce8\u610f\u6211\u4eec\u5fc5\u987b\u4f7f\u7528 A \u6570\u7ec4\u4e2d\u7684\u6bcf\u4e00\u4e2a\u6570\u8fdb\u884c\u5206\u7ec4\uff0c\u5e76\u4e14\u5206\u6570\u4e0d\u4e00\u5b9a\u9700\u8981\u662f\u6574\u6570\u3002
\n\n\n\u793a\u4f8b:\n\u8f93\u5165: \nA = [9,1,2,3,9]\nK = 3\n\u8f93\u51fa: 20\n\u89e3\u91ca: \nA \u7684\u6700\u4f18\u5206\u7ec4\u662f[9], [1, 2, 3], [9]. \u5f97\u5230\u7684\u5206\u6570\u662f 9 + (1 + 2 + 3) / 3 + 9 = 20.\n\u6211\u4eec\u4e5f\u53ef\u4ee5\u628a A \u5206\u6210[9, 1], [2], [3, 9].\n\u8fd9\u6837\u7684\u5206\u7ec4\u5f97\u5230\u7684\u5206\u6570\u4e3a 5 + 2 + 6 = 13, \u4f46\u4e0d\u662f\u6700\u5927\u503c.\n\n\n
\u8bf4\u660e:
\n\n1 <= A.length <= 100.1 <= A[i] <= 10000.1 <= K <= A.length.10^-6 \u5185\u88ab\u89c6\u4e3a\u662f\u6b63\u786e\u7684\u3002Say you have an array for which the ith element is the price of a given stock on day i.
\n\nIf you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit.
\n\nNote that you cannot sell a stock before you buy one.
\n\nExample 1:
\n\n\nInput: [7,1,5,3,6,4]\nOutput: 5\nExplanation: Buy on day 2 (price = 1) and sell on day 5 (price = 6), profit = 6-1 = 5.\n Not 7-1 = 6, as selling price needs to be larger than buying price.\n\n\n
Example 2:
\n\n\nInput: [7,6,4,3,1]\nOutput: 0\nExplanation: In this case, no transaction is done, i.e. max profit = 0.\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5b83\u7684\u7b2c i \u4e2a\u5143\u7d20\u662f\u4e00\u652f\u7ed9\u5b9a\u80a1\u7968\u7b2c i \u5929\u7684\u4ef7\u683c\u3002
\n\n\u5982\u679c\u4f60\u6700\u591a\u53ea\u5141\u8bb8\u5b8c\u6210\u4e00\u7b14\u4ea4\u6613\uff08\u5373\u4e70\u5165\u548c\u5356\u51fa\u4e00\u652f\u80a1\u7968\uff09\uff0c\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002
\n\n\u6ce8\u610f\u4f60\u4e0d\u80fd\u5728\u4e70\u5165\u80a1\u7968\u524d\u5356\u51fa\u80a1\u7968\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: [7,1,5,3,6,4]\n\u8f93\u51fa: 5\n\u89e3\u91ca: \u5728\u7b2c 2 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 5 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 6\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u6700\u5927\u5229\u6da6 = 6-1 = 5 \u3002\n \u6ce8\u610f\u5229\u6da6\u4e0d\u80fd\u662f 7-1 = 6, \u56e0\u4e3a\u5356\u51fa\u4ef7\u683c\u9700\u8981\u5927\u4e8e\u4e70\u5165\u4ef7\u683c\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: [7,6,4,3,1]\n\u8f93\u51fa: 0\n\u89e3\u91ca: \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u6ca1\u6709\u4ea4\u6613\u5b8c\u6210, \u6240\u4ee5\u6700\u5927\u5229\u6da6\u4e3a 0\u3002\n\n
\u7ed9\u5b9a\u4e00\u4e2a\u6570\u7ec4\uff0c\u5b83\u7684\u7b2c i \u4e2a\u5143\u7d20\u662f\u4e00\u652f\u7ed9\u5b9a\u80a1\u7968\u7b2c i \u5929\u7684\u4ef7\u683c\u3002
\n\n\u5982\u679c\u4f60\u6700\u591a\u53ea\u5141\u8bb8\u5b8c\u6210\u4e00\u7b14\u4ea4\u6613\uff08\u5373\u4e70\u5165\u548c\u5356\u51fa\u4e00\u652f\u80a1\u7968\uff09\uff0c\u8bbe\u8ba1\u4e00\u4e2a\u7b97\u6cd5\u6765\u8ba1\u7b97\u4f60\u6240\u80fd\u83b7\u53d6\u7684\u6700\u5927\u5229\u6da6\u3002
\n\n\u6ce8\u610f\u4f60\u4e0d\u80fd\u5728\u4e70\u5165\u80a1\u7968\u524d\u5356\u51fa\u80a1\u7968\u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: [7,1,5,3,6,4]\n\u8f93\u51fa: 5\n\u89e3\u91ca: \u5728\u7b2c 2 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 1\uff09\u7684\u65f6\u5019\u4e70\u5165\uff0c\u5728\u7b2c 5 \u5929\uff08\u80a1\u7968\u4ef7\u683c = 6\uff09\u7684\u65f6\u5019\u5356\u51fa\uff0c\u6700\u5927\u5229\u6da6 = 6-1 = 5 \u3002\n \u6ce8\u610f\u5229\u6da6\u4e0d\u80fd\u662f 7-1 = 6, \u56e0\u4e3a\u5356\u51fa\u4ef7\u683c\u9700\u8981\u5927\u4e8e\u4e70\u5165\u4ef7\u683c\u3002\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: [7,6,4,3,1]\n\u8f93\u51fa: 0\n\u89e3\u91ca: \u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b, \u6ca1\u6709\u4ea4\u6613\u5b8c\u6210, \u6240\u4ee5\u6700\u5927\u5229\u6da6\u4e3a 0\u3002\n\n*/\n\n\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n \n};"} {"doc_id": "60075ee218cd467d73c092bcfe34a630", "text": "var numValor1 = parseInt(prompt(\"Informe o primeiro valor: \"));\nvar numValor2 = parseInt(prompt(\"Informe o segundo valor: \"));\n\nvar opcaoOperacao = parseInt(prompt(\"Qual opera\u00e7\u00e3o quer fazer:\\n 1-somar \\n 2-sub \\n 3-mult \\n 4-div \"));\n\nif(opcaoOperacao == 1){\n var resultado = numValor1 + numValor2;\n document.write(\"
Implement atoi which converts a string to an integer.
The function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.
\n\nThe string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.
\n\nIf the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.
\n\nIf no valid conversion could be performed, a zero value is returned.
\n\nNote:
\n\n' ' is considered as whitespace character.Example 1:
\n\n\nInput: "42"\nOutput: 42\n\n\n
Example 2:
\n\n\nInput: " -42"\nOutput: -42\nExplanation: The first non-whitespace character is '-', which is the minus sign.\n Then take as many numerical digits as possible, which gets 42.\n\n\n
Example 3:
\n\n\nInput: "4193 with words"\nOutput: 4193\nExplanation: Conversion stops at digit '3' as the next character is not a numerical digit.\n\n\n
Example 4:
\n\n\nInput: "words and 987"\nOutput: 0\nExplanation: The first non-whitespace character is 'w', which is not a numerical \n digit or a +/- sign. Therefore no valid conversion could be performed.\n\n
Example 5:
\n\n\nInput: "-91283472332"\nOutput: -2147483648\nExplanation: The number "-91283472332" is out of the range of a 32-bit signed integer.\n Thefore INT_MIN (−231) is returned.\n
\u5b9e\u73b0 atoi\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6574\u6570\u3002
\u5728\u627e\u5230\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u4e4b\u524d\uff0c\u9700\u8981\u79fb\u9664\u6389\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u5b57\u7b26\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f\u6b63\u53f7\u6216\u8d1f\u53f7\uff0c\u9009\u53d6\u8be5\u7b26\u53f7\uff0c\u5e76\u5c06\u5176\u4e0e\u540e\u9762\u5c3d\u53ef\u80fd\u591a\u7684\u8fde\u7eed\u7684\u6570\u5b57\u7ec4\u5408\u8d77\u6765\uff0c\u8fd9\u90e8\u5206\u5b57\u7b26\u5373\u4e3a\u6574\u6570\u7684\u503c\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f\u6570\u5b57\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u4e0e\u4e4b\u540e\u8fde\u7eed\u7684\u6570\u5b57\u5b57\u7b26\u7ec4\u5408\u8d77\u6765\uff0c\u5f62\u6210\u6574\u6570\u3002
\n\n\u5b57\u7b26\u4e32\u53ef\u4ee5\u5728\u5f62\u6210\u6574\u6570\u7684\u5b57\u7b26\u540e\u9762\u5305\u62ec\u591a\u4f59\u7684\u5b57\u7b26\uff0c\u8fd9\u4e9b\u5b57\u7b26\u53ef\u4ee5\u88ab\u5ffd\u7565\uff0c\u5b83\u4eec\u5bf9\u4e8e\u51fd\u6570\u6ca1\u6709\u5f71\u54cd\u3002
\n\n\u5f53\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u5e8f\u5217\u4e0d\u662f\u4e2a\u6709\u6548\u7684\u6574\u6570\uff1b\u6216\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff1b\u6216\u5b57\u7b26\u4e32\u4ec5\u5305\u542b\u7a7a\u767d\u5b57\u7b26\u65f6\uff0c\u5219\u4e0d\u8fdb\u884c\u8f6c\u6362\u3002
\n\n\u82e5\u51fd\u6570\u4e0d\u80fd\u6267\u884c\u6709\u6548\u7684\u8f6c\u6362\uff0c\u8fd4\u56de 0\u3002
\n\n\u8bf4\u660e\uff1a
\n\n\u5047\u8bbe\u6211\u4eec\u7684\u73af\u5883\u53ea\u80fd\u5b58\u50a8 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5176\u6570\u503c\u8303\u56f4\u662f [−231, 231 − 1]\u3002\u5982\u679c\u6570\u503c\u8d85\u8fc7\u53ef\u8868\u793a\u7684\u8303\u56f4\uff0c\u5219\u8fd4\u56de INT_MAX (231 − 1) \u6216 INT_MIN (−231) \u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: "42"\n\u8f93\u51fa: 42\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: " -42"\n\u8f93\u51fa: -42\n\u89e3\u91ca: \u7b2c\u4e00\u4e2a\u975e\u7a7a\u767d\u5b57\u7b26\u4e3a '-', \u5b83\u662f\u4e00\u4e2a\u8d1f\u53f7\u3002\n \u6211\u4eec\u5c3d\u53ef\u80fd\u5c06\u8d1f\u53f7\u4e0e\u540e\u9762\u6240\u6709\u8fde\u7eed\u51fa\u73b0\u7684\u6570\u5b57\u7ec4\u5408\u8d77\u6765\uff0c\u6700\u540e\u5f97\u5230 -42 \u3002\n\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: "4193 with words"\n\u8f93\u51fa: 4193\n\u89e3\u91ca: \u8f6c\u6362\u622a\u6b62\u4e8e\u6570\u5b57 '3' \uff0c\u56e0\u4e3a\u5b83\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u4e0d\u4e3a\u6570\u5b57\u3002\n\n\n
\u793a\u4f8b 4:
\n\n\u8f93\u5165: "words and 987"\n\u8f93\u51fa: 0\n\u89e3\u91ca: \u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f 'w', \u4f46\u5b83\u4e0d\u662f\u6570\u5b57\u6216\u6b63\u3001\u8d1f\u53f7\u3002\n \u56e0\u6b64\u65e0\u6cd5\u6267\u884c\u6709\u6548\u7684\u8f6c\u6362\u3002\n\n
\u793a\u4f8b 5:
\n\n\u8f93\u5165: "-91283472332"\n\u8f93\u51fa: -2147483648\n\u89e3\u91ca: \u6570\u5b57 "-91283472332" \u8d85\u8fc7 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u8303\u56f4\u3002 \n \u56e0\u6b64\u8fd4\u56de INT_MIN (−231) \u3002\n\n
\u5b9e\u73b0 atoi\uff0c\u5c06\u5b57\u7b26\u4e32\u8f6c\u4e3a\u6574\u6570\u3002
\u5728\u627e\u5230\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u4e4b\u524d\uff0c\u9700\u8981\u79fb\u9664\u6389\u5b57\u7b26\u4e32\u4e2d\u7684\u7a7a\u683c\u5b57\u7b26\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f\u6b63\u53f7\u6216\u8d1f\u53f7\uff0c\u9009\u53d6\u8be5\u7b26\u53f7\uff0c\u5e76\u5c06\u5176\u4e0e\u540e\u9762\u5c3d\u53ef\u80fd\u591a\u7684\u8fde\u7eed\u7684\u6570\u5b57\u7ec4\u5408\u8d77\u6765\uff0c\u8fd9\u90e8\u5206\u5b57\u7b26\u5373\u4e3a\u6574\u6570\u7684\u503c\u3002\u5982\u679c\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f\u6570\u5b57\uff0c\u5219\u76f4\u63a5\u5c06\u5176\u4e0e\u4e4b\u540e\u8fde\u7eed\u7684\u6570\u5b57\u5b57\u7b26\u7ec4\u5408\u8d77\u6765\uff0c\u5f62\u6210\u6574\u6570\u3002
\n\n\u5b57\u7b26\u4e32\u53ef\u4ee5\u5728\u5f62\u6210\u6574\u6570\u7684\u5b57\u7b26\u540e\u9762\u5305\u62ec\u591a\u4f59\u7684\u5b57\u7b26\uff0c\u8fd9\u4e9b\u5b57\u7b26\u53ef\u4ee5\u88ab\u5ffd\u7565\uff0c\u5b83\u4eec\u5bf9\u4e8e\u51fd\u6570\u6ca1\u6709\u5f71\u54cd\u3002
\n\n\u5f53\u5b57\u7b26\u4e32\u4e2d\u7684\u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u5e8f\u5217\u4e0d\u662f\u4e2a\u6709\u6548\u7684\u6574\u6570\uff1b\u6216\u5b57\u7b26\u4e32\u4e3a\u7a7a\uff1b\u6216\u5b57\u7b26\u4e32\u4ec5\u5305\u542b\u7a7a\u767d\u5b57\u7b26\u65f6\uff0c\u5219\u4e0d\u8fdb\u884c\u8f6c\u6362\u3002
\n\n\u82e5\u51fd\u6570\u4e0d\u80fd\u6267\u884c\u6709\u6548\u7684\u8f6c\u6362\uff0c\u8fd4\u56de 0\u3002
\n\n\u8bf4\u660e\uff1a
\n\n\u5047\u8bbe\u6211\u4eec\u7684\u73af\u5883\u53ea\u80fd\u5b58\u50a8 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\uff0c\u5176\u6570\u503c\u8303\u56f4\u662f [−231, 231 − 1]\u3002\u5982\u679c\u6570\u503c\u8d85\u8fc7\u53ef\u8868\u793a\u7684\u8303\u56f4\uff0c\u5219\u8fd4\u56de INT_MAX (231 − 1) \u6216 INT_MIN (−231) \u3002
\n\n\u793a\u4f8b 1:
\n\n\u8f93\u5165: "42"\n\u8f93\u51fa: 42\n\n\n
\u793a\u4f8b 2:
\n\n\u8f93\u5165: " -42"\n\u8f93\u51fa: -42\n\u89e3\u91ca: \u7b2c\u4e00\u4e2a\u975e\u7a7a\u767d\u5b57\u7b26\u4e3a '-', \u5b83\u662f\u4e00\u4e2a\u8d1f\u53f7\u3002\n \u6211\u4eec\u5c3d\u53ef\u80fd\u5c06\u8d1f\u53f7\u4e0e\u540e\u9762\u6240\u6709\u8fde\u7eed\u51fa\u73b0\u7684\u6570\u5b57\u7ec4\u5408\u8d77\u6765\uff0c\u6700\u540e\u5f97\u5230 -42 \u3002\n\n\n
\u793a\u4f8b 3:
\n\n\u8f93\u5165: "4193 with words"\n\u8f93\u51fa: 4193\n\u89e3\u91ca: \u8f6c\u6362\u622a\u6b62\u4e8e\u6570\u5b57 '3' \uff0c\u56e0\u4e3a\u5b83\u7684\u4e0b\u4e00\u4e2a\u5b57\u7b26\u4e0d\u4e3a\u6570\u5b57\u3002\n\n\n
\u793a\u4f8b 4:
\n\n\u8f93\u5165: "words and 987"\n\u8f93\u51fa: 0\n\u89e3\u91ca: \u7b2c\u4e00\u4e2a\u975e\u7a7a\u5b57\u7b26\u662f 'w', \u4f46\u5b83\u4e0d\u662f\u6570\u5b57\u6216\u6b63\u3001\u8d1f\u53f7\u3002\n \u56e0\u6b64\u65e0\u6cd5\u6267\u884c\u6709\u6548\u7684\u8f6c\u6362\u3002\n\n
\u793a\u4f8b 5:
\n\n\u8f93\u5165: "-91283472332"\n\u8f93\u51fa: -2147483648\n\u89e3\u91ca: \u6570\u5b57 "-91283472332" \u8d85\u8fc7 32 \u4f4d\u6709\u7b26\u53f7\u6574\u6570\u8303\u56f4\u3002 \n \u56e0\u6b64\u8fd4\u56de INT_MIN (−231) \u3002\n\n*/\n\n\n/**\n * @param {string} str\n * @return {number}\n */\nvar myAtoi = function(str) {\n \n};"} {"doc_id": "60395b6ae2ba75232e277843999e43c7", "text": "// Declare a fuction\n\nfunction sayHello(){\n console.log(\"Hello\");\n}\n\n// Call or invoke a fuction\n\nsayHello();\nsayHello();\nsayHello();\n\n//Declaring a function with variable\n\nfunction sayGoodbye(name){\n console.log(sayGoodbye +\"name\");\n}\nsayGoodbye(\"Ash\");\nsayGoodbye(\"Raj\");\nsayGoodbye(\"Syamil\");\n\n\n// fuction with 2 parameters with results\nfunction sum(a,b){\n return a+b;\n \n}\nconsole.log(sum(2,3));\nconsole.log(sum(4,8));\n\n\n\n// function with result...( example of abstration if you need to use later )\nfunction checkEven (number){\n if (number %2 == 0){\n return true;\n } else {\n return false;\n }\n}\nconsole.log(checkEven(4));\n\n//function with multiple results\n\nfunction areaVolume(width,length,height){\n return [ width * length, width*length*height];\n}\nconsole.log (areaVolume(3,4,5));\n\n\n\n\n\n//Arrays\n// A collection of items..\n// I create an array of numbers, which is called scores..\nvar scores = [50,80,90,60,70];\n\n//how many items are there in your array\nconsole.log(scores.length);\n\n//retrieving item number x....\n\n// an array start with an idex 0... to retrieve the item, you will do scores(index)\n\n\nconsole.log(scores[0]);\n\n//go thru the array, retrieve the value from the array\n//show it in the console...\nfor (var i=0; i< scores.length; i++){\n console.log(scores[i]);\n}\n\n//add an item at the end of the array\nscores.push(50);\nconsole.log(scores);\n\n//remove the last item from an array\nscores.pop();\nconsole.log(scores);\n\nscores.splice(1,1);\nconsole.log(scores);\n\n\n\n\n\n\n// exercise 1\nvar numberArray = [3,5,1,9,6,4,2];\n\nfunction minMaxAvg(arr){\n var average = 0;\n var sum =0 ;\n // assume the first one is the smallest one\n var min = arr[0];\n var max = arr[0];\n for ( var i=0; i
\nGiven a string and a string dictionary, find the longest string in the dictionary that can be formed by deleting some characters of the given string. If there are more than one possible results, return the longest word with the smallest lexicographical order. If there is no possible result, return the empty string.\n
\nExample 1:
\n
\nInput:\ns = \"abpcplea\", d = [\"ale\",\"apple\",\"monkey\",\"plea\"]\n\nOutput: \n\"apple\"\n\n\n\n\n
Example 2:
\n
\nInput:\ns = \"abpcplea\", d = [\"a\",\"b\",\"c\"]\n\nOutput: \n\"a\"\n\n\n\n
Note:
\n
\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u5178\uff0c\u627e\u5230\u5b57\u5178\u91cc\u9762\u6700\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u5220\u9664\u7ed9\u5b9a\u5b57\u7b26\u4e32\u7684\u67d0\u4e9b\u5b57\u7b26\u6765\u5f97\u5230\u3002\u5982\u679c\u7b54\u6848\u4e0d\u6b62\u4e00\u4e2a\uff0c\u8fd4\u56de\u957f\u5ea6\u6700\u957f\u4e14\u5b57\u5178\u987a\u5e8f\u6700\u5c0f\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u7b54\u6848\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165:\ns = "abpcplea", d = ["ale","apple","monkey","plea"]\n\n\u8f93\u51fa: \n"apple"\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165:\ns = "abpcplea", d = ["a","b","c"]\n\n\u8f93\u51fa: \n"a"\n\n\n
\u8bf4\u660e:
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32\u548c\u4e00\u4e2a\u5b57\u7b26\u4e32\u5b57\u5178\uff0c\u627e\u5230\u5b57\u5178\u91cc\u9762\u6700\u957f\u7684\u5b57\u7b26\u4e32\uff0c\u8be5\u5b57\u7b26\u4e32\u53ef\u4ee5\u901a\u8fc7\u5220\u9664\u7ed9\u5b9a\u5b57\u7b26\u4e32\u7684\u67d0\u4e9b\u5b57\u7b26\u6765\u5f97\u5230\u3002\u5982\u679c\u7b54\u6848\u4e0d\u6b62\u4e00\u4e2a\uff0c\u8fd4\u56de\u957f\u5ea6\u6700\u957f\u4e14\u5b57\u5178\u987a\u5e8f\u6700\u5c0f\u7684\u5b57\u7b26\u4e32\u3002\u5982\u679c\u7b54\u6848\u4e0d\u5b58\u5728\uff0c\u5219\u8fd4\u56de\u7a7a\u5b57\u7b26\u4e32\u3002
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165:\ns = "abpcplea", d = ["ale","apple","monkey","plea"]\n\n\u8f93\u51fa: \n"apple"\n\n\n
\u793a\u4f8b 2:
\n\n\n\u8f93\u5165:\ns = "abpcplea", d = ["a","b","c"]\n\n\u8f93\u51fa: \n"a"\n\n\n
\u8bf4\u660e:
\n\nGiven an array of characters, compress it in-place.
\n\nThe length after compression must always be smaller than or equal to the original array.
\n\nEvery element of the array should be a character (not int) of length 1.
\n \nAfter you are done modifying the input array in-place, return the new length of the array.
\n\nFollow up:
\nCould you solve it using only O(1) extra space?\n
Example 1:
\n
\nInput:\n[\"a\",\"a\",\"b\",\"b\",\"c\",\"c\",\"c\"]\n\nOutput:\nReturn 6, and the first 6 characters of the input array should be: [\"a\",\"2\",\"b\",\"2\",\"c\",\"3\"]\n\nExplanation:\n\"aa\" is replaced by \"a2\". \"bb\" is replaced by \"b2\". \"ccc\" is replaced by \"c3\".\n\n\n\n
Example 2:
\n
\nInput:\n[\"a\"]\n\nOutput:\nReturn 1, and the first 1 characters of the input array should be: [\"a\"]\n\nExplanation:\nNothing is replaced.\n\n\n\n
Example 3:
\n
\nInput:\n[\"a\",\"b\",\"b\",\"b\",\"b\",\"b\",\"b\",\"b\",\"b\",\"b\",\"b\",\"b\",\"b\"]\n\nOutput:\nReturn 4, and the first 4 characters of the input array should be: [\"a\",\"b\",\"1\",\"2\"].\n\nExplanation:\nSince the character \"a\" does not repeat, it is not compressed. \"bbbbbbbbbbbb\" is replaced by \"b12\".\nNotice each digit has it's own entry in the array.\n\n\n\n
Note:
\n
[35, 126].1 <= len(chars) <= 1000.\u7ed9\u5b9a\u4e00\u7ec4\u5b57\u7b26\uff0c\u4f7f\u7528\u539f\u5730\u7b97\u6cd5\u5c06\u5176\u538b\u7f29\u3002
\n\n\u538b\u7f29\u540e\u7684\u957f\u5ea6\u5fc5\u987b\u59cb\u7ec8\u5c0f\u4e8e\u6216\u7b49\u4e8e\u539f\u6570\u7ec4\u957f\u5ea6\u3002
\n\n\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u8be5\u662f\u957f\u5ea6\u4e3a1 \u7684\u5b57\u7b26\uff08\u4e0d\u662f int \u6574\u6570\u7c7b\u578b\uff09\u3002
\n\n\u5728\u5b8c\u6210\u539f\u5730\u4fee\u6539\u8f93\u5165\u6570\u7ec4\u540e\uff0c\u8fd4\u56de\u6570\u7ec4\u7684\u65b0\u957f\u5ea6\u3002
\n\n\n\n
\u8fdb\u9636\uff1a
\n\u4f60\u80fd\u5426\u4ec5\u4f7f\u7528O(1) \u7a7a\u95f4\u89e3\u51b3\u95ee\u9898\uff1f
\n\n
\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1a\n["a","a","b","b","c","c","c"]\n\n\u8f93\u51fa\uff1a\n\u8fd4\u56de6\uff0c\u8f93\u5165\u6570\u7ec4\u7684\u524d6\u4e2a\u5b57\u7b26\u5e94\u8be5\u662f\uff1a["a","2","b","2","c","3"]\n\n\u8bf4\u660e\uff1a\n"aa"\u88ab"a2"\u66ff\u4ee3\u3002"bb"\u88ab"b2"\u66ff\u4ee3\u3002"ccc"\u88ab"c3"\u66ff\u4ee3\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1a\n["a"]\n\n\u8f93\u51fa\uff1a\n\u8fd4\u56de1\uff0c\u8f93\u5165\u6570\u7ec4\u7684\u524d1\u4e2a\u5b57\u7b26\u5e94\u8be5\u662f\uff1a["a"]\n\n\u8bf4\u660e\uff1a\n\u6ca1\u6709\u4efb\u4f55\u5b57\u7b26\u4e32\u88ab\u66ff\u4ee3\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1a\n["a","b","b","b","b","b","b","b","b","b","b","b","b"]\n\n\u8f93\u51fa\uff1a\n\u8fd4\u56de4\uff0c\u8f93\u5165\u6570\u7ec4\u7684\u524d4\u4e2a\u5b57\u7b26\u5e94\u8be5\u662f\uff1a["a","b","1","2"]\u3002\n\n\u8bf4\u660e\uff1a\n\u7531\u4e8e\u5b57\u7b26"a"\u4e0d\u91cd\u590d\uff0c\u6240\u4ee5\u4e0d\u4f1a\u88ab\u538b\u7f29\u3002"bbbbbbbbbbbb"\u88ab“b12”\u66ff\u4ee3\u3002\n\u6ce8\u610f\u6bcf\u4e2a\u6570\u5b57\u5728\u6570\u7ec4\u4e2d\u90fd\u6709\u5b83\u81ea\u5df1\u7684\u4f4d\u7f6e\u3002\n\n\n
\u6ce8\u610f\uff1a
\n\n[35, 126]\u533a\u95f4\u5185\u30021 <= len(chars) <= 1000\u3002\u7ed9\u5b9a\u4e00\u7ec4\u5b57\u7b26\uff0c\u4f7f\u7528\u539f\u5730\u7b97\u6cd5\u5c06\u5176\u538b\u7f29\u3002
\n\n\u538b\u7f29\u540e\u7684\u957f\u5ea6\u5fc5\u987b\u59cb\u7ec8\u5c0f\u4e8e\u6216\u7b49\u4e8e\u539f\u6570\u7ec4\u957f\u5ea6\u3002
\n\n\u6570\u7ec4\u7684\u6bcf\u4e2a\u5143\u7d20\u5e94\u8be5\u662f\u957f\u5ea6\u4e3a1 \u7684\u5b57\u7b26\uff08\u4e0d\u662f int \u6574\u6570\u7c7b\u578b\uff09\u3002
\n\n\u5728\u5b8c\u6210\u539f\u5730\u4fee\u6539\u8f93\u5165\u6570\u7ec4\u540e\uff0c\u8fd4\u56de\u6570\u7ec4\u7684\u65b0\u957f\u5ea6\u3002
\n\n\n\n
\u8fdb\u9636\uff1a
\n\u4f60\u80fd\u5426\u4ec5\u4f7f\u7528O(1) \u7a7a\u95f4\u89e3\u51b3\u95ee\u9898\uff1f
\n\n
\u793a\u4f8b 1\uff1a
\n\n\n\u8f93\u5165\uff1a\n["a","a","b","b","c","c","c"]\n\n\u8f93\u51fa\uff1a\n\u8fd4\u56de6\uff0c\u8f93\u5165\u6570\u7ec4\u7684\u524d6\u4e2a\u5b57\u7b26\u5e94\u8be5\u662f\uff1a["a","2","b","2","c","3"]\n\n\u8bf4\u660e\uff1a\n"aa"\u88ab"a2"\u66ff\u4ee3\u3002"bb"\u88ab"b2"\u66ff\u4ee3\u3002"ccc"\u88ab"c3"\u66ff\u4ee3\u3002\n\n\n
\u793a\u4f8b 2\uff1a
\n\n\n\u8f93\u5165\uff1a\n["a"]\n\n\u8f93\u51fa\uff1a\n\u8fd4\u56de1\uff0c\u8f93\u5165\u6570\u7ec4\u7684\u524d1\u4e2a\u5b57\u7b26\u5e94\u8be5\u662f\uff1a["a"]\n\n\u8bf4\u660e\uff1a\n\u6ca1\u6709\u4efb\u4f55\u5b57\u7b26\u4e32\u88ab\u66ff\u4ee3\u3002\n\n\n
\u793a\u4f8b 3\uff1a
\n\n\n\u8f93\u5165\uff1a\n["a","b","b","b","b","b","b","b","b","b","b","b","b"]\n\n\u8f93\u51fa\uff1a\n\u8fd4\u56de4\uff0c\u8f93\u5165\u6570\u7ec4\u7684\u524d4\u4e2a\u5b57\u7b26\u5e94\u8be5\u662f\uff1a["a","b","1","2"]\u3002\n\n\u8bf4\u660e\uff1a\n\u7531\u4e8e\u5b57\u7b26"a"\u4e0d\u91cd\u590d\uff0c\u6240\u4ee5\u4e0d\u4f1a\u88ab\u538b\u7f29\u3002"bbbbbbbbbbbb"\u88ab“b12”\u66ff\u4ee3\u3002\n\u6ce8\u610f\u6bcf\u4e2a\u6570\u5b57\u5728\u6570\u7ec4\u4e2d\u90fd\u6709\u5b83\u81ea\u5df1\u7684\u4f4d\u7f6e\u3002\n\n\n
\u6ce8\u610f\uff1a
\n\n[35, 126]\u533a\u95f4\u5185\u30021 <= len(chars) <= 1000\u3002S and T are strings composed of lowercase letters. In S, no letter occurs more than once.
S was sorted in some custom order previously. We want to permute the characters of T so that they match the order that S was sorted. More specifically, if x occurs before y in S, then x should occur before y in the returned string.
Return any permutation of T (as a string) that satisfies this property.
\nExample :\nInput: \nS = "cba"\nT = "abcd"\nOutput: "cbad"\nExplanation: \n"a", "b", "c" appear in S, so the order of "a", "b", "c" should be "c", "b", and "a". \nSince "d" does not appear in S, it can be at any position in T. "dcba", "cdba", "cbda" are also valid outputs.\n\n\n
\n\n
Note:
\n\nS has length at most 26, and no character is repeated in S.T has length at most 200.S and T consist of lowercase letters only.\u5b57\u7b26\u4e32S\u548c T \u53ea\u5305\u542b\u5c0f\u5199\u5b57\u7b26\u3002\u5728S\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u53ea\u4f1a\u51fa\u73b0\u4e00\u6b21\u3002
S \u5df2\u7ecf\u6839\u636e\u67d0\u79cd\u89c4\u5219\u8fdb\u884c\u4e86\u6392\u5e8f\u3002\u6211\u4eec\u8981\u6839\u636eS\u4e2d\u7684\u5b57\u7b26\u987a\u5e8f\u5bf9T\u8fdb\u884c\u6392\u5e8f\u3002\u66f4\u5177\u4f53\u5730\u8bf4\uff0c\u5982\u679cS\u4e2dx\u5728y\u4e4b\u524d\u51fa\u73b0\uff0c\u90a3\u4e48\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4e2dx\u4e5f\u5e94\u51fa\u73b0\u5728y\u4e4b\u524d\u3002
\u8fd4\u56de\u4efb\u610f\u4e00\u79cd\u7b26\u5408\u6761\u4ef6\u7684\u5b57\u7b26\u4e32T\u3002
\n\u793a\u4f8b:\n\u8f93\u5165:\nS = "cba"\nT = "abcd"\n\u8f93\u51fa: "cbad"\n\u89e3\u91ca: \nS\u4e2d\u51fa\u73b0\u4e86\u5b57\u7b26 "a", "b", "c", \u6240\u4ee5 "a", "b", "c" \u7684\u987a\u5e8f\u5e94\u8be5\u662f "c", "b", "a". \n\u7531\u4e8e "d" \u6ca1\u6709\u5728S\u4e2d\u51fa\u73b0, \u5b83\u53ef\u4ee5\u653e\u5728T\u7684\u4efb\u610f\u4f4d\u7f6e. "dcba", "cdba", "cbda" \u90fd\u662f\u5408\u6cd5\u7684\u8f93\u51fa\u3002\n\n\n
\u6ce8\u610f:
\n\nS\u7684\u6700\u5927\u957f\u5ea6\u4e3a26\uff0c\u5176\u4e2d\u6ca1\u6709\u91cd\u590d\u7684\u5b57\u7b26\u3002T\u7684\u6700\u5927\u957f\u5ea6\u4e3a200\u3002S\u548cT\u53ea\u5305\u542b\u5c0f\u5199\u5b57\u7b26\u3002\u5b57\u7b26\u4e32S\u548c T \u53ea\u5305\u542b\u5c0f\u5199\u5b57\u7b26\u3002\u5728S\u4e2d\uff0c\u6240\u6709\u5b57\u7b26\u53ea\u4f1a\u51fa\u73b0\u4e00\u6b21\u3002
S \u5df2\u7ecf\u6839\u636e\u67d0\u79cd\u89c4\u5219\u8fdb\u884c\u4e86\u6392\u5e8f\u3002\u6211\u4eec\u8981\u6839\u636eS\u4e2d\u7684\u5b57\u7b26\u987a\u5e8f\u5bf9T\u8fdb\u884c\u6392\u5e8f\u3002\u66f4\u5177\u4f53\u5730\u8bf4\uff0c\u5982\u679cS\u4e2dx\u5728y\u4e4b\u524d\u51fa\u73b0\uff0c\u90a3\u4e48\u8fd4\u56de\u7684\u5b57\u7b26\u4e32\u4e2dx\u4e5f\u5e94\u51fa\u73b0\u5728y\u4e4b\u524d\u3002
\u8fd4\u56de\u4efb\u610f\u4e00\u79cd\u7b26\u5408\u6761\u4ef6\u7684\u5b57\u7b26\u4e32T\u3002
\n\u793a\u4f8b:\n\u8f93\u5165:\nS = "cba"\nT = "abcd"\n\u8f93\u51fa: "cbad"\n\u89e3\u91ca: \nS\u4e2d\u51fa\u73b0\u4e86\u5b57\u7b26 "a", "b", "c", \u6240\u4ee5 "a", "b", "c" \u7684\u987a\u5e8f\u5e94\u8be5\u662f "c", "b", "a". \n\u7531\u4e8e "d" \u6ca1\u6709\u5728S\u4e2d\u51fa\u73b0, \u5b83\u53ef\u4ee5\u653e\u5728T\u7684\u4efb\u610f\u4f4d\u7f6e. "dcba", "cdba", "cbda" \u90fd\u662f\u5408\u6cd5\u7684\u8f93\u51fa\u3002\n\n\n
\u6ce8\u610f:
\n\nS\u7684\u6700\u5927\u957f\u5ea6\u4e3a26\uff0c\u5176\u4e2d\u6ca1\u6709\u91cd\u590d\u7684\u5b57\u7b26\u3002T\u7684\u6700\u5927\u957f\u5ea6\u4e3a200\u3002S\u548cT\u53ea\u5305\u542b\u5c0f\u5199\u5b57\u7b26\u3002\nSpecial binary strings are binary strings with the following two properties:\n
\n
\nGiven a special string S, a move consists of choosing two consecutive, non-empty, special substrings of S, and swapping them. (Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.)\n
\nAt the end of any number of moves, what is the lexicographically largest resulting string possible?\n
\n\nExample 1:
\n
\nInput: S = \"11011000\"\nOutput: \"11100100\"\nExplanation:\nThe strings \"10\" [occuring at S[1]] and \"1100\" [at S[3]] are swapped.\nThis is the lexicographically largest string possible after some number of swaps.\n\n\n\n
Note:
S has length at most 50.S is guaranteed to be a special binary string as defined above.\u7279\u6b8a\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u662f\u5177\u6709\u4ee5\u4e0b\u4e24\u4e2a\u6027\u8d28\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1a
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u7279\u6b8a\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217 S\uff0c\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u3002\u5b9a\u4e49\u4e00\u4e2a\u64cd\u4f5c \u4e3a\u9996\u5148\u9009\u62e9 S \u7684\u4e24\u4e2a\u8fde\u7eed\u4e14\u975e\u7a7a\u7684\u7279\u6b8a\u7684\u5b50\u4e32\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\uff08\u4e24\u4e2a\u5b50\u4e32\u4e3a\u8fde\u7eed\u7684\u5f53\u4e14\u4ec5\u5f53\u7b2c\u4e00\u4e2a\u5b50\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u6070\u597d\u4e3a\u7b2c\u4e8c\u4e2a\u5b50\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u524d\u4e00\u4e2a\u5b57\u7b26\u3002)
\u5728\u4efb\u610f\u6b21\u6570\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u4ea4\u6362\u540e\u7684\u5b57\u7b26\u4e32\u6309\u7167\u5b57\u5178\u5e8f\u6392\u5217\u7684\u6700\u5927\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: S = "11011000"\n\u8f93\u51fa: "11100100"\n\u89e3\u91ca:\n\u5c06\u5b50\u4e32 "10" \uff08\u5728S[1]\u51fa\u73b0\uff09 \u548c "1100" \uff08\u5728S[3]\u51fa\u73b0\uff09\u8fdb\u884c\u4ea4\u6362\u3002\n\u8fd9\u662f\u5728\u8fdb\u884c\u82e5\u5e72\u6b21\u64cd\u4f5c\u540e\u6309\u5b57\u5178\u5e8f\u6392\u5217\u6700\u5927\u7684\u7ed3\u679c\u3002\n\n\n
\u8bf4\u660e:
\n\nS \u7684\u957f\u5ea6\u4e0d\u8d85\u8fc7 50\u3002S \u4fdd\u8bc1\u4e3a\u4e00\u4e2a\u6ee1\u8db3\u4e0a\u8ff0\u5b9a\u4e49\u7684\u7279\u6b8a \u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002\u7279\u6b8a\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u662f\u5177\u6709\u4ee5\u4e0b\u4e24\u4e2a\u6027\u8d28\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\uff1a
\n\n\u7ed9\u5b9a\u4e00\u4e2a\u7279\u6b8a\u7684\u4e8c\u8fdb\u5236\u5e8f\u5217 S\uff0c\u4ee5\u5b57\u7b26\u4e32\u5f62\u5f0f\u8868\u793a\u3002\u5b9a\u4e49\u4e00\u4e2a\u64cd\u4f5c \u4e3a\u9996\u5148\u9009\u62e9 S \u7684\u4e24\u4e2a\u8fde\u7eed\u4e14\u975e\u7a7a\u7684\u7279\u6b8a\u7684\u5b50\u4e32\uff0c\u7136\u540e\u5c06\u5b83\u4eec\u4ea4\u6362\u3002\uff08\u4e24\u4e2a\u5b50\u4e32\u4e3a\u8fde\u7eed\u7684\u5f53\u4e14\u4ec5\u5f53\u7b2c\u4e00\u4e2a\u5b50\u4e32\u7684\u6700\u540e\u4e00\u4e2a\u5b57\u7b26\u6070\u597d\u4e3a\u7b2c\u4e8c\u4e2a\u5b50\u4e32\u7684\u7b2c\u4e00\u4e2a\u5b57\u7b26\u7684\u524d\u4e00\u4e2a\u5b57\u7b26\u3002)
\u5728\u4efb\u610f\u6b21\u6570\u7684\u64cd\u4f5c\u4e4b\u540e\uff0c\u4ea4\u6362\u540e\u7684\u5b57\u7b26\u4e32\u6309\u7167\u5b57\u5178\u5e8f\u6392\u5217\u7684\u6700\u5927\u7684\u7ed3\u679c\u662f\u4ec0\u4e48\uff1f
\n\n\u793a\u4f8b 1:
\n\n\n\u8f93\u5165: S = "11011000"\n\u8f93\u51fa: "11100100"\n\u89e3\u91ca:\n\u5c06\u5b50\u4e32 "10" \uff08\u5728S[1]\u51fa\u73b0\uff09 \u548c "1100" \uff08\u5728S[3]\u51fa\u73b0\uff09\u8fdb\u884c\u4ea4\u6362\u3002\n\u8fd9\u662f\u5728\u8fdb\u884c\u82e5\u5e72\u6b21\u64cd\u4f5c\u540e\u6309\u5b57\u5178\u5e8f\u6392\u5217\u6700\u5927\u7684\u7ed3\u679c\u3002\n\n\n
\u8bf4\u660e:
\n\nS \u7684\u957f\u5ea6\u4e0d\u8d85\u8fc7 50\u3002S \u4fdd\u8bc1\u4e3a\u4e00\u4e2a\u6ee1\u8db3\u4e0a\u8ff0\u5b9a\u4e49\u7684\u7279\u6b8a \u7684\u4e8c\u8fdb\u5236\u5e8f\u5217\u3002Given a string S, we can transform every letter individually to be lowercase or uppercase to create another string. Return a list of all possible strings we could create.
\n\n\nExamples:\nInput: S = "a1b2"\nOutput: ["a1b2", "a1B2", "A1b2", "A1B2"]\n\nInput: S = "3z4"\nOutput: ["3z4", "3Z4"]\n\nInput: S = "12345"\nOutput: ["12345"]\n\n\n
Note:
\n\nS will be a string with length at most 12.S will consist only of letters or digits.\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32S\uff0c\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32S\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u8f6c\u53d8\u5927\u5c0f\u5199\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u96c6\u5408\u3002
\n\u793a\u4f8b:\n\u8f93\u5165: S = "a1b2"\n\u8f93\u51fa: ["a1b2", "a1B2", "A1b2", "A1B2"]\n\n\u8f93\u5165: S = "3z4"\n\u8f93\u51fa: ["3z4", "3Z4"]\n\n\u8f93\u5165: S = "12345"\n\u8f93\u51fa: ["12345"]\n\n\n
\u6ce8\u610f\uff1a
\n\nS \u7684\u957f\u5ea6\u4e0d\u8d85\u8fc712\u3002S \u4ec5\u7531\u6570\u5b57\u548c\u5b57\u6bcd\u7ec4\u6210\u3002\u7ed9\u5b9a\u4e00\u4e2a\u5b57\u7b26\u4e32S\uff0c\u901a\u8fc7\u5c06\u5b57\u7b26\u4e32S\u4e2d\u7684\u6bcf\u4e2a\u5b57\u6bcd\u8f6c\u53d8\u5927\u5c0f\u5199\uff0c\u6211\u4eec\u53ef\u4ee5\u83b7\u5f97\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u8fd4\u56de\u6240\u6709\u53ef\u80fd\u5f97\u5230\u7684\u5b57\u7b26\u4e32\u96c6\u5408\u3002
\n\u793a\u4f8b:\n\u8f93\u5165: S = "a1b2"\n\u8f93\u51fa: ["a1b2", "a1B2", "A1b2", "A1B2"]\n\n\u8f93\u5165: S = "3z4"\n\u8f93\u51fa: ["3z4", "3Z4"]\n\n\u8f93\u5165: S = "12345"\n\u8f93\u51fa: ["12345"]\n\n\n
\u6ce8\u610f\uff1a
\n\nS \u7684\u957f\u5ea6\u4e0d\u8d85\u8fc712\u3002S \u4ec5\u7531\u6570\u5b57\u548c\u5b57\u6bcd\u7ec4\u6210\u3002