浏览代码

回文数

xxt 9 月之前
父节点
当前提交
8a0d10c3ff
共有 1 个文件被更改,包括 25 次插入2 次删除
  1. 25 2
      twoSum.py

+ 25 - 2
twoSum.py

@@ -1,7 +1,30 @@
+from typing import List
+import unittest
+
+
 class Solution:
     def twoSum(self, nums: List[int], target: int) -> List[int]:
         n = len(nums)
         for i in range(n):
-            for j in range(i + 1 , n):
+            for j in range(i + 1, n):
                 if nums[i] + nums[j] == target:
-                    return [i , j]
+                    return [i, j]
+
+
+class TestTwoSum(unittest.TestCase):
+
+    def test_two_sum1(self):
+        nums = [2, 7, 11, 15]
+        target = 9
+        s = Solution()
+        res = s.twoSum(nums, target)
+
+        self.assertEqual(res, [0, 1])
+
+    def test_two_sum2(self):
+        nums = [3, 3]
+        target = 6
+        s = Solution()
+        res = s.twoSum(nums, target)
+
+        self.assertEqual(res, [0, 1])