diff --git a/solution/0200-0299/0266.Palindrome Permutation/README.md b/solution/0200-0299/0266.Palindrome Permutation/README.md index d13f351cc65f9..a4be9414ffc0e 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/README.md +++ b/solution/0200-0299/0266.Palindrome Permutation/README.md @@ -31,7 +31,7 @@ 创建一个长度为 $26$ 的数组,统计每个字母出现的频率,至多有一个字符出现奇数次数即可。 -时间复杂度 $O(n)$,空间复杂度 $O(26)$。其中 $n$ 是字符串的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma|=26$。 **方法二:哈希表** @@ -39,7 +39,7 @@ 遍历结束,若哈希表中元素个数不超过 $1$,则返回 $true$,否则返回 $false$。 -时间复杂度 $O(n)$,空间复杂度 $O(26)$。其中 $n$ 是字符串的长度。 +时间复杂度 $O(n)$,空间复杂度 $O(|\Sigma|)$。其中 $n$ 是字符串的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma|=26$。 @@ -50,7 +50,7 @@ ```python class Solution: def canPermutePalindrome(self, s: str) -> bool: - return sum(v % 2 for v in Counter(s).values()) <= 1 + return sum(v & 1 for v in Counter(s).values()) < 2 ``` ### **Java** @@ -64,11 +64,11 @@ class Solution { for (char c : s.toCharArray()) { ++cnt[c - 'a']; } - int n = 0; - for (int v : cnt) { - n += v % 2; + int odd = 0; + for (int x : cnt) { + odd += x & 1; } - return n < 2; + return odd < 2; } } ``` @@ -80,10 +80,14 @@ class Solution { public: bool canPermutePalindrome(string s) { vector cnt(26); - for (char& c : s) ++cnt[c - 'a']; - int n = 0; - for (int& v : cnt) n += v & 1; - return n < 2; + for (char& c : s) { + ++cnt[c - 'a']; + } + int odd = 0; + for (int x : cnt) { + odd += x & 1; + } + return odd < 2; } }; ``` @@ -92,15 +96,27 @@ public: ```go func canPermutePalindrome(s string) bool { - cnt := make([]int, 26) + cnt := [26]int{} for _, c := range s { cnt[c-'a']++ } - n := 0 - for _, v := range cnt { - n += v & 1 + odd := 0 + for _, x := range cnt { + odd += x & 1 } - return n < 2 + return odd < 2 +} +``` + +### **TypeScript** + +```ts +function canPermutePalindrome(s: string): boolean { + const cnt: number[] = new Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + return cnt.filter(c => c % 2 === 1).length < 2; } ``` @@ -112,15 +128,11 @@ func canPermutePalindrome(s string) bool { * @return {boolean} */ var canPermutePalindrome = function (s) { - let ss = new Set(); - for (let c of s) { - if (ss.has(c)) { - ss.delete(c); - } else { - ss.add(c); - } + const cnt = new Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; } - return ss.size < 2; + return cnt.filter(c => c % 2 === 1).length < 2; }; ``` diff --git a/solution/0200-0299/0266.Palindrome Permutation/README_EN.md b/solution/0200-0299/0266.Palindrome Permutation/README_EN.md index c9af9a45b87ca..3468bf07d97f7 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/README_EN.md +++ b/solution/0200-0299/0266.Palindrome Permutation/README_EN.md @@ -45,7 +45,7 @@ ```python class Solution: def canPermutePalindrome(self, s: str) -> bool: - return sum(v % 2 for v in Counter(s).values()) <= 1 + return sum(v & 1 for v in Counter(s).values()) < 2 ``` ### **Java** @@ -57,11 +57,11 @@ class Solution { for (char c : s.toCharArray()) { ++cnt[c - 'a']; } - int n = 0; - for (int v : cnt) { - n += v % 2; + int odd = 0; + for (int x : cnt) { + odd += x & 1; } - return n < 2; + return odd < 2; } } ``` @@ -73,10 +73,14 @@ class Solution { public: bool canPermutePalindrome(string s) { vector cnt(26); - for (char& c : s) ++cnt[c - 'a']; - int n = 0; - for (int& v : cnt) n += v & 1; - return n < 2; + for (char& c : s) { + ++cnt[c - 'a']; + } + int odd = 0; + for (int x : cnt) { + odd += x & 1; + } + return odd < 2; } }; ``` @@ -85,15 +89,27 @@ public: ```go func canPermutePalindrome(s string) bool { - cnt := make([]int, 26) + cnt := [26]int{} for _, c := range s { cnt[c-'a']++ } - n := 0 - for _, v := range cnt { - n += v & 1 + odd := 0 + for _, x := range cnt { + odd += x & 1 } - return n < 2 + return odd < 2 +} +``` + +### **TypeScript** + +```ts +function canPermutePalindrome(s: string): boolean { + const cnt: number[] = new Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + return cnt.filter(c => c % 2 === 1).length < 2; } ``` @@ -105,15 +121,11 @@ func canPermutePalindrome(s string) bool { * @return {boolean} */ var canPermutePalindrome = function (s) { - let ss = new Set(); - for (let c of s) { - if (ss.has(c)) { - ss.delete(c); - } else { - ss.add(c); - } + const cnt = new Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; } - return ss.size < 2; + return cnt.filter(c => c % 2 === 1).length < 2; }; ``` diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.cpp b/solution/0200-0299/0266.Palindrome Permutation/Solution.cpp index ff7752148a7a0..9a2828859f2e3 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/Solution.cpp +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.cpp @@ -1,10 +1,14 @@ -class Solution { -public: - bool canPermutePalindrome(string s) { - vector cnt(26); - for (char& c : s) ++cnt[c - 'a']; - int n = 0; - for (int& v : cnt) n += v & 1; - return n < 2; - } +class Solution { +public: + bool canPermutePalindrome(string s) { + vector cnt(26); + for (char& c : s) { + ++cnt[c - 'a']; + } + int odd = 0; + for (int x : cnt) { + odd += x & 1; + } + return odd < 2; + } }; \ No newline at end of file diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.go b/solution/0200-0299/0266.Palindrome Permutation/Solution.go index 6394d1773a7b7..36d61033b54c6 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/Solution.go +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.go @@ -1,11 +1,11 @@ func canPermutePalindrome(s string) bool { - cnt := make([]int, 26) + cnt := [26]int{} for _, c := range s { cnt[c-'a']++ } - n := 0 - for _, v := range cnt { - n += v & 1 + odd := 0 + for _, x := range cnt { + odd += x & 1 } - return n < 2 + return odd < 2 } \ No newline at end of file diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.java b/solution/0200-0299/0266.Palindrome Permutation/Solution.java index f94a9483f1e7d..b22bfecff0c8e 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/Solution.java +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.java @@ -1,13 +1,13 @@ -class Solution { - public boolean canPermutePalindrome(String s) { - int[] cnt = new int[26]; - for (char c : s.toCharArray()) { - ++cnt[c - 'a']; - } - int n = 0; - for (int v : cnt) { - n += v % 2; - } - return n < 2; - } +class Solution { + public boolean canPermutePalindrome(String s) { + int[] cnt = new int[26]; + for (char c : s.toCharArray()) { + ++cnt[c - 'a']; + } + int odd = 0; + for (int x : cnt) { + odd += x & 1; + } + return odd < 2; + } } \ No newline at end of file diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.js b/solution/0200-0299/0266.Palindrome Permutation/Solution.js index 0a37dc97c9781..346a9ee9421a0 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/Solution.js +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.js @@ -3,13 +3,9 @@ * @return {boolean} */ var canPermutePalindrome = function (s) { - let ss = new Set(); - for (let c of s) { - if (ss.has(c)) { - ss.delete(c); - } else { - ss.add(c); - } + const cnt = new Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt() - 'a'.charCodeAt()]; } - return ss.size < 2; + return cnt.filter(c => c % 2 === 1).length < 2; }; diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.py b/solution/0200-0299/0266.Palindrome Permutation/Solution.py index 7ed254c310937..34a2e7ee1b592 100644 --- a/solution/0200-0299/0266.Palindrome Permutation/Solution.py +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.py @@ -1,3 +1,3 @@ -class Solution: - def canPermutePalindrome(self, s: str) -> bool: - return sum(v % 2 for v in Counter(s).values()) <= 1 +class Solution: + def canPermutePalindrome(self, s: str) -> bool: + return sum(v & 1 for v in Counter(s).values()) < 2 diff --git a/solution/0200-0299/0266.Palindrome Permutation/Solution.ts b/solution/0200-0299/0266.Palindrome Permutation/Solution.ts new file mode 100644 index 0000000000000..5edeb264d7743 --- /dev/null +++ b/solution/0200-0299/0266.Palindrome Permutation/Solution.ts @@ -0,0 +1,7 @@ +function canPermutePalindrome(s: string): boolean { + const cnt: number[] = new Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + return cnt.filter(c => c % 2 === 1).length < 2; +} diff --git a/solution/2500-2599/2525.Categorize Box According to Criteria/README.md b/solution/2500-2599/2525.Categorize Box According to Criteria/README.md index 752020f2aaba4..2d74d6cb0f3b2 100644 --- a/solution/2500-2599/2525.Categorize Box According to Criteria/README.md +++ b/solution/2500-2599/2525.Categorize Box According to Criteria/README.md @@ -134,6 +134,32 @@ func categorizeBox(length int, width int, height int, mass int) string { } ``` +### **TypeScript** + +```ts +function categorizeBox( + length: number, + width: number, + height: number, + mass: number, +): string { + const v = length * width * height; + let i = 0; + if ( + length >= 10000 || + width >= 10000 || + height >= 10000 || + v >= 1000000000 + ) { + i |= 1; + } + if (mass >= 100) { + i |= 2; + } + return ['Neither', 'Bulky', 'Heavy', 'Both'][i]; +} +``` + ### **Rust** ```rust diff --git a/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md b/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md index b79afa108f105..565e6c519c92d 100644 --- a/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md +++ b/solution/2500-2599/2525.Categorize Box According to Criteria/README_EN.md @@ -118,6 +118,32 @@ func categorizeBox(length int, width int, height int, mass int) string { } ``` +### **TypeScript** + +```ts +function categorizeBox( + length: number, + width: number, + height: number, + mass: number, +): string { + const v = length * width * height; + let i = 0; + if ( + length >= 10000 || + width >= 10000 || + height >= 10000 || + v >= 1000000000 + ) { + i |= 1; + } + if (mass >= 100) { + i |= 2; + } + return ['Neither', 'Bulky', 'Heavy', 'Both'][i]; +} +``` + ### **Rust** ```rust diff --git a/solution/2500-2599/2525.Categorize Box According to Criteria/Solution.ts b/solution/2500-2599/2525.Categorize Box According to Criteria/Solution.ts new file mode 100644 index 0000000000000..1d1c384d8a28d --- /dev/null +++ b/solution/2500-2599/2525.Categorize Box According to Criteria/Solution.ts @@ -0,0 +1,21 @@ +function categorizeBox( + length: number, + width: number, + height: number, + mass: number, +): string { + const v = length * width * height; + let i = 0; + if ( + length >= 10000 || + width >= 10000 || + height >= 10000 || + v >= 1000000000 + ) { + i |= 1; + } + if (mass >= 100) { + i |= 2; + } + return ['Neither', 'Bulky', 'Heavy', 'Both'][i]; +} diff --git a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md index 2b689cbcefc6a..64846b89f256a 100644 --- a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md +++ b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README.md @@ -162,6 +162,33 @@ func (this *DataStream) Consec(num int) bool { */ ``` +### **TypeScript** + +```ts +class DataStream { + private val: number; + private k: number; + private cnt: number; + + constructor(value: number, k: number) { + this.val = value; + this.k = k; + this.cnt = 0; + } + + consec(num: number): boolean { + this.cnt = this.val === num ? this.cnt + 1 : 0; + return this.cnt >= this.k; + } +} + +/** + * Your DataStream object will be instantiated and called as such: + * var obj = new DataStream(value, k) + * var param_1 = obj.consec(num) + */ +``` + ### **...** ``` diff --git a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md index 944129a70977a..9256f18c6fa08 100644 --- a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md +++ b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/README_EN.md @@ -144,6 +144,33 @@ func (this *DataStream) Consec(num int) bool { */ ``` +### **TypeScript** + +```ts +class DataStream { + private val: number; + private k: number; + private cnt: number; + + constructor(value: number, k: number) { + this.val = value; + this.k = k; + this.cnt = 0; + } + + consec(num: number): boolean { + this.cnt = this.val === num ? this.cnt + 1 : 0; + return this.cnt >= this.k; + } +} + +/** + * Your DataStream object will be instantiated and called as such: + * var obj = new DataStream(value, k) + * var param_1 = obj.consec(num) + */ +``` + ### **...** ``` diff --git a/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/Solution.ts b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/Solution.ts new file mode 100644 index 0000000000000..e74e61d2efc0a --- /dev/null +++ b/solution/2500-2599/2526.Find Consecutive Integers from a Data Stream/Solution.ts @@ -0,0 +1,22 @@ +class DataStream { + private val: number; + private k: number; + private cnt: number; + + constructor(value: number, k: number) { + this.val = value; + this.k = k; + this.cnt = 0; + } + + consec(num: number): boolean { + this.cnt = this.val === num ? this.cnt + 1 : 0; + return this.cnt >= this.k; + } +} + +/** + * Your DataStream object will be instantiated and called as such: + * var obj = new DataStream(value, k) + * var param_1 = obj.consec(num) + */ diff --git a/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md b/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md index 9459f6fe8a187..7db0d75f7a956 100644 --- a/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md +++ b/solution/2500-2599/2527.Find Xor-Beauty of Array/README.md @@ -125,6 +125,14 @@ func xorBeauty(nums []int) (ans int) { } ``` +### **TypeScript** + +```ts +function xorBeauty(nums: number[]): number { + return nums.reduce((acc, cur) => acc ^ cur, 0); +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md b/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md index e1b5f191fa094..46be8cc14ed62 100644 --- a/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md +++ b/solution/2500-2599/2527.Find Xor-Beauty of Array/README_EN.md @@ -105,6 +105,14 @@ func xorBeauty(nums []int) (ans int) { } ``` +### **TypeScript** + +```ts +function xorBeauty(nums: number[]): number { + return nums.reduce((acc, cur) => acc ^ cur, 0); +} +``` + ### **...** ``` diff --git a/solution/2500-2599/2527.Find Xor-Beauty of Array/Solution.ts b/solution/2500-2599/2527.Find Xor-Beauty of Array/Solution.ts new file mode 100644 index 0000000000000..f2259a296816f --- /dev/null +++ b/solution/2500-2599/2527.Find Xor-Beauty of Array/Solution.ts @@ -0,0 +1,3 @@ +function xorBeauty(nums: number[]): number { + return nums.reduce((acc, cur) => acc ^ cur, 0); +}