Skip to content

Instantly share code, notes, and snippets.

@nabeen
Last active May 8, 2020 13:28
Show Gist options
  • Save nabeen/5a70e71ed5b81c2268df837208c19c02 to your computer and use it in GitHub Desktop.
Save nabeen/5a70e71ed5b81c2268df837208c19c02 to your computer and use it in GitHub Desktop.
two-sum.ipynb
Display the source blob
Display the rendered blob
Raw
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "two-sum.ipynb",
"provenance": [],
"authorship_tag": "ABX9TyP0ehzGUGC7m/UpgNMN1SGZ",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/nabeen/5a70e71ed5b81c2268df837208c19c02/two-sum.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"metadata": {
"id": "GWDh0I6mbbhB",
"colab_type": "code",
"colab": {}
},
"source": [
"from typing import List"
],
"execution_count": 0,
"outputs": []
},
{
"cell_type": "code",
"metadata": {
"id": "VNMVHsYPYpMQ",
"colab_type": "code",
"outputId": "92a6fad2-e031-4130-8d9b-04416becb73f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
}
},
"source": [
"class Solution:\n",
" def twoSum(self, nums: List[int], target: int) -> List[int]:\n",
" for i in range(len(nums)):\n",
" print('{fix}を固定した場合'.format(fix=nums[i]))\n",
"\n",
"nums = [2, 7, 11, 15]\n",
"target = 9\n",
"s = Solution()\n",
"s.twoSum(nums, target)"
],
"execution_count": 3,
"outputs": [
{
"output_type": "stream",
"text": [
"2を固定した場合\n",
"7を固定した場合\n",
"11を固定した場合\n",
"15を固定した場合\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "1-t20VFwbJW7",
"colab_type": "code",
"outputId": "dea9a801-6207-48c1-f80d-dc56c7be618e",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 187
}
},
"source": [
"class Solution:\n",
" def twoSum(self, nums: List[int], target: int) -> List[int]:\n",
" for i in range(len(nums)):\n",
" print('{fix}を固定した場合'.format(fix=nums[i]))\n",
" for j in range(i+1, len(nums)):\n",
" print('{i}-{j}'.format(i=nums[i], j=nums[j]))\n",
"\n",
"nums = [2, 7, 11, 15]\n",
"target = 9\n",
"s = Solution()\n",
"s.twoSum(nums, target)"
],
"execution_count": 4,
"outputs": [
{
"output_type": "stream",
"text": [
"2を固定した場合\n",
"2-7\n",
"2-11\n",
"2-15\n",
"7を固定した場合\n",
"7-11\n",
"7-15\n",
"11を固定した場合\n",
"11-15\n",
"15を固定した場合\n"
],
"name": "stdout"
}
]
},
{
"cell_type": "code",
"metadata": {
"id": "st4-Ub11bNzC",
"colab_type": "code",
"outputId": "e7a9c4ea-6358-4e10-aa83-efc5d49c9e91",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
}
},
"source": [
"class Solution:\n",
" def twoSum(self, nums: List[int], target: int) -> List[int]:\n",
" for i in range(len(nums)):\n",
" for j in range(i+1, len(nums)):\n",
" if nums[i] + nums[j] == target:\n",
" return [i,j]\n",
"\n",
"nums = [2, 7, 11, 15]\n",
"target = 9\n",
"s = Solution()\n",
"s.twoSum(nums, target)"
],
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"[0, 1]"
]
},
"metadata": {
"tags": []
},
"execution_count": 5
}
]
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment