def twoSum(self, nums, target):
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i]+nums[j]==target:
return i,j
def addTwoNumbers(self,l1,l2):
# 创建一个结点值为 None 的头结点, dummy 和 p 指向头结点, dummy 用来最后返回, p 用来遍历
dummy=p=ListNode(None)
# 初始化进位 s 为 0
s=0
while l1 or l2 or s:
# 如果 l1 或 l2 存在, 则取l1的值 + l2的值 + s(s初始为0, 如果下面有进位1, 下次加上)
s+=(l1.val if l1 else 0)+(l2.val if l2 else 0)
# p.next 指向新链表, 用来创建一个新的链表
p.next=ListNode(s%10)
# p 向后遍历
p=p.next
# 有进位情况则取模, eg. s = 18, 18 // 10 = 1
s//=10
# 如果l1存在, 则向后遍历, 否则为 None
l1 = l1.next if l1 else None
# 如果l2存在, 则向后遍历, 否则为 None
l2 = l2.next if l2 else None
# 返回 dummy 的下一个节点, 因为 dummy 指向空的头结点, 下一个节点才是新建链表的后序节点
return dummy.next
def lengthOfLongestSubstring(self, s):
k=len(s)
for i in range(len(s)):
for j in range(i+1,k):
if s[i]!=s[j]:
continue
else:
k=j
break
return k
def findMedianSortedArrays(self, nums1, nums2):
list=[]
for i in range(len(nums1)):
list.append(nums1[i])
for j in range(len(nums2)):
list.append(nums2[j])
print(list)
list.sort()
print(list)
if (len(list)%2)==0:
return (list[len(list)/2-1]+list[len(list)/2])/2.0
else:
return list[len(list)//2]
def longestPalindrome(self, s):
list=[]
for i in range(len(s)):
if s[i]==s[len(s)-i-1]:
list.append(s[i:len(s)-i])
continue
return max(list)
def reverse(self, x):
num=[]
i=0
while x:
num.append(x % 10)
x= x / 10
i = i + 1
s=0
for i in range(len(num)):
s = s + num[i]*10**(len(num)-i-1)
return s
public double myPow(double x, int n)
{
double sum=1;
for(int i=Math.abs(n);i>0;i--)
{
sum=sum*x;
}
if(n>=0)
{
return sum;
}
else
{
return 1/sum;
}
}
class MyHashSet(object):
def __init__(self):
self.nums = []
def add(self, key):
if key not in self.nums:
self.nums.append(key)
def remove(self, key):
if key in self.nums:
self.nums.remove(key)
else:
pass
def contains(self, key):
return key in self.nums
def maximumCount(self, nums):
countzheng=0
countfu=0
for i in range(len(nums)):
if nums[i]>0:
countzheng+=1
elif nums[i]<0:
countfu+=1
else:
continue
max=countfu
if max<countzheng:
max =countzheng
return max
def findChampion(self, grid):
n=len(grid)
sums=numpy.ones(n)
for i in range(n):
for j in range(n):
sums[i]+=grid[i][j]
return numpy.argmax(sums)
def findChampion(self, n, edges):
"""
:type n: int
:type edges: List[List[int]]
:rtype: int
"""
# 创建一个列表d,用于记录每个节点的入边数量,初始化为0,长度设为n
d = [0] * n
# 遍历每一条边,对于每条边的目标节点b,增加其入边的计数
for a, b in edges:
d[b] += 1
# 找到所有没有入边的节点(即入边计数为0的节点)
# 初始化一个空列表来存放没有入边的节点
champions = []
# 遍历所有节点的索引
for i in range(n):
# 检查节点i的入边数量是否为0
if d[i] == 0:
# 如果没有入边,则将节点i添加到champions列表中
champions.append(i)
# 如果恰好只有一个没有入边的节点,则返回该节点的编号,否则返回-1
return champions[0] if len(champions) == 1 else -1
class MyHashMap(object):
def __init__(self, nums=None):
if nums is None:
self.nums = {}
else:
self.nums = nums
def put(self, key, value):
self.nums[key] = value
def get(self, key):
if key in self.nums:
return self.nums[key]
else:
return -1
def remove(self, key):
if key in self.nums:
del self.nums[key]