From d8084d2a1602d2a5c243e9042143f4d539a93c19 Mon Sep 17 00:00:00 2001 From: gameloader Date: Mon, 3 Jun 2024 20:35:01 +0800 Subject: [PATCH] leetcode update --- content/posts/leetcode.md | 118 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 114 insertions(+), 4 deletions(-) diff --git a/content/posts/leetcode.md b/content/posts/leetcode.md index f5b6e52..a08bafe 100644 --- a/content/posts/leetcode.md +++ b/content/posts/leetcode.md @@ -6605,7 +6605,9 @@ func numSteps(s string) int { 本题直接从最后一位开始向前遍历, 遇到0直接将结果加1, 遇到1设定一个标记进位的标记变量为1, 同时将结果加2, 如此重复即可. 注意在每次判断当前位是0还是1之前要将原本的当前位值加上进位的标记变量再判断. ## day94 2024-05-30 -### 1442. Count Triplets That Can Form Two Arrays of Equal XOR + +### 1442. Count Triplets That Can Form Two Arrays of Equal XOR + Given an array of integers arr. We want to select three indices i, j and k where (0 <= i < j <= k < arr.length). @@ -6618,14 +6620,15 @@ Note that ^ denotes the bitwise-xor operation. Return the number of triplets (i, j and k) Where a == b. -![0530uVcNw3JX4b49](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0530uVcNw3JX4b49.png) +![0530uVcNw3JX4b49](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0530uVcNw3JX4b49.png) ### 题解 -题目中要求i-j和j-k之间的数组元素全部异或后的值相等, 而i> i) & 1) == 1{ + bit = i + } + } + result1 := 0 + result2 := 0 + for _, value := range nums{ + if ((value >> bit) & 1) == 1{ + result1 = result1 ^ value + }else{ + result2 = result2 ^ value + } + } + return []int{result1, result2} +} +``` + +## day96 2024-06-01 + +### 3110. Score of a String + +You are given a string s. The score of a string is defined as the sum of the absolute difference between the ASCII values of adjacent characters. + +Return the score of s. + +![0601IP0RDAtOoMHP](https://testingcf.jsdelivr.net/gh/game-loader/picbase@master/uPic/0601IP0RDAtOoMHP.png) + +### 题解 + +比较基础, 按照题目要求计算相邻字符的差的绝对值之和即可 + +### 代码 + +```go +func scoreOfString(s string) int { + result := 0 + for i,_ := range s[1:]{ + if s[i] < s[i+1]{ + result += int(s[i+1] - s[i]) + }else{ + result += int(s[i] - s[i+1]) + } + } + return result +} +``` + +## day97 2024-06-02 + +### 344. Reverse String + +Write a function that reverses a string. The input string is given as an array of characters s. + +You must do this by modifying the input array in-place with O(1) extra memory. + +Example 1: + +Input: s = ["h","e","l","l","o"] +Output: ["o","l","l","e","h"] + +Example 2: + +Input: s = ["H","a","n","n","a","h"] +Output: ["h","a","n","n","a","H"] + +### 题解 + +将字符串逆转也是一道简单题, 要求只能使用常数额外空间且直接在原始字符串上逆转, 因为原始字符串以数组形式保存, 可以分别从首尾开始向中间遍历数组并交换首尾的字符即可完成逆转操作. + +### 代码 + +```go +func reverseString(s []byte) { + length := len(s) - 1 + for i:=0;0+i <= length-i;i++{ + s[i], s[length-i] = s[length-i], s[i] + } +} +```