最近忙着調教新裝備,沒有及時的寫題解,可是沒有在偷懶沒刷題喔~
來認真整理下最近作的題目~node
以前考慮按tag
來刷題,後來收到了推薦的leetcode題解
,就根據上面的說明陸續刷題啦~
tag主要作了:數組、雙指針
找時間要開始部署gitbook
了,而後將題解部署到電子書上~python
387. 字符串中的第一個惟一字符
難度:簡單
給定一個字符串,找到它的第一個不重複的字符,並返回它的索引。若是不存在,則返回-1
。git
案例:
s = "leetcode"
返回 0.
s = "loveleetcode",
返回 2.
個人題解:算法
class Solution(object): def firstUniqChar(self, s): """ :type s: str :rtype: int """ mapa = dict() for i in s: if i not in mapa: mapa[i] = 1 else: mapa[i] += 1 for j in range(len(s)): a = s[j] if a in mapa and mapa[a] == 1: return j return -1
個人思路:
作兩次循環,第一次循環用來作映射表,用hash表能夠快速查詢。
第二遍從頭檢查,在hash表中僅出現一次的字母,即最先不重複的字母。數組
283. 移動零
難度:簡單
給定一個數組 nums,編寫一個函數將全部0
移動到數組的末尾,同時保持非零元素的相對順序。app
案例:
輸入: [0,1,0,3,12]
輸出: [1,3,12,0,0]
返回 2.
個人題解:函數
class Solution(object): def moveZeroes(self, nums): """ :type nums: List[int] :rtype: None Do not return anything, modify nums in-place instead. """ l = len(nums) j = 0 for i in range(l): if nums[i] !=0: nums[j] = nums[i] j +=1 nums[j:l] = [0 for i in range(l-j)]
個人思路:
從頭遍歷數組,若是對應數組的值不爲0,則利用慢指針,將非零項向前移動歸併。
最後一個非零項對應的索引到數組的最後則被0包圍了~ui
268. 缺失數字
難度:簡單
給定一個包含0, 1, 2, ..., n
中 n 個數的序列,找出 0 .. n 中沒有出如今序列中的那個數。spa
案例:
輸入: [3,0,1]
輸出: 2
輸入: [9,6,4,2,3,5,7,0,1]
輸出: 8
個人題解:3d
class Solution(object): def missingNumber(self, nums): """ :type nums: List[int] :rtype: int """ sum = 0 l =len(nums) sum_a = (1+l)*l/2 for i in nums: sum += i return sum_a - sum```
個人思路:
缺乏的值 = 未缺失數的序列綜合 - 當前的序列總和
229. 求衆數 II
難度:簡單
給定一個大小爲 n 的數組,找出其中全部出現超過⌊ n/3 ⌋
次的元素。
說明: 要求算法的時間複雜度爲O(n)
,空間複雜度爲O(1)
。
案例:
輸入: [3,2,3]
輸出: [3]
輸入: [1,1,1,3,3,2,2,2]
輸出: [1,2]
個人題解:
class Solution(object): def majorityElement(self, nums): """ :type nums: List[int] :rtype: List[int] """ a = dict() b = list() n = len(nums) / 3 for i in nums: if i not in a: a[i] = 1 else: a[i] += 1 for j in a: if a[j] > n: b.append(j) return b
個人思路:
同第一題的思路一致,兩次循環,第一次檢查每一個數的重複狀況。
第二遍循環用於找出對應的值。
101. 對稱二叉樹
難度:簡單
給定一個二叉樹,檢查它是不是鏡像對稱的。
例如,二叉樹[1,2,2,3,4,4,3]
是對稱的。
可是下面這個[1,2,2,null,3,null,3]
則不是鏡像對稱的:
個人題解:
# Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution(object): def isSymmetric(self, root): """ :type root: TreeNode :rtype: bool """ if not root: return True return self.isSame(root.left,root.right) def isSame(self,leftNode,rightNode): if leftNode == None: return rightNode == None if rightNode == None: return leftNode == None if rightNode.val == leftNode.val: return self.isSame(leftNode.left,rightNode.right) and self.isSame(leftNode.right,rightNode.left) return False
個人思路:
使用遞歸
的思路,跳出條件爲,左右節點不一致,包括二者某一個爲空的狀況。
當還存在下一級的左右節點的時候,就作遞歸進行查找。
905. 按奇偶排序數組
難度:簡單
給定一個非負整數數組A
,返回一個由A
的全部偶數元素組成的數組,後面跟A
的全部奇數元素。
你能夠返回知足此條件的任何數組做爲答案。
示例:
輸入:[3,1,2,4]
輸出:[2,4,3,1]
輸出 [4,2,3,1],[2,4,1,3] 和 [4,2,1,3] 也會被接受。
個人題解:
class Solution(object): def sortArrayByParity(self, A): """ :type A: List[int] :rtype: List[int] """ n = [0]*len(A) k = 0 j = len(A) - 1 for i in range(len(A)): if A[i] % 2 ==1: #奇數 n[j] = A[i] j -= 1 else: n[k] = A[i] k += 1 return n
個人思路:
新建一個數組,而後頭尾兩個指針,分別用於指向偶數和奇數。
832. 翻轉圖像
難度:簡單
給定一個二進制矩陣A
,咱們想先水平翻轉圖像,而後反轉圖像並返回結果。
水平翻轉圖片就是將圖片的每一行都進行翻轉,即逆序。例如,水平翻轉[1, 1, 0]
的結果是[0, 1, 1]
。
反轉圖片的意思是圖片中的0
所有被1
替換,1
所有被0
替換。例如,反轉[0, 1, 1]
的結果是[1, 0, 0]
。
個人題解:
class Solution(object): def flipAndInvertImage(self, A): """ :type A: List[List[int]] :rtype: List[List[int]] """ #逆序 return [[j ^ 1 for j in i[::-1]] for i in A]
個人思路:
python感受有不少小做弊的方式,好比這題先進行了逆序,而後再進行了位運算。
僅僅用了一行代碼也是很奇特了。
922. 按奇偶排序數組 II
難度:簡單
給定一個非負整數數組A
,A
中一半整數是奇數,一半整數是偶數。
對數組進行排序,以便當A[i]
爲奇數時,i
也是奇數;當A[i]
爲偶數時,i
也是偶數。
你能夠返回任何知足上述條件的數組做爲答案。
個人題解:
class Solution(object): def sortArrayByParityII(self, A): """ :type A: List[int] :rtype: List[int] """ count0 = 0 count1 = 1 re = [] for i in A: if i%2 == 0: re.insert(count0, i) count0 += 2 else: re.insert(count1, i) count1 += 2 return re
個人思路:
適用兩個指針,一個從0開始,一個從1開始,每次的步數爲2,對應放入奇數和偶數幾顆。
509. 斐波那契數
難度:簡單
斐波那契數,一般用F(n)
表示,造成的序列稱爲斐波那契數列。該數列由0
和1
開始,後面的每一項數字都是前面兩項數字的和。也就是:
F(0) = 0, F(1) = 1
F(N) = F(N - 1) + F(N - 2), 其中 N > 1.
給定N
,計算F(N)
。
個人題解:
class Solution(object): def fib(self, N): """ :type N: int :rtype: int """ if N == 0:return 0 if N==1 or N == 2:return 1 return (self.fib(N-1)+self.fib(N-2))
個人思路:
由於每個數的構成,都是由前面的數的基礎構成,因此用遞歸的思路去尋找遞歸棧。
遞歸棧的跳出條件爲:n=0/1/2。
561. 數組拆分 I
難度:簡單
給定長度爲2n的數組, 你的任務是將這些數分紅 n 對, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得從1 到 n 的 min(ai, bi) 總和最大。
輸入: [1,4,3,2]輸出: 4
解釋: n 等於 2, 最大總和爲 4 = min(1, 2) + min(3, 4).
提示:
n 是正整數,範圍在 [1, 10000].
數組中的元素範圍在 [-10000, 10000].
個人題解:
class Solution(object): def arrayPairSum(self, nums): """ :type nums: List[int] :rtype: int """ nums.sort() return sum(nums[::2])
個人思路:
最大的算法,實際上是排序後獲取2個一組中最小的那個數,得到的總值最大。
867. 轉置矩陣
難度:簡單
給定一個矩陣A
, 返回A
的轉置矩陣。
矩陣的轉置是指將矩陣的主對角線翻轉,交換矩陣的行索引與列索引。
個人題解:
class Solution(object): def transpose(self, A): """ :type A: List[List[int]] :rtype: List[List[int]] """ return zip(*A)
個人思路:
zip()函數,打包爲元組的列表。
zip(*)返回二維矩陣。--->解壓
1002. 查找經常使用字符
難度:簡單
給定僅有小寫字母組成的字符串數組 A,返回列表中的每一個字符串中都顯示的所有字符(包括重複字符)組成的列表。例如,若是一個字符在每一個字符串中出現3
次,但不是4
次,則須要在最終答案中包含該字符3
次。
你能夠按任意順序返回答案。
示例 1:
輸入:["bella","label","roller"]
輸出:["e","l","l"]
個人題解:
class Solution(object): def commonChars(self, A): """ :type A: List[str] :rtype: List[str] """ tmp = list(A[0]) for i in range(1,len(A)): tmp_list =list() for j in A[i]: if j in tmp: index = tmp.index(j) del tmp[index] tmp_list.append(j) tmp = tmp_list return tmp
個人思路:
最基礎的思路是雙重循環,外層數組要是遍歷一維,內層主要是循環每一個二維。
最開始以a[0]做爲重複值的參考,若是比對過程當中發現了重複的值,就記錄下來,並由於考慮到參考值自己可能存在重複值,因此刪除對應索引上的值,並根據記錄下來的重複值,不斷的遍歷過程當中,就減小,最終得到的就是正確值。
350. 兩個數組的交集 II
難度:簡單
給定兩個數組,編寫一個函數來計算它們的交集。
示例 1:
輸入: nums1 = [1,2,2,1], nums2 = [2,2]
輸出: [2,2]
輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出: [4,9]
說明:
輸出結果中每一個元素出現的次數,應與元素在兩個數組中出現的次數一致。
咱們能夠不考慮輸出結果的順序。
個人題解:
class Solution(object): def intersect(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ l = list() for i in nums2: if i in nums1: index = nums1.index(i) del nums1[index] l.append(i) return l
個人思路:
另外增長一個數組用於記錄重複值,由於可能存在同個數組中重複值,因此須要刪除對應的索引上的值。
349. 兩個數組的交集
難度:簡單
給定兩個數組,編寫一個函數來計算它們的交集。
示例:
輸入: nums1 = [1,2,2,1], nums2 = [2,2]
輸出: [2]
輸入: nums1 = [4,9,5], nums2 = [9,4,9,8,4]
輸出: [9,4]
說明:
輸出結果中的每一個元素必定是惟一的。
咱們能夠不考慮輸出結果的順序。
個人題解:
class Solution(object): def intersection(self, nums1, nums2): """ :type nums1: List[int] :type nums2: List[int] :rtype: List[int] """ l = dict() a = list() for i in nums2: if i in nums1: if i not in l: l[i] = 1 for key in l: a.append(key) return a
個人思路:
循環其中一個數組,並新建hash記錄重複值。最後遍歷hash表,得出最終結果。
566. 重塑矩陣
難度:簡單
在MATLAB中,有一個很是有用的函數reshape
,它能夠將一個矩陣重塑爲另外一個大小不一樣的新矩陣,但保留其原始數據。
給出一個由二維數組表示的矩陣,以及兩個正整數r和c,分別表示想要的重構的矩陣的行數和列數。
重構後的矩陣須要將原始矩陣的全部元素以相同的行遍歷順序填充。
若是具備給定參數的reshape
操做是可行且合理的,則輸出新的重塑矩陣;不然,輸出原始矩陣。
示例:
輸入:
nums =
[[1,2],
[3,4]]
r = 1, c = 4
輸出:
[[1,2,3,4]]
解釋:
行遍歷nums的結果是 [1,2,3,4]。新的矩陣是 1 * 4 矩陣, 用以前的元素值一行一行填充新矩陣。
個人題解:
class Solution(object): def matrixReshape(self, nums, r, c): """ :type nums: List[List[int]] :type r: int :type c: int :rtype: List[List[int]] """ l_a = len(nums) l_b = len(nums[0]) if l_a*l_b != r*c: return nums if l_a == r: return nums list_a = list() list_b = list() count = 0 for i in range(l_a): for j in range(l_b): list_b.append(nums[i][j]) count += 1 if count == c: list_a.append(list_b) list_b = list() count = 0 return list_a
個人思路:
首先判斷行數和列數是否和給出的值一致,乘積是否一致,用於判斷是不是否直接輸出結果或者是否可行。
而後新建兩個數組用於新建矩陣,遍歷原有矩陣便可。
485. 最大連續1的個數
難度:簡單
給定一個二進制數組, 計算其中最大連續1的個數。
示例 1:
輸入: [1,1,0,1,1,1]
輸出: 3
解釋: 開頭的兩位和最後的三位都是連續1,因此最大連續1的個數是3
注意:
輸入的數組只包含0
和1
。
輸入數組的長度是正整數,且不超過10,000
。
個人題解:
class Solution(object): def findMaxConsecutiveOnes(self, nums): """ :type nums: List[int] :rtype: int """ max_l = 0 count = 0 for i in nums: if i == 1 : count +=1 else: ###遇到0 if count > max_l: max_l = count count = 0 if count > max_l: max_l = count return max_l
個人思路:
使用動態規劃的思路,記錄每次的最大值並進行比對。最後輸出最大值。
167. 兩數之和 II - 輸入有序數組
難度:簡單
給定一個已按照升序排列 的有序數組,找到兩個數使得它們相加之和等於目標數。
函數應該返回這兩個下標值index1
和index2
,其中index1
必須小於index2
。
說明:
個人題解:
class Solution(object): def twoSum(self, numbers, target): """ :type numbers: List[int] :type target: int :rtype: List[int] """ l = len(numbers) i = 0 j = l - 1 l_a = list() while i < j: if numbers[i]+numbers[j] == target: l_a.append(i+1) l_a.append(j+1) return l_a elif numbers[i]+numbers[j] > target: j -= 1 else: i +=1 return null
個人思路:
使用頭尾兩個指針,當二者對應的值相加,當大於目標值的時候,則尾指針左移,當小於目標值得時候,則尾指針右移。
633. 平方數之和
難度:簡單
給定一個非負整數 c ,你要判斷是否存在兩個整數a
和b
,使得a²
+b²
= c。
示例:
輸入: 5
輸出: True
解釋: 1 1 + 2 2 = 5
個人題解:
import math class Solution(object): def judgeSquareSum(self, c): """ :type c: int :rtype: bool """ a = int(math.sqrt(c)) b = 0 while a > b: if a**2 + b**2 == c: return True elif a**2 + b**2 > c: a -= 1 else: b += 1 if a**2 + b**2 == c: return True else: return False
個人思路:
兩個指針,一個從0開始,一個從開方數開始,不斷逼近,斷定是否平方和爲對應值。
345. 反轉字符串中的元音字母
難度:簡單
編寫一個函數,以字符串做爲輸入,反轉該字符串中的元音字母。
示例:
輸入: "hello"
輸出: "holle"
個人題解:
class Solution(object): def reverseVowels(self, s): """ :type s: str :rtype: str """ y = ['a','e','i','o','u','A','E','I','O','U'] p = 0 q = len(s) - 1 s = list(s) while p<=q: if s[q] not in y and s[p] not in y: p += 1 q -= 1 elif s[p] in y and s[q] not in y: q -= 1 elif s[q] in y and s[p] not in y: p += 1 else: flag = s[q] s[q] = s[p] s[p] = flag p += 1 q -= 1 return ''.join(s)
個人思路:
使用自定義hash表,並使用雙指針,不斷逼近,當於到二者都是元音的時候,進行數值交換。
141. 環形鏈表
難度:簡單
給定一個鏈表,判斷鏈表中是否有環。
爲了表示給定鏈表中的環,咱們使用整數 pos 來表示鏈表尾鏈接到鏈表中的位置(索引從 0 開始)。 若是pos
是-1
,則在該鏈表中沒有環。
個人題解:
# Definition for singly-linked list. # class ListNode(object): # def __init__(self, x): # self.val = x # self.next = None class Solution(object): def hasCycle(self, head): """ :type head: ListNode :rtype: bool """ if not head: return False l1 = head l2 = head.next while l1 and l2 and l2.next: if l1 == l2: return True l1 = l1.next l2 = l2.next.next return False
個人思路:
使用快慢兩個指針,一個步數爲1,一個步數爲2,當存在環的時候,二者必定會相遇。
985. 查詢後的偶數和
難度:簡單
給出一個整數數組A
和一個查詢數組queries
。
對於第i
次查詢,有val=queriesi, index = queriesi,咱們會把val
加到A[index]
上。而後,第i
次查詢的答案是A
中偶數值的和。
(此處給定的 index = queriesi 是從 0 開始的索引,每次查詢都會永久修改數組 A。)
返回全部查詢的答案。
你的答案應當以數組 answer 給出,answer[i] 爲第 i 次查詢的答案。
示例:
輸入:A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
輸出:[8,6,2,4]
解釋:
開始時,數組爲 [1,2,3,4]。
將 1 加到 A[0] 上以後,數組爲 [2,2,3,4],偶數值之和爲 2 + 2 + 4 = 8。
將 -3 加到 A[1] 上以後,數組爲 [2,-1,3,4],偶數值之和爲 2 + 4 = 6。
將 -4 加到 A[0] 上以後,數組爲 [-2,-1,3,4],偶數值之和爲 -2 + 4 = 2。
將 2 加到 A[3] 上以後,數組爲 [-2,-1,3,6],偶數值之和爲 -2 + 6 = 4。
個人題解:
class Solution(object): def sumEvenAfterQueries(self, A, queries): """ :type A: List[int] :type queries: List[List[int]] :rtype: List[int] """ l =list() sum = 0 sum_a = 0 for j in A: if j%2 ==0: sum_a += j for i in range(len(queries)): A[queries[i][1]] += queries[i][0]#增長數值 if A[queries[i][1]] % 2 ==0:#是偶數 if queries[i][0]%2 ==0:#是偶數 sum = sum_a + queries[i][0] else:#是奇數 sum = sum_a + A[queries[i][1]] else:#是奇數 if queries[i][0]%2 ==0:#是偶數 sum = sum_a else: sum = sum_a - A[queries[i][1]] + queries[i][0] l.append(sum) sum_a =sum return l
個人思路
每次比對前,比對所加數的奇偶,以及對應的數原有的奇偶。
當奇數+奇數,則總值加上倆奇數之和;當奇數+偶數,則總值不增長;當偶數加偶數,則總數增長新增值;當偶數+奇數,則總值減小原有偶數值。
按tag來刷,會對對應思路有更爲深入的理解,小李要繼續加油呀!