Topics MathBit Manipulation Companies Hint 1 Write a helper function to count the number of set bits in a number, then check whether the number of set bits is 2, 3, 5, 7, 11, 13, 17 or 19. Similar Questions Number of 1 Bits Easy Discussion (18) ...
leetcode_136 只出现一次的数字 class Solution(object): def singleNumber(self, nums): """ :type nums: List[int] :rtype: int """ ans = 0 for i in nums: ans ^= i return ans 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 参考文献: https://www.runoob.com/python3/python3-tutorial.ht...
— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime! — I see, being theprime ministeryou cannot stand having a non-prime number on your door even for a few seconds. — Correct! So I must invent a ...
packageleetcodeimport("container/heap")// 解法一 二分搜索funckthSmallest378(matrix[][]int,kint)int{m,n,low:=len(matrix),len(matrix[0]),matrix[0][0]high:=matrix[m-1][n-1]+1forlow<high{mid:=low+(high-low)>>1// 如果 count 比 k 小,在大值的那一半继续二分搜索ifcounterKthSmall(...
html = urlopen(url)exceptHTTPErrorase:# the page is not found on the server (or there was an error in retrieving it).returnNonetry:# if the server is not found (or the server did not exist),# then html would be a None object, and html.read() would throw an AttributeErrorbs = ...
packageleetcode// ListNode definetypeListNode=structures.ListNode/** * Definition for singly-linked list. * type ListNode struct { * Val int * Next *ListNode * } */funcreverseKGroup(head*ListNode,kint)*ListNode{node:=headfori:=0;i<k;i++{ifnode==nil{returnhead}node=node.Next}newHead:...
packageleetcodefunclongestPalindrome(sstring)int{counter:=make(map[rune]int)for_,r:=ranges{counter[r]++}answer:=0for_,v:=rangecounter{answer+=v/2*2ifanswer%2==0&&v%2==1{answer++}}returnanswer} 1. 2. 3. 4. 5. 6. 7.
packageleetcodeimport("/halfrost/LeetCode-Go/template")vardir=[][]int{{-1,0},{0,1},{1,0},{0,-1},}// 解法一 并查集funcsolve(board[][]byte){iflen(board)==0{return}m,n:=len(board[0]),len(board)uf:=template.UnionFind{}uf.Init(n*m+1)// 特意多一个特殊点用来标记fori:=0...
packageleetcodeimport("fmt""strconv")// 解法一funcreadBinaryWatch(numint)[]string{memo:=make([]int,60)// count the number of 1 in a binary numbercount:=func(nint)int{ifmemo[n]!=0{returnmemo[n]}originN,res:=n,0forn!=0{n=n&(n-1)res++}memo[originN]=resreturnres}// fmtMinut...
packageleetcodeimport"strings"funcreverseWords151(sstring)string{ss:=strings.Fields(s)reverse151(&ss,0,len(ss)-1)returnstrings.Join(ss," ")}funcreverse151(m*[]string,iint,jint){fori<=j{(*m)[i],(*m)[j]=(*m)[j],(*m)[i]i++j--}} ...