From 2eda91e399762f26210eb851cd399b815ec8690c Mon Sep 17 00:00:00 2001 From: yanglbme Date: Sat, 20 Apr 2024 11:39:03 +0800 Subject: [PATCH] feat: add solutions to lc problems: No.190~195 --- .../0100-0199/0190.Reverse Bits/README.md | 60 ++++++++++--------- .../0100-0199/0190.Reverse Bits/README_EN.md | 60 ++++++++++--------- .../0100-0199/0190.Reverse Bits/Solution.cpp | 8 +-- .../0100-0199/0190.Reverse Bits/Solution.go | 9 ++- .../0100-0199/0190.Reverse Bits/Solution.java | 6 +- .../0100-0199/0190.Reverse Bits/Solution.js | 11 ++-- .../0100-0199/0190.Reverse Bits/Solution.py | 6 +- .../0100-0199/0190.Reverse Bits/Solution.rs | 12 ++-- .../0100-0199/0192.Word Frequency/README.md | 11 ++++ .../0192.Word Frequency/README_EN.md | 11 ++++ .../0193.Valid Phone Numbers/README.md | 11 ++++ .../0193.Valid Phone Numbers/README_EN.md | 11 ++++ .../0100-0199/0194.Transpose File/README.md | 25 ++++++++ .../0194.Transpose File/README_EN.md | 25 ++++++++ solution/0100-0199/0195.Tenth Line/README.md | 11 ++++ .../0100-0199/0195.Tenth Line/README_EN.md | 11 ++++ 16 files changed, 205 insertions(+), 83 deletions(-) diff --git a/solution/0100-0199/0190.Reverse Bits/README.md b/solution/0100-0199/0190.Reverse Bits/README.md index 4de5715094753..e0725a1eb4dac 100644 --- a/solution/0100-0199/0190.Reverse Bits/README.md +++ b/solution/0100-0199/0190.Reverse Bits/README.md @@ -49,30 +49,36 @@ ## 解法 -### 方法一 +### 方法一:位运算 + +我们可以从低位到高位,逐个取出 `n` 的每一位,然后将其放到 `ans` 的对应位置上。 + +比如,对于第 $i$ 位,我们可以通过 `(n & 1) << (31 - i)` 来取出 `n` 的第 $i$ 位,并将其放到 `ans` 的第 $31 - i$ 位上,然后将 `n` 右移一位。 + +时间复杂度 $(\log n)$,空间复杂度 $O(1)$。 ```python class Solution: def reverseBits(self, n: int) -> int: - res = 0 + ans = 0 for i in range(32): - res |= (n & 1) << (31 - i) + ans |= (n & 1) << (31 - i) n >>= 1 - return res + return ans ``` ```java public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { - int res = 0; + int ans = 0; for (int i = 0; i < 32 && n != 0; ++i) { - res |= ((n & 1) << (31 - i)); + ans |= (n & 1) << (31 - i); n >>>= 1; } - return res; + return ans; } } ``` @@ -81,36 +87,35 @@ public class Solution { class Solution { public: uint32_t reverseBits(uint32_t n) { - uint32_t res = 0; - for (int i = 0; i < 32; ++i) { - res |= ((n & 1) << (31 - i)); + uint32_t ans = 0; + for (int i = 0; i < 32 && n; ++i) { + ans |= (n & 1) << (31 - i); n >>= 1; } - return res; + return ans; } }; ``` ```go -func reverseBits(num uint32) uint32 { - var ans uint32 = 0 +func reverseBits(n uint32) (ans uint32) { for i := 0; i < 32; i++ { - ans |= (num & 1) << (31 - i) - num >>= 1 + ans |= (n & 1) << (31 - i) + n >>= 1 } - return ans + return } ``` ```rust impl Solution { - pub fn reverse_bits(mut x: u32) -> u32 { - let mut res = 0; - for _ in 0..32 { - res = (res << 1) | (x & 1); - x >>= 1; + pub fn reverse_bits(mut n: u32) -> u32 { + let mut ans = 0; + for i in 0..32 { + ans |= (n & 1) << (31 - i); + n >>= 1; } - res + ans } } ``` @@ -121,13 +126,12 @@ impl Solution { * @return {number} - a positive integer */ var reverseBits = function (n) { - let res = 0; - for (let i = 0; i < 32 && n > 0; ++i) { - res |= (n & 1) << (31 - i); - n >>>= 1; + let ans = 0; + for (let i = 0; i < 32 && n; ++i) { + ans |= (n & 1) << (31 - i); + n >>= 1; } - // 无符号右移 - return res >>> 0; + return ans >>> 0; }; ``` diff --git a/solution/0100-0199/0190.Reverse Bits/README_EN.md b/solution/0100-0199/0190.Reverse Bits/README_EN.md index 6b939da6b61b7..b421afe5e54e9 100644 --- a/solution/0100-0199/0190.Reverse Bits/README_EN.md +++ b/solution/0100-0199/0190.Reverse Bits/README_EN.md @@ -44,30 +44,36 @@ ## Solutions -### Solution 1 +### Solution 1: Bit Manipulation + +We can extract each bit of `n` from the least significant bit to the most significant bit, and then place it in the corresponding position of `ans`. + +For example, for the $i$-th bit, we can use `(n & 1) << (31 - i)` to extract the $i$-th bit of `n` and place it on the $31 - i$-th bit of `ans`, then right shift `n` by one bit. + +The time complexity is $O(\log n)$, and the space complexity is $O(1)$. ```python class Solution: def reverseBits(self, n: int) -> int: - res = 0 + ans = 0 for i in range(32): - res |= (n & 1) << (31 - i) + ans |= (n & 1) << (31 - i) n >>= 1 - return res + return ans ``` ```java public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { - int res = 0; + int ans = 0; for (int i = 0; i < 32 && n != 0; ++i) { - res |= ((n & 1) << (31 - i)); + ans |= (n & 1) << (31 - i); n >>>= 1; } - return res; + return ans; } } ``` @@ -76,36 +82,35 @@ public class Solution { class Solution { public: uint32_t reverseBits(uint32_t n) { - uint32_t res = 0; - for (int i = 0; i < 32; ++i) { - res |= ((n & 1) << (31 - i)); + uint32_t ans = 0; + for (int i = 0; i < 32 && n; ++i) { + ans |= (n & 1) << (31 - i); n >>= 1; } - return res; + return ans; } }; ``` ```go -func reverseBits(num uint32) uint32 { - var ans uint32 = 0 +func reverseBits(n uint32) (ans uint32) { for i := 0; i < 32; i++ { - ans |= (num & 1) << (31 - i) - num >>= 1 + ans |= (n & 1) << (31 - i) + n >>= 1 } - return ans + return } ``` ```rust impl Solution { - pub fn reverse_bits(mut x: u32) -> u32 { - let mut res = 0; - for _ in 0..32 { - res = (res << 1) | (x & 1); - x >>= 1; + pub fn reverse_bits(mut n: u32) -> u32 { + let mut ans = 0; + for i in 0..32 { + ans |= (n & 1) << (31 - i); + n >>= 1; } - res + ans } } ``` @@ -116,13 +121,12 @@ impl Solution { * @return {number} - a positive integer */ var reverseBits = function (n) { - let res = 0; - for (let i = 0; i < 32 && n > 0; ++i) { - res |= (n & 1) << (31 - i); - n >>>= 1; + let ans = 0; + for (let i = 0; i < 32 && n; ++i) { + ans |= (n & 1) << (31 - i); + n >>= 1; } - // 无符号右移 - return res >>> 0; + return ans >>> 0; }; ``` diff --git a/solution/0100-0199/0190.Reverse Bits/Solution.cpp b/solution/0100-0199/0190.Reverse Bits/Solution.cpp index 8447fe17f9d6e..48a6d71afb93d 100644 --- a/solution/0100-0199/0190.Reverse Bits/Solution.cpp +++ b/solution/0100-0199/0190.Reverse Bits/Solution.cpp @@ -1,11 +1,11 @@ class Solution { public: uint32_t reverseBits(uint32_t n) { - uint32_t res = 0; - for (int i = 0; i < 32; ++i) { - res |= ((n & 1) << (31 - i)); + uint32_t ans = 0; + for (int i = 0; i < 32 && n; ++i) { + ans |= (n & 1) << (31 - i); n >>= 1; } - return res; + return ans; } }; \ No newline at end of file diff --git a/solution/0100-0199/0190.Reverse Bits/Solution.go b/solution/0100-0199/0190.Reverse Bits/Solution.go index c8bc6e5377d59..9a5edfaa5038d 100644 --- a/solution/0100-0199/0190.Reverse Bits/Solution.go +++ b/solution/0100-0199/0190.Reverse Bits/Solution.go @@ -1,8 +1,7 @@ -func reverseBits(num uint32) uint32 { - var ans uint32 = 0 +func reverseBits(n uint32) (ans uint32) { for i := 0; i < 32; i++ { - ans |= (num & 1) << (31 - i) - num >>= 1 + ans |= (n & 1) << (31 - i) + n >>= 1 } - return ans + return } \ No newline at end of file diff --git a/solution/0100-0199/0190.Reverse Bits/Solution.java b/solution/0100-0199/0190.Reverse Bits/Solution.java index 906f71f0c76bf..6811e2a6d2b25 100644 --- a/solution/0100-0199/0190.Reverse Bits/Solution.java +++ b/solution/0100-0199/0190.Reverse Bits/Solution.java @@ -1,11 +1,11 @@ public class Solution { // you need treat n as an unsigned value public int reverseBits(int n) { - int res = 0; + int ans = 0; for (int i = 0; i < 32 && n != 0; ++i) { - res |= ((n & 1) << (31 - i)); + ans |= (n & 1) << (31 - i); n >>>= 1; } - return res; + return ans; } } \ No newline at end of file diff --git a/solution/0100-0199/0190.Reverse Bits/Solution.js b/solution/0100-0199/0190.Reverse Bits/Solution.js index db72df44be60d..9c0ddbda04591 100644 --- a/solution/0100-0199/0190.Reverse Bits/Solution.js +++ b/solution/0100-0199/0190.Reverse Bits/Solution.js @@ -3,11 +3,10 @@ * @return {number} - a positive integer */ var reverseBits = function (n) { - let res = 0; - for (let i = 0; i < 32 && n > 0; ++i) { - res |= (n & 1) << (31 - i); - n >>>= 1; + let ans = 0; + for (let i = 0; i < 32 && n; ++i) { + ans |= (n & 1) << (31 - i); + n >>= 1; } - // 无符号右移 - return res >>> 0; + return ans >>> 0; }; diff --git a/solution/0100-0199/0190.Reverse Bits/Solution.py b/solution/0100-0199/0190.Reverse Bits/Solution.py index 2cc985f7a4182..c77a3ed78d57a 100644 --- a/solution/0100-0199/0190.Reverse Bits/Solution.py +++ b/solution/0100-0199/0190.Reverse Bits/Solution.py @@ -1,7 +1,7 @@ class Solution: def reverseBits(self, n: int) -> int: - res = 0 + ans = 0 for i in range(32): - res |= (n & 1) << (31 - i) + ans |= (n & 1) << (31 - i) n >>= 1 - return res + return ans diff --git a/solution/0100-0199/0190.Reverse Bits/Solution.rs b/solution/0100-0199/0190.Reverse Bits/Solution.rs index 3318dba9aef01..b9cb8d72c2ae6 100644 --- a/solution/0100-0199/0190.Reverse Bits/Solution.rs +++ b/solution/0100-0199/0190.Reverse Bits/Solution.rs @@ -1,10 +1,10 @@ impl Solution { - pub fn reverse_bits(mut x: u32) -> u32 { - let mut res = 0; - for _ in 0..32 { - res = (res << 1) | (x & 1); - x >>= 1; + pub fn reverse_bits(mut n: u32) -> u32 { + let mut ans = 0; + for i in 0..32 { + ans |= (n & 1) << (31 - i); + n >>= 1; } - res + ans } } diff --git a/solution/0100-0199/0192.Word Frequency/README.md b/solution/0100-0199/0192.Word Frequency/README.md index af5ec1f2f8d71..7c2598fde3d2f 100644 --- a/solution/0100-0199/0192.Word Frequency/README.md +++ b/solution/0100-0199/0192.Word Frequency/README.md @@ -45,4 +45,15 @@ day 1 ## 解法 +### 方法一:awk + + + +```bash +# Read from the file words.txt and output the word frequency list to stdout. +cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}' +``` + + + diff --git a/solution/0100-0199/0192.Word Frequency/README_EN.md b/solution/0100-0199/0192.Word Frequency/README_EN.md index f5e984defddc7..ba3583c843d81 100644 --- a/solution/0100-0199/0192.Word Frequency/README_EN.md +++ b/solution/0100-0199/0192.Word Frequency/README_EN.md @@ -43,4 +43,15 @@ day 1 ## Solutions +### Solution 1: awk + + + +```bash +# Read from the file words.txt and output the word frequency list to stdout. +cat words.txt | tr -s ' ' '\n' | sort | uniq -c | sort -nr | awk '{print $2, $1}' +``` + + + diff --git a/solution/0100-0199/0193.Valid Phone Numbers/README.md b/solution/0100-0199/0193.Valid Phone Numbers/README.md index 7e659efc828c2..0545fc64bdb8e 100644 --- a/solution/0100-0199/0193.Valid Phone Numbers/README.md +++ b/solution/0100-0199/0193.Valid Phone Numbers/README.md @@ -35,4 +35,15 @@ ## 解法 +### 方法一:awk + + + +```bash +# Read from the file file.txt and output all valid phone numbers to stdout. +awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt +``` + + + diff --git a/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md b/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md index 9dc81e346b900..1f2b95d028863 100644 --- a/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md +++ b/solution/0100-0199/0193.Valid Phone Numbers/README_EN.md @@ -31,4 +31,15 @@ ## Solutions +### Solution 1: awk + + + +```bash +# Read from the file file.txt and output all valid phone numbers to stdout. +awk '/^([0-9]{3}-|\([0-9]{3}\) )[0-9]{3}-[0-9]{4}$/' file.txt +``` + + + diff --git a/solution/0100-0199/0194.Transpose File/README.md b/solution/0100-0199/0194.Transpose File/README.md index 71b6459154da7..30c066f443790 100644 --- a/solution/0100-0199/0194.Transpose File/README.md +++ b/solution/0100-0199/0194.Transpose File/README.md @@ -33,4 +33,29 @@ age 21 30 ## 解法 +### 方法一:awk + + + +```bash +# Read from the file file.txt and print its transposed content to stdout. +awk ' +{ + for (i=1; i<=NF; i++) { + if(NR == 1) { + res[i] = re$i + } else { + res[i] = res[i]" "$i + } + } +}END { + for (i=1;i<=NF;i++) { + print res[i] + } +} +' file.txt +``` + + + diff --git a/solution/0100-0199/0194.Transpose File/README_EN.md b/solution/0100-0199/0194.Transpose File/README_EN.md index 86c700877c0f8..6c5a53986d77c 100644 --- a/solution/0100-0199/0194.Transpose File/README_EN.md +++ b/solution/0100-0199/0194.Transpose File/README_EN.md @@ -29,4 +29,29 @@ age 21 30 ## Solutions +### Solution 1: awk + + + +```bash +# Read from the file file.txt and print its transposed content to stdout. +awk ' +{ + for (i=1; i<=NF; i++) { + if(NR == 1) { + res[i] = re$i + } else { + res[i] = res[i]" "$i + } + } +}END { + for (i=1;i<=NF;i++) { + print res[i] + } +} +' file.txt +``` + + + diff --git a/solution/0100-0199/0195.Tenth Line/README.md b/solution/0100-0199/0195.Tenth Line/README.md index 39ed5e52469b6..9cce5f108f459 100644 --- a/solution/0100-0199/0195.Tenth Line/README.md +++ b/solution/0100-0199/0195.Tenth Line/README.md @@ -37,4 +37,15 @@ Line 10 ## 解法 +### 方法一:sed + + + +```bash +# Read from the file file.txt and output the tenth line to stdout. +sed -n 10p file.txt +``` + + + diff --git a/solution/0100-0199/0195.Tenth Line/README_EN.md b/solution/0100-0199/0195.Tenth Line/README_EN.md index a63a630304f39..8826b8aa08ee3 100644 --- a/solution/0100-0199/0195.Tenth Line/README_EN.md +++ b/solution/0100-0199/0195.Tenth Line/README_EN.md @@ -52,4 +52,15 @@ Line 10 ## Solutions +### Solution 1: sed + + + +```bash +# Read from the file file.txt and output the tenth line to stdout. +sed -n 10p file.txt +``` + + +