{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Number 136" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[https://leetcode.com/problems/single-number/](https://leetcode.com/problems/single-number/)" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "from typing import List\n", "\n", "class Solution:\n", " def singleNumber(self, nums: List[int]) -> int:\n", " \"\"\"\n", " Implementation 1\n", " \"\"\"\n", " desired_dict = {}\n", " for i in nums:\n", " if i in desired_dict:\n", " desired_dict[i] = desired_dict[i]+1\n", " else:\n", " desired_dict[i] = 1\n", " \n", " return [i for i in desired_dict.keys() if desired_dict[i]==1][0]\n", " \n", " def singleNumber_revised(self, nums: List[int]) -> int:\n", " \"\"\"\n", " Implementation 2\n", " \"\"\"\n", " desired_set = set()\n", " for i in nums:\n", " if i in desired_set:\n", " desired_set.remove(i)\n", " else:\n", " desired_set.add(i)\n", " \n", " return desired_set.pop()\n", "\n", "a = Solution()" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print(a.singleNumber([4,1,2,1,2]))" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4\n" ] } ], "source": [ "print(a.singleNumber_revised([4,1,2,1,2]))" ] } ], "metadata": { "interpreter": { "hash": "033d5ea8e9748582193a6d8f975af35153e280c1f566336ac6ff582d76ae2a04" }, "kernelspec": { "display_name": "Python 3.6.8 64-bit", "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.6.8" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }