10000 leetcode/algorithms/python/HappyNumber/isHappy.py at master · pjq/leetcode · GitHub
[go: up one dir, main page]

Skip to content
{"payload":{"allShortcutsEnabled":false,"fileTree":{"algorithms/python/HappyNumber":{"items":[{"name":"isHappy.py","path":"algorithms/python/HappyNumber/isHappy.py","contentType":"file"}],"totalCount":1},"algorithms/python":{"items":[{"name":"1-bitAnd2-bitCharacters","path":"algorithms/python/1-bitAnd2-bitCharacters","contentType":"directory"},{"name":"AddOneRowToTree","path":"algorithms/python/AddOneRowToTree","contentType":"directory"},{"name":"BinarySearchTreeIterator","path":"algorithms/python/BinarySearchTreeIterator","contentType":"directory"},{"name":"BinaryTreePostorderTraversal","path":"algorithms/python/BinaryTreePostorderTraversal","contentType":"directory"},{"name":"BinaryTreeTilt","path":"algorithms/python/BinaryTreeTilt","contentType":"directory"},{"name":"BinaryTreeZigzagLevelOrderTraversal","path":"algorithms/python/BinaryTreeZigzagLevelOrderTraversal","contentType":"directory"},{"name":"CheckCompletenessOfABinaryTree","path":"algorithms/python/CheckCompletenessOfABinaryTree","contentType":"directory"},{"name":"ConstructBinaryTreeFromInorderAndPostorderTraversal","path":"algorithms/python/ConstructBinaryTreeFromInorderAndPostorderTraversal","contentType":"directory"},{"name":"ConstructBinaryTreeFromPreorderAndInorderTraversal","path":"algorithms/python/ConstructBinaryTreeFromPreorderAndInorderTraversal","contentType":"directory"},{"name":"ContainsDuplicateII","path":"algorithms/python/ContainsDuplicateII","contentType":"directory"},{"name":"ContainsDuplicateIII","path":"algorithms/python/ContainsDuplicateIII","contentType":"directory"},{"name":"ConvertBSTtoGreaterTree","path":"algorithms/python/ConvertBSTtoGreaterTree","contentType":"directory"},{"name":"CopyListWithRandomPointer","path":"algorithms/python/CopyListWithRandomPointer","contentType":"directory"},{"name":"CountCompleteTreeNodes","path":"algorithms/python/CountCompleteTreeNodes","contentType":"directory"},{"name":"DeleteNodeInABST","path":"algorithms/python/DeleteNodeInABST","contentType":"directory"},{"name":"DiameterOfBinaryTree","path":"algorithms/python/DiameterOfBinaryTree","contentType":"directory"},{"name":"DifferentWaysToAddParentheses","path":"algorithms/python/DifferentWaysToAddParentheses","contentType":"directory"},{"name":"FibonacciNumber","path":"algorithms/python/FibonacciNumber","contentType":"directory"},{"name":"FindDuplicateSubtrees","path":"algorithms/python/FindDuplicateSubtrees","contentType":"directory"},{"name":"FindTheDuplicateNumber","path":"algorithms/python/FindTheDuplicateNumber","contentType":"directory"},{"name":"FlattenBinaryTreeToLinkedList","path":"algorithms/python/FlattenBinaryTreeToLinkedList","contentType":"directory"},{"name":"FlipBinaryTreeToMatchPreorderTraversal","path":"algorithms/python/FlipBinaryTreeToMatchPreorderTraversal","contentType":"directory"},{"name":"FlipEquivalentBinaryTrees","path":"algorithms/python/FlipEquivalentBinaryTrees","contentType":"directory"},{"name":"HappyNumber","path":"algorithms/python/HappyNumber","contentType":"directory"},{"name":"HouseRobber","path":"algorithms/python/HouseRobber","contentType":"directory"},{"name":"HouseRobberIII","path":"algorithms/python/HouseRobberIII","contentType":"directory"},{"name":"ImageSmoother","path":"algorithms/python/ImageSmoother","contentType":"directory"},{"name":"InsertionSortList","path":"algorithms/python/InsertionSortList","contentType":"directory"},{"name":"K-diffPairsInAnArray","path":"algorithms/python/K-diffPairsInAnArray","contentType":"directory"},{"name":"KthSmallestElementInABST","path":"algorithms/python/KthSmallestElementInABST","contentType":"directory"},{"name":"LargestNumberAtLeastTwiceOfOthers","path":"algorithms/python/LargestNumberAtLeastTwiceOfOthers","contentType":"directory"},{"name":"LargestPerimeterTriangle","path":"algorithms/python/LargestPerimeterTriangle","contentType":"directory"},{"name":"LinkedListCycleII","path":"algorithms/python/LinkedListCycleII","contentType":"directory"},{"name":"LongestContinuousIncreasingSubsequence","path":"algorithms/python/LongestContinuousIncreasingSubsequence","contentType":"directory"},{"name":"LongestTurbulentSubarray","path":"algorithms/python/LongestTurbulentSubarray","contentType":"directory"},{"name":"LongestUnivaluePath","path":"algorithms/python/LongestUnivaluePath","contentType":"directory"},{"name":"LowestCommonAncestorOfABinarySearchTree","path":"algorithms/python/LowestCommonAncestorOfABinarySearchTree","contentType":"directory"},{"name":"LowestCommonAncestorOfABinaryTree","path":"algorithms/python/LowestCommonAncestorOfABinaryTree","contentType":"directory"},{"name":"MaximizeDistanceToClosestPerson","path":"algorithms/python/MaximizeDistanceToClosestPerson","contentType":"directory"},{"name":"MaximumAverageSubarrayI","path":"algorithms/python/MaximumAverageSubarrayI","contentType":"directory"},{"name":"MaximumProductOfThreeNumbers","path":"algorithms/python/MaximumProductOfThreeNumbers","contentType":"directory"},{"name":"MaximumWidthOfBinaryTree","path":"algorithms/python/MaximumWidthOfBinaryTree","contentType":"directory"},{"name":"MiddleOfTheLinkedList","path":"algorithms/python/MiddleOfTheLinkedList","contentType":"directory"},{"name":"MinCostClimbingStairs","path":"algorithms/python/MinCostClimbingStairs","contentType":"directory"},{"name":"Non-decreasingArray","path":"algorithms/python/Non-decreasingArray","contentType":"directory"},{"name":"NumberOfIslands","path":"algorithms/python/NumberOfIslands","contentType":"directory"},{"name":"PancakeSorting","path":"algorithms/python/PancakeSorting","contentType":"directory"},{"name":"PartitionList","path":"algorithms/python/PartitionList","contentType":"directory"},{"name":"PathSumII","path":"algorithms/python/PathSumII","contentType":"directory"},{"name":"PathSumIII","path":"algorithms/python/PathSumIII","contentType":"directory"},{"name":"PopulatingNextRightPointersInEachNode","path":"algorithms/python/PopulatingNextRightPointersInEachNode","contentType":"directory"},{"name":"PopulatingNextRightPointersInEachNodeII","path":"algorithms/python/PopulatingNextRightPointersInEachNodeII","contentType":"directory"},{"name":"PositionsOfLargeGroups","path":"algorithms/python/PositionsOfLargeGroups","contentType":"directory"},{"name":"PrintBinaryTree","path":"algorithms/python/PrintBinaryTree","contentType":"directory"},{"name":"RedundantConnection","path":"algorithms/python/RedundantConnection","contentType":"directory"},{"name":"RemoveDuplicatesFromSortedListII","path":"algorithms/python/RemoveDuplicatesFromSortedListII","contentType":"directory"},{"name":"RemoveNthNodeFromEndOfList","path":"algorithms/python/RemoveNthNodeFromEndOfList","contentType":"directory"},{"name":"ReorderList","path":"algorithms/python/ReorderList","contentType":"directory"},{"name":"RevealCardsInIncreasingOrder","path":"algorithms/python/RevealCardsInIncreasingOrder","contentType":"directory"},{"name":"ReverseLinkedListII","path":"algorithms/python/ReverseLinkedListII","contentType":"directory"},{"name":"SearchA2DMatrixII","path":"algorithms/python/SearchA2DMatrixII","contentType":"directory"},{"name":"SecondMinimumNodeInABinaryTree","path":"algorithms/python/SecondMinimumNodeInABinaryTree","contentType":"directory"},{"name":"SerializeAndDeserializeBST","path":"algorithms/python/SerializeAndDeserializeBST","contentType":"directory"},{"name":"ShortestUnsortedContinuousSubarray","path":"algorithms/python/ShortestUnsortedContinuousSubarray","contentType":"directory"},{"name":"SortList","path":"algorithms/python/SortList","contentType":"directory"},{"name":"SquaresOfSortedArray","path":"algorithms/python/SquaresOfSortedArray","contentType":"directory"},{"name":"Subsets","path":"algorithms/python/Subsets","contentType":"directory"},{"name":"SubtreeOfAnotherTree","path":"algorithms/python/SubtreeOfAnotherTree","contentType":"directory"},{"name":"SumOfLeftLeaves","path":"algorithms/python/SumOfLeftLeaves","contentType":"directory"},{"name":"SumRootToLeafNumbers","path":"algorithms/python/SumRootToLeafNumbers","contentType":"directory"},{"name":"ThirdMaximumNumber","path":"algorithms/python/ThirdMaximumNumber","contentType":"directory"},{"name":"UniqueBinarySearchTrees","path":"algorithms/python/UniqueBinarySearchTrees","contentType":"directory"},{"name":"UniqueBinarySearchTreesII","path":"algorithms/python/UniqueBinarySearchTreesII","contentType":"directory"},{"name":"ValidMountainArray","path":"algorithms/python/ValidMountainArray","contentType":"directory"},{"name":"ValidateBinarySearchTree","path":"algorithms/python/ValidateBinarySearchTree","contentType":"directory"},{"name":"XOfAKindInADeckOfCards","path":"algorithms/python/XOfAKindInADeckOfCards","contentType":"directory"}],"totalCount":76},"algorithms":{"items":[{"name":"cpp","path":"algorithms/cpp","contentType":"directory"},{"name":"golang","path":"algorithms/golang","contentType":"directory"},{"name":"java","path":"algorithms/java","contentType":"directory"},{"name":"python","path":"algorithms/python","contentType":"directory"}],"totalCount":4},"":{"items":[{"name":"algorithms","path":"algorithms","contentType":"directory"},{"name":"database","path":"database","contentType":"directory"},{"name":"scripts","path":"scripts","contentType":"directory"},{"name":"shell","path":"shell","contentType":"directory"},{"name":".gitconfig","path":".gitconfig","contentType":"file"},{"name":".gitignore","path":".gitignore","contentType":"file"},{"name":"README.md","path":"README.md","contentType":"file"}],"totalCount":7}},"fileTreeProcessingTime":14.130790000000001,"foldersToFetch":[],"incompleteFileTree":false,"repo":{"id":25997802,"defaultBranch":"master","name":"leetcode","ownerLogin":"pjq","currentUserCanPush":false,"isFork":true,"isEmpty":false,"createdAt":"2014-10-31T01:54:39.000Z","ownerAvatar":"https://avatars.githubusercontent.com/u/87846?v=4","public":true,"private":false,"isOrgOwned":false},"codeLineWrapEnabled":false,"symbolsExpanded":false,"treeExpanded":true,"refInfo":{"name":"master","listCacheKey":"v0:1620049621.32255","canEdit":false,"refType":"branch","currentOid":"a9393efc33a1e4a44d11b1f2d60808a293e21a84"},"path":"algorithms/python/HappyNumber/isHappy.py","currentUser":null,"blob":{"rawLines":["# Method 1: straight forward solution, use a set to track if there is a cycle","","def isHappy1(self, n):"," if n \u003c= 0: return False"," s = set()"," while n not in s:"," s.add(n)"," n = sum([int(i) ** 2 for i in str(n)])"," if n == 1: return True"," return False","","# Method 2: using a slow and fast pointer to determine cycle (like in linked list)","# No extra space needed","def isHappy2(self, n):"," \"\"\""," :type n: int"," :rtype: bool"," \"\"\""," if n \u003c= 0: return False"," def helper(n):"," res = 0"," while n:"," res += (n % 10) ** 2"," n = n // 10"," return res"," "," slow = fast = n"," while True:"," slow = helper(slow)"," fast = helper(helper(fast))"," if slow == 1: return True"," if slow == fast: break"," return False"],"stylingDirectives":null,"colorizedLines":null,"csv":null,"csvError":null,"dependabotInfo":{"showConfigurationBanner":false,"configFilePath":null,"networkDependabotPath":"/pjq/leetcode/network/updates","dismissConfigurationNoticePath":"/settings/dismiss-notice/dependabot_configuration_notice","configurationNoticeDismissed":null},"displayName":"isHappy.py","displayUrl":"https://github.com/pjq/leetcode/blob/master/algorithms/python/HappyNumber/isHappy.py?raw=true","headerInfo":{"blobSize":"801 Bytes","deleteTooltip":"You must be signed in to make or propose changes","editTooltip":"You must be signed in to make or propose changes","ghDesktopPath":"https://desktop.github.com","isGitLfs":false,"onBranch":true,"shortPath":"2e21039","siteNavLoginPath":"/login?return_to=https%3A%2F%2Fgithub.com%2Fpjq%2Fleetcode%2Fblob%2Fmaster%2Falgorithms%2Fpython%2FHappyNumber%2FisHappy.py","isCSV":false,"isRichtext":false,"toc":null,"lineInfo":{"truncatedLoc":"33","truncatedSloc":"30"},"mode":"file"},"image":false,"isCodeownersFile":null,"isPlain":false,"isValidLegacyIssueTemplate":false,"issueTemplate":null,"discussionTemplate":null,"language":"Python","languageID":303,"large":false,"planSupportInfo":{"repoIsFork":null,"repoOwnedByCurrentUser":null,"requestFullPath":"/pjq/leetcode/blob/master/algorithms/python/HappyNumber/isHappy.py","showFreeOrgGatedFeatureMessage":null,"showPlanSupportBanner":null,"upgradeDataAttributes":null,"upgradePath":null},"publishBannersInfo":{"dismissActionNoticePath":"/settings/dismiss-notice/publish_action_from_dockerfile","releasePath":"/pjq/leetcode/releases/new?marketplace=true","showPublishActionBanner":false},"rawBlobUrl":"https://github.com/pjq/leetcode/raw/refs/heads/master/algorithms/python/HappyNumber/isHappy.py","renderImageOrRaw":false,"richText":null,"renderedFileInfo":null,"shortPath":null,"symbolsEnabled":true,"tabSize":8,"topBannersInfo":{"overridingGlobalFundingFile":false,"globalPreferredFundingPath":null,"showInvalidCitationWarning":false,"citationHelpUrl":"https://docs.github.com/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files","actionsOnboardingTip":null},"truncated":false,"viewable":true,"workflowRedirectUrl":null,"symbols":null},"copilotInfo":null,"copilotAccessAllowed":false,"modelsAccessAllowed":false,"modelsRepoIntegrationEnabled":false,"csrf_tokens":{"/pjq/leetcode/branches":{"post":"OPwoTaBScnJujm-W1Tre7O_5PFUjKgyISGeLJ0d1CeCuG1nGfO6wzhNoEMaoNxOPaqHo3EM4vdNFcb7dMXKhSQ"},"/repos/preferences":{"post":"T-xF8Dog1y7byoJX_fA89rae6icEKjMnbmU8pyutz8UC1esDe2PL4JhuyQRyQjVSV0m0l1R0oo9uca_tQSIiFw"}}},"title":"leetcode/algorithms/python/HappyNumber/isHappy.py at master · pjq/leetcode","appPayload":{"helpUrl":"https://docs.github.com","findFileWorkerPath":"/assets-cdn/worker/find-file-worker-263cab1760dd.js","findInFileWorkerPath":"/assets-cdn/worker/find-in-file-worker-1b17b3e7786a.js","githubDevUrl":null,"enabled_features":{"code_nav_ui_events":false,"react_blob_overlay":false,"accessible_code_button":true}}}
0