摘要: 给定一个数,得到这个数的数位上的数字的平方和,然后循环,如果最后能等于1,则输出true,否则为false. 难点:不知道这个数到底要循环多少轮才能得到1.思路:设置一个最大的循环数(如:20轮),如果达到最大循环次数,还不等于1,则返回false. #include<iostream> #incl 阅读全文
posted @ 2020-05-10 23:33 星海寻梦233 阅读(98) 评论(0) 推荐(0)
摘要: #include<iostream> #include<vector> #include<algorithm> using namespace std; int rob(vector<int>& nums) { if (nums.size() == 0) return 0; if (nums.siz 阅读全文
posted @ 2020-05-10 22:43 星海寻梦233 阅读(131) 评论(0) 推荐(0)
摘要: #include<iostream> #include<string> using namespace std; int hammingWeight(uint32_t n) { int ans = 0; while (n) { if (n % 2 == 1) { ans += 1; } n = n 阅读全文
posted @ 2020-05-10 20:12 星海寻梦233 阅读(113) 评论(0) 推荐(0)
摘要: #include<iostream> #include<string> using namespace std; uint32_t reverseBits(uint32_t n) { uint32_t a = 0; int count = 0; string s = ""; while (n) { 阅读全文
posted @ 2020-05-10 19:47 星海寻梦233 阅读(91) 评论(0) 推荐(0)
摘要: class Solution: def romanToInt(self, s: str) -> int: dic = {} dic["I"] = 1 dic["V"] = 5 dic["X"] = 10 dic["L"] = 50 dic["C"] = 100 dic["D"] = 500 dic[ 阅读全文
posted @ 2020-05-09 15:29 星海寻梦233 阅读(98) 评论(0) 推荐(0)
摘要: class Solution: def longestCommonPrefix(self, strs: List[str]) -> str: if len(strs) == 0: return "" last_s = strs[0] same_prefix = "" for i, s in enum 阅读全文
posted @ 2020-05-09 15:28 星海寻梦233 阅读(97) 评论(0) 推荐(0)
摘要: class Solution: def isValid(self, s: str) -> bool: dic = {"(":")",")":"(","[":"]","]":"[","{":"}","}":"{"} if len(s) == 0: return True else: stack = " 阅读全文
posted @ 2020-05-09 15:26 星海寻梦233 阅读(112) 评论(0) 推荐(0)
摘要: class Solution: def removeDuplicates(self, nums: List[int]) -> int: j = 0 for i in range(len(nums)): if i >j and nums[i] > nums[j] : j += 1 nums[j] = 阅读全文
posted @ 2020-05-09 15:25 星海寻梦233 阅读(102) 评论(0) 推荐(0)
摘要: # Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None class Solution: def mergeTwoLists(se 阅读全文
posted @ 2020-05-09 15:23 星海寻梦233 阅读(134) 评论(0) 推荐(0)
摘要: class Solution: def removeElement(self, nums: List[int], val: int) -> int: j = 0 for i, v in enumerate(nums): if v != val: nums[j] = v j += 1 return j 阅读全文
posted @ 2020-05-09 15:21 星海寻梦233 阅读(105) 评论(0) 推荐(0)