{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CPP" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 728. Self Dividing Numbers\n", "\n", "Easy\n", "\n", "A self-dividing number is a number that is divisible by every digit it contains.\n", "\n", "For example, 128 is a self-dividing number because 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0.\n", "\n", "Also, a self-dividing number is not allowed to contain the digit zero.\n", "\n", "Given a lower and upper number bound, output a list of every possible self dividing number, including the bounds if possible.\n", "\n", "Example 1:\n", "\n", "Input: \n", "\n", " left = 1, right = 22\n", " Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22]\n", "\n", "Note:\n", "The boundaries of each input argument are 1 <= left <= right <= 10000." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 12, 15, 22, " ] } ], "source": [ "// 很慢因為 selfDividing 檢查了所有 digit。其實只要有一個不整除就可以踢掉這個數了\n", "// getDigits 裡用 to_string 和 stoi 在整數和字串間切換\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto getDigits = [](int num)\n", " {\n", " std::unordered_set digits;\n", " for(char& c : std::to_string(num))\n", " digits.insert(std::stoi(std::string(1, c)));\n", " return digits;\n", " };\n", " \n", " auto f = [=](int left, int right)\n", " {\n", " std::vector res;\n", " for (int n=left ; n<=right ; n++)\n", " {\n", " std::unordered_set digits = getDigits(n);\n", " bool selfDividing = true;\n", " if (digits.find(0) != digits.end()) \n", " selfDividing = false;\n", " else\n", " for (const int& digit : digits)\n", " selfDividing = (selfDividing && n%digit==0);\n", " if (selfDividing)\n", " res.push_back(n);\n", " }\n", " return res;\n", " };\n", " \n", " for (const int& n : f(1, 22))\n", " std::cout << n << \", \";\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1281. Subtract the Product and Sum of Digits of an Integer\n", "\n", "Easy\n", "\n", "Given an integer number n, return the difference between the product of its digits and the sum of its digits.\n", "\n", "Example 1:\n", "\n", " Input: n = 234\n", " Output: 15 \n", " Explanation: \n", " Product of digits = 2 * 3 * 4 = 24 \n", " Sum of digits = 2 + 3 + 4 = 9 \n", " Result = 24 - 9 = 15\n", "\n", "Constraints:\n", "\n", " 1 <= n <= 10^5" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-2\n" ] } ], "source": [ "#include \n", "#include \n", "\n", "{\n", " auto f = [](int n)\n", " {\n", " int sum = 0;\n", " int prod = 1;\n", " while (n>0)\n", " {\n", " int digit = n%10;\n", " sum = sum + digit;\n", " prod = prod*digit;\n", " n = n/10;\n", " }\n", " return prod-sum;\n", " };\n", " \n", " std::cout << f(114) << std::endl;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1512. Number of Good Pairs\n", "\n", "Easy\n", "\n", "Given an array of integers nums. A pair (i,j) is called good if nums[i] == nums[j] and i < j. Return the number of good pairs.\n", "\n", "Example 1:\n", "\n", " Input: nums = [1,2,3,1,1,3]\n", " Output: 4\n", " Explanation: There are 4 good pairs (0,3), (0,4), (3,4), (2,5) 0-indexed. \n", "\n", "Constraints:\n", "\n", " 1 <= nums.length <= 100\n", " 1 <= nums[i] <= 100" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "#include \n", "#include \n", "{\n", " auto f = [](std::vector& nums)\n", " {\n", " int res = 0;\n", " \n", " for(size_t j=0 ; j nums = {1, 2, 3, 1, 1, 3};\n", " std::cout << f(nums) << std::endl;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1470. Shuffle the Array\n", "\n", "Easy\n", "\n", "Given the array nums consisting of 2n elements in the form [x1,x2,...,xn,y1,y2,...,yn], return the array in the form [x1,y1,x2,y2,...,xn,yn].\n", "\n", "Example 1:\n", "\n", " Input: nums = [2,5,1,3,4,7], n = 3\n", " Output: [2,3,5,4,1,7] \n", " Explanation: Since x1=2, x2=5, x3=1, y1=3, y2=4, y3=7 then the answer is [2,3,5,4,1,7].\n", "\n", "Constraints:\n", "\n", " 1 <= n <= 500\n", " nums.length == 2n\n", " 1 <= nums[i] <= 10^3" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2, 3, 5, 4, 1, 7, " ] } ], "source": [ "#include \n", "#include \n", "{\n", " auto f = [](std::vector& nums, int n)\n", " {\n", " std::vector res;\n", " \n", " for (size_t i=0 ; i nums = {2, 5, 1, 3, 4, 7};\n", " for(const int& n : f(nums, 3))\n", " std::cout << n << \", \";\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1480. Running Sum of 1d Array\n", "\n", "Easy\n", "\n", "Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]). Return the running sum of nums.\n", "\n", "Example 1:\n", "\n", " Input: nums = [1,2,3,4]\n", " Output: [1,3,6,10]\n", " Explanation: Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].\n", "\n", "Constraints:\n", "\n", " 1 <= nums.length <= 1000\n", " -10^6 <= nums[i] <= 10^6\n" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1, 3, 6, 10, " ] } ], "source": [ "// std::partial_sum 比 native 寫法慢一點\n", "\n", "#include \n", "#include \n", "#include \n", "{\n", " auto f = [](std::vector& nums)\n", " {\n", " std::partial_sum(nums.begin(), nums.end(), nums.begin());\n", " return nums;\n", " };\n", " std::vector nums = {1, 2, 3, 4};\n", " for (int n : f(nums))\n", " std::cout << n << \", \";\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1, 3, 6, 10, " ] } ], "source": [ "#include \n", "#include \n", "{\n", " auto f = [](std::vector& nums)\n", " {\n", " int sum = 0;\n", " for (int& n : nums)\n", " {\n", " int m = n;\n", " n += sum;\n", " sum += m;\n", " }\n", " return nums;\n", " };\n", " std::vector nums = {1, 2, 3, 4};\n", " for (int n : f(nums))\n", " std::cout << n << \", \";\n", "}\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 344. Reverse String\n", "\n", "Easy\n", "\n", "Write a function that reverses a string. The input string is given as an array of characters char[].\n", "\n", "Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.\n", "\n", "You may assume all the characters consist of printable ascii characters.\n", "\n", "\n", "Example 1:\n", "\n", " Input: [\"h\",\"e\",\"l\",\"l\",\"o\"]\n", " Output: [\"o\",\"l\",\"l\",\"e\",\"h\"]\n" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "olleh" ] } ], "source": [ "// std::reverse\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::vector& s)\n", " {\n", " std::reverse(s.begin(), s.end());\n", " };\n", " \n", " std::vector s = {'h', 'e', 'l', 'l', 'o'}; \n", " f(s);\n", " for (char c : s) \n", " std::cout << c;\n", "}" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "olleh" ] } ], "source": [ "// for & std::swap\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::vector& s)\n", " {\n", " for(int i=0, j=s.size()-1 ; i s = {'h', 'e', 'l', 'l', 'o'}; \n", " f(s);\n", " for (char c : s)\n", " std::cout << c;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 557. Reverse Words in a String III\n", "\n", "Easy\n", "\n", "Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.\n", "\n", "Example 1:\n", "\n", " Input: \"Let's take LeetCode contest\"\n", " Output: \"s'teL ekat edoCteeL tsetnoc\"\n", "\n", "Note: In the string, each word is separated by single space and there will not be any extra space in the string. \n", "\n", "把被空白格開的字一個一個倒過來" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "s'teL ekat edoCteeL tsetnoc\n" ] } ], "source": [ "#include \n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::string s)\n", " {\n", " if (s.size()==0)\n", " return s;\n", " else\n", " {\n", " s += \" \";\n", " size_t i = 0, j = 0;\n", " for (size_t k=0 ; k\n", "#include \n", "\n", "{\n", " auto f = [](std::string moves)\n", " {\n", " int ud = 0, lr = 0;\n", " for (char c : moves)\n", " {\n", " switch(c){\n", " case 'U': ud += 1; break;\n", " case 'D': ud -= 1; break;\n", " case 'L': lr -= 1; break;\n", " case 'R': lr += 1; break;\n", " }\n", " }\n", " return (ud==0 && lr==0);\n", " };\n", " \n", " std::cout << f(\"UD\") << std::endl;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1309. Decrypt String from Alphabet to Integer Mapping\n", "\n", "Easy\n", "\n", "Given a string s formed by digits ('0' - '9') and '#' . We want to map s to English lowercase characters as follows:\n", "\n", " Characters ('a' to 'i') are represented by ('1' to '9') respectively.\n", " Characters ('j' to 'z') are represented by ('10#' to '26#') respectively. \n", "\n", "Return the string formed after mapping.\n", "\n", "It's guaranteed that a unique mapping will always exist.\n", "\n", " \n", "\n", "Example 1:\n", "\n", " Input: s = \"10#11#12\"\n", " Output: \"jkab\"\n", " Explanation: \"j\" -> \"10#\" , \"k\" -> \"11#\" , \"a\" -> \"1\" , \"b\" -> \"2\".\n", "\n", "Constraints:\n", "\n", " 1 <= s.length <= 1000\n", " s[i] only contains digits letters ('0'-'9') and '#' letter.\n", " s will be valid string such that mapping is always possible." ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "jkab\n" ] } ], "source": [ "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::string s)\n", " {\n", " std::unordered_map map = {{\"1\", \"a\"}, {\"2\", \"b\"}, {\"3\", \"c\"}, {\"4\", \"d\"}, {\"5\", \"e\"}, {\"6\", \"f\"}, {\"7\", \"g\"}, {\"8\", \"h\"}, {\"9\", \"i\"}, {\"10#\", \"j\"}, {\"11#\", \"k\"}, {\"12#\", \"l\"}, {\"13#\", \"m\"}, {\"14#\", \"n\"}, {\"15#\", \"o\"}, {\"16#\", \"p\"}, {\"17#\", \"q\"}, {\"18#\", \"r\"}, {\"19#\", \"s\"}, {\"20#\", \"t\"}, {\"21#\", \"u\"}, {\"22#\", \"v\"}, {\"23#\", \"w\"}, {\"24#\", \"x\"}, {\"25#\", \"y\"}, {\"26#\", \"z\"}};\n", " std::string res;\n", " std::string::reverse_iterator it=s.rbegin();\n", " while(it!=s.rend())\n", " {\n", " if (*it=='#'){\n", " res = map[std::string({*(it+2), *(it+1), *it})] + res;\n", " it+=3;\n", " }else{\n", " res = map[std::string({*it})] + res;\n", " it+=1;\n", " }\n", " }\n", " return res;\n", " };\n", " std::cout << f(\"10#11#12\") << std::endl;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1436. Destination City\n", "\n", "Easy\n", "\n", "You are given the array paths, where paths[i] = [cityAi, cityBi] means there exists a direct path going from cityAi to cityBi. Return the destination city, that is, the city without any path outgoing to another city.\n", "\n", "It is guaranteed that the graph of paths forms a line without any loop, therefore, there will be exactly one destination city.\n", "\n", "Example 1:\n", "\n", " Input: paths = [[\"London\",\"New York\"],[\"New York\",\"Lima\"],[\"Lima\",\"Sao Paulo\"]]\n", " Output: \"Sao Paulo\" \n", " Explanation: Starting at \"London\" city you will reach \"Sao Paulo\" city which is the destination city. Your trip consist of: \"London\" -> \"New York\" -> \"Lima\" -> \"Sao Paulo\". \n", "\n", "Constraints:\n", "\n", " 1 <= paths.length <= 100\n", " paths[i].length == 2\n", " 1 <= cityAi.length, cityBi.length <= 10\n", " cityAi != cityBi\n", " All strings consist of lowercase and uppercase English letters and the space character." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Sao Paulo\n" ] } ], "source": [ "// 把 iput 看成一個 2-column table,找出現在右邊但不在左邊 column 的城市\n", "// std::set_difference 需要 sorted input 所以不能用。只能用 std::copy_if\n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::vector>& paths)\n", " {\n", " std::unordered_set a, b;\n", " std::vector diff;\n", " for (std::vector path : paths)\n", " {\n", " a.insert(path[0]);\n", " b.insert(path[1]);\n", " } \n", " std::copy_if(b.begin(), b.end(), std::back_inserter(diff), [&a] (std::string city) { return a.find(city) == a.end(); });\n", " \n", " return diff[0];\n", " };\n", " \n", " std::vector> paths = {{\"London\",\"New York\"}, {\"New York\",\"Lima\"}, {\"Lima\",\"Sao Paulo\"}};\n", " std::cout << f(paths) << std::endl;\n", "}\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 804. Unique Morse Code Words\n", "\n", "Easy\n", "\n", "International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes, as follows: \"a\" maps to \".-\", \"b\" maps to \"-...\", \"c\" maps to \"-.-.\", and so on.\n", "\n", "For convenience, the full table for the 26 letters of the English alphabet is given below:\n", "\n", "[\".-\",\"-...\",\"-.-.\",\"-..\",\".\",\"..-.\",\"--.\",\"....\",\"..\",\".---\",\"-.-\",\".-..\",\"--\",\"-.\",\"---\",\".--.\",\"--.-\",\".-.\",\"...\",\"-\",\"..-\",\"...-\",\".--\",\"-..-\",\"-.--\",\"--..\"]\n", "\n", "Now, given a list of words, each word can be written as a concatenation of the Morse code of each letter. For example, \"cab\" can be written as \"-.-..--...\", (which is the concatenation \"-.-.\" + \".-\" + \"-...\"). We'll call such a concatenation, the transformation of a word.\n", "\n", "Return the number of different transformations among all words we have.\n", "\n", "Example:\n", "\n", " Input: words = [\"gin\", \"zen\", \"gig\", \"msg\"]\n", " Output: 2\n", " Explanation: \n", " The transformation of each word is:\n", " \"gin\" -> \"--...-.\"\n", " \"zen\" -> \"--...-.\"\n", " \"gig\" -> \"--...--.\"\n", " \"msg\" -> \"--...--.\"\n", "\n", " There are 2 different transformations, \"--...-.\" and \"--...--.\".\n", "\n", "Note:\n", "\n", " The length of words will be at most 100.\n", " Each words[i] will have length in range [1, 12].\n", " words[i] will only consist of lowercase letters." ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2\n" ] } ], "source": [ "#include \n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::vector& words)\n", " {\n", " std::unordered_map morse = {{'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', \"--..\"}};\n", " std::unordered_set transformed;\n", " for (const std::string& word : words)\n", " {\n", " std::string code = \"\";\n", " for (const char& c : word)\n", " code += morse[c];\n", " \n", " transformed.insert(code);\n", " }\n", " return transformed.size();\n", " };\n", " \n", " std::vector words = {\"uirqyr\",\"ffqkc\",\"joq\",\"joq\",\"joq\"};\n", " std::cout << f(words) << std::endl;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 709. To Lower Case\n", "\n", "Easy\n", "\n", "Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase.\n", "\n", "Example 1:\n", "\n", " Input: \"Hello\"\n", " Output: \"hello\"" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hellow\n" ] } ], "source": [ "// std::for_each 和 std::tolower 寫法\n", "// 如果用 transform 就要寫 std::transform(str.begin(), str.end(), str.begin(), [](char c){ return std::tolower(c); });\n", "// for_each 和 transform 的差別就是 for_each 的 unary fnc 是 void,transform 的一定要 output 然後被填進第三個 argument 所指向的位址\n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", " \n", "{\n", " auto f = [](std::string str)\n", " {\n", "// std::transform(str.begin(), str.end(), str.begin(), std::tolower); // 不能這樣寫;為什麼一定要寫一個 lambda?\n", "// std::transform(str.begin(), str.end(), str.begin(), [](const char& c){ return std::tolower(c); }); // ok\n", " std::for_each(str.begin(), str.end(), [](char& c){ c = std::tolower(c); });\n", " \n", " return str;\n", " };\n", " std::cout << f(\"Hellow\") << std::endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hellow\n" ] } ], "source": [ "// unordered_map 寫法\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::string str)\n", " {\n", " std::unordered_map u = {{'A','a'}, {'B','b'}, {'C','c'}, {'D','d'}, {'E','e'}, \n", " {'F','f'}, {'G','g'}, {'H','h'}, {'I','i'}, {'J','j'}, {'K','k'}, {'L','l'}, {'M','m'}, \n", " {'N','n'}, {'O','o'}, {'P','p'}, {'Q','q'}, {'R','r'}, {'S','s'}, {'T','t'}, {'U','u'}, \n", " {'V','v'}, {'W','w'}, {'X','x'}, {'Y','y'}, {'Z','z'}};\n", " \n", " for (std::string::iterator it=str.begin() ; it!=str.end() ; it++)\n", " if (u.find(*it) != u.end()) // 在 c++20 可以直接寫 u.contains(*it) \n", " *it = u[*it];\n", " return str;\n", " };\n", " \n", " std::cout << f(\"Hellow\") << std::endl;\n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hellow\n" ] } ], "source": [ "// transform + unordered_map 寫法\n", "\n", "#include \n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::string str)\n", " {\n", " std::unordered_map lower = {{'A','a'}, {'B','b'}, {'C','c'}, {'D','d'}, \n", " {'E','e'}, {'F','f'}, {'G','g'}, {'H','h'}, {'I','i'}, {'J','j'}, {'K','k'}, \n", " {'L','l'}, {'M','m'}, {'N','n'}, {'O','o'}, {'P','p'}, {'Q','q'}, {'R','r'}, \n", " {'S','s'}, {'T','t'}, {'U','u'}, {'V','v'}, {'W','w'}, {'X','x'}, {'Y','y'}, \n", " {'Z','z'}, {'a','a'}, {'b','b'}, {'c','c'}, {'d','d'}, {'e','e'}, {'f','f'}, \n", " {'g','g'}, {'h','h'}, {'i','i'}, {'j','j'}, {'k','k'}, {'l','l'}, {'m','m'}, \n", " {'n','n'}, {'o','o'}, {'p','p'}, {'q','q'}, {'r','r'}, {'s','s'}, {'t','t'}, \n", " {'u','u'}, {'v','v'}, {'w','w'}, {'x','x'}, {'y','y'}, {'z','z'}};\n", " \n", " std::transform(str.begin(), str.end(), str.begin(), [&](char c){ return lower[c]; });\n", " return str;\n", " };\n", " \n", " std::cout << f(\"Hellow\") << std::endl;\n", "\n", "}" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hellow\n" ] } ], "source": [ "// 直接改 ACSII 寫法\n", "\n", "#include \n", "#include \n", "#include \n", "{\n", " auto f = [](std::string str)\n", " { \n", " for (std::string::iterator it=str.begin() ; it!=str.end() ; it++)\n", " if ( 64 < *it && *it < 91 )\n", " *it += 32;\n", " return str;\n", " };\n", " \n", " std::cout << f(\"Hellow\") << std::endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hellow\n" ] } ], "source": [ "// 用 transform 改 ACSII 寫法\n", "\n", "#include \n", "#include \n", "#include \n", "{\n", " auto f = [](std::string str)\n", " { \n", " std::transform(str.begin(), str.end(), str.begin(), \n", " [](char& c){ return (64 < (int)c && (int)c < 91) ? (char)((int)c + 32) : c;});\n", " return str;\n", " };\n", " \n", " std::cout << f(\"Hellow\") << std::endl;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1108. Defanging an IP Address\n", "\n", "Easy\n", "\n", "Given a valid (IPv4) IP address, return a defanged version of that IP address.\n", "\n", "A defanged IP address replaces every period \".\" with \"[.]\".\n", "\n", " \n", "\n", "Example 1:\n", "\n", " Input: address = \"1.1.1.1\"\n", " Output: \"1[.]1[.]1[.]1\"\n", "\n", "Constraints:\n", "\n", " The given address is a valid IPv4 address.\n", "\n", "把一串 IP 位址的 \".\" 全部取代成 \"[.]\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1[.]1[.]1[.]1\n" ] } ], "source": [ "// 用 algorithm 裡的方法沒辦法把 char 取代成 std::string\n", "// 如果想先把 std::string 拆成 std::string 的 vector 都需要用到 istream,搜 c++ sentense to list of words\n", "\n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::string address)\n", " {\n", " std::string res = \"\";\n", " for (const char& c : address)\n", " if (c=='.')\n", " res += \"[.]\";\n", " else\n", " res += c;\n", " \n", " return res;\n", " };\n", " \n", " std::cout << f(\"1.1.1.1\") << std::endl;\n", "}" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false, "jupyter": { "outputs_hidden": false } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "140[.]112[.]90[.]3\n" ] } ], "source": [ "// std::string 有 replace 可以用但要先自己把 \".\" 的 index 找出來\n", "// 除了 ranges 之外沒看到現成的 filter/find all 可以用\n", "\n", "#include \n", "#include \n", "#include \n", "\n", "{\n", " auto f = [](std::string address)\n", " {\n", " std::vector idx;\n", " for (size_t i=0 ; i::reverse_iterator it=idx.rbegin() ; it!=idx.rend(); it++)\n", " address.replace(*it, 1, \"[.]\");\n", "\n", " return address;\n", " };\n", " \n", " std::cout << f(\"140.112.90.3\") << std::endl;\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "C++17", "language": "C++17", "name": "xcpp17" }, "language_info": { "codemirror_mode": "text/x-c++src", "file_extension": ".cpp", "mimetype": "text/x-c++src", "name": "c++", "version": "17" } }, "nbformat": 4, "nbformat_minor": 4 }