class Solution: def validPalindrome(self, s: str) -> bool: l, r = 0, len(s) - 1 while l < r: if s[l] != s[r]: case1, case2 = s[l:r], s[l+1:r+1] # 如果原數組去頭,去尾的兩個數組有一個爲迴文,則整體爲迴文 return case1 == case1[::-1] or case2 == case2[::-1] l, r = l+1, r-1 return True
附上題目鏈接
class Solution: def validMountainArray(self, A: List[int]) -> bool: if len(A) < 3: return False a = A.index(max(A)) if a == 0 or a == len(A) - 1: return False for i in range(a): if A[i] >= A[i+1]: return False for i in range(a,len(A)-1): if A[i] <= A[i+1]: return False return True
class Solution: def isBoomerang(self, points: List[List[int]]) -> bool: # 判斷三個點相同 if not(points[0] != points[1] != points[2]): return False # 比較斜率 k1 = (points[1][1]-points[0][1])/(points[1][0]-points[0][0]) if points[1][0]-points[0][0] != 0 else 'non-exist' k2 = (points[2][1]-points[1][1])/(points[2][0]-points[1][0]) if points[2][0]-points[1][0] != 0 else 'non-exist' return k1 != k2
面試題62. 圓圈中最後剩下的數字 class Solution { public int lastRemaining(int n, int m) { // 模擬法 List<Integer> l
820. 單詞的壓縮編碼 暴力.. class Solution { public int minimumLengthEncoding(String[] words) { String[] temp = ne
1162. 地圖分析 class Solution { public int maxDistance(int[][] grid) { Queue<Node> queue = new LinkedList<>();
914. 卡牌分組 // 暴力.. class Solution { public boolean hasGroupsSizeX(int[] deck) { int[] temp = new int[10000]
1. 兩數之和 暴力: class Solution { public int[] twoSum(int[] nums, int target) { int[] res = new int[2];
8. 字符串轉換整數 (atoi) class Solution { public int myAtoi(String str) { if (str == null || str.length() == 0 ||
01.07. 旋轉矩陣 簡單粗暴: class Solution { public void rotate(int[][] matrix) { int size = matrix.length;
11. 盛最多水的容器 暴力... class Solution { public int maxArea(int[] height) { int max = 0; for (int i
238. 除自身以外數組的乘積 不能用除法,不能額外空間,所以先用一個數組存第 i 位左邊的所有數乘積,再倒過來遍歷賦值:res = 左邊乘積 * 右邊乘積 class Solution { public int[] pro
7. 整數反轉 class Solution { public int reverse(int x) { long result = 0; while (x != 0) { // 如果是-1
20. 有效的括號 class Solution { public boolean isValid(String s) { Stack<Character> stack = new Stack<>();
208. 實現 Trie (前綴樹) class Trie { //定義節點 class TrieNode { boolean isEnd; TrieNode[] next = new
912. 排序數組 class Solution { public int[] sortArray(int[] nums) { quickSort(nums, 0, nums.length - 1);
999. 可以被一步捕獲的棋子數 class Solution { public int numRookCaptures(char[][] board) { if (board == null) {
給定一個數組,將數組中的元素向右移動 k 個位置,其中 k 是非負數。 示例 1: 輸入: [1,2,3,4,5,6,7] 和 k = 3 輸出: [5,6,7,1,2,3,4] 解釋: 向右旋轉 1 步: [7,1,2,3,4,