{ "cells": [ { "cell_type": "markdown", "id": "after-potter", "metadata": {}, "source": [ "# [Quick Start Python](https://github.com/dashidhy/algorithm-pattern-python/blob/master/introduction/quickstart.md)" ] }, { "cell_type": "code", "execution_count": 2, "id": "infrared-crisis", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import IPython; IPython.display.HTML('''
''')" ] }, { "cell_type": "markdown", "id": "suited-atmosphere", "metadata": {}, "source": [ "## [28. Implement strStr()](https://leetcode.com/problems/implement-strstr/)\n", "\n", "Easy\n", "\n", "Implement strStr().\n", "\n", "Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.\n", "\n", "Clarification:\n", "\n", "What should we return when needle is an empty string? This is a great question to ask during an interview.\n", "\n", "For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().\n", "\n", "\n", "Example 1:\n", "\n", " Input: haystack = \"hello\", needle = \"ll\"\n", " Output: 2\n", " \n", "\n", "Constraints:\n", "\n", " 0 <= haystack.length, needle.length <= 5 * 10^4\n", " haystack and needle consist of only lower-case English characters.\n", "\n" ] }, { "cell_type": "code", "execution_count": 4, "id": "premium-nebraska", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "haystack = \"hello\"\n", "needle = \"ll\"\n", "\n", "def strStr(haystack, needle):\n", " try:\n", " return haystack.index(needle)\n", " except ValueError:\n", " return -1\n", " \n", "strStr(haystack, needle)" ] }, { "cell_type": "markdown", "id": "resident-sword", "metadata": {}, "source": [ "## [78. Subsets](https://leetcode.com/problems/subsets/)\n", "\n", "Medium\n", "\n", "Given an integer array nums of unique elements, return all possible subsets (the power set).\n", "\n", "The solution set must not contain duplicate subsets. Return the solution in any order.\n", "\n", "\n", "\n", "Example 1:\n", "\n", " Input: nums = [1,2,3]\n", " Output: [[],[1],[2],[1,2],[3],[1,3],[2,3],[1,2,3]]\n", "\n", "Constraints:\n", "\n", " 1 <= nums.length <= 10\n", " -10 <= nums[i] <= 10\n", " All the numbers of nums are unique." ] }, { "cell_type": "code", "execution_count": 1, "id": "nearby-polls", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[], [1], [2], [1, 2], [3], [1, 3], [2, 3], [1, 2, 3]]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# bottom up\n", "\n", "nums = [1, 2, 3]\n", "\n", "def subsets(nums):\n", " res = [[]]\n", " for n in nums:\n", " for subset in res[:]:\n", " res.append(subset + [n])\n", "\n", " return res\n", " \n", "subsets(nums)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.7.10" } }, "nbformat": 4, "nbformat_minor": 5 }