Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit db30b17

Browse files
committed
feat: add solutions to lc problems: No.0242,1356
- No.0242.Valid Anagram - No.1356.Sort Integers by The Number of 1 Bits
1 parent 4352b31 commit db30b17

File tree

7 files changed

+162
-0
lines changed

7 files changed

+162
-0
lines changed

‎solution/0200-0299/0242.Valid Anagram/README.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,26 @@ var isAnagram = function (s, t) {
167167
};
168168
```
169169

170+
### **Rust**
171+
172+
```rust
173+
impl Solution {
174+
pub fn is_anagram(s: String, t: String) -> bool {
175+
if s.len() != t.len() {
176+
return false;
177+
}
178+
let (s, t) = (s.as_bytes(), t.as_bytes());
179+
let mut record = [0; 26];
180+
let n = s.len();
181+
for i in 0..n {
182+
record[(s[i] - b'a') as usize] += 1;
183+
record[(t[i] - b'a') as usize] -= 1;
184+
}
185+
record.iter().all(|&c| c == 0)
186+
}
187+
}
188+
```
189+
170190
### **...**
171191

172192
```

‎solution/0200-0299/0242.Valid Anagram/README_EN.md‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,26 @@ var isAnagram = function (s, t) {
148148
};
149149
```
150150

151+
### **Rust**
152+
153+
```rust
154+
impl Solution {
155+
pub fn is_anagram(s: String, t: String) -> bool {
156+
if s.len() != t.len() {
157+
return false;
158+
}
159+
let (s, t) = (s.as_bytes(), t.as_bytes());
160+
let mut record = [0; 26];
161+
let n = s.len();
162+
for i in 0..n {
163+
record[(s[i] - b'a') as usize] += 1;
164+
record[(t[i] - b'a') as usize] -= 1;
165+
}
166+
record.iter().all(|&c| c == 0)
167+
}
168+
}
169+
```
170+
151171
### **...**
152172

153173
```
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
impl Solution {
2+
pub fn is_anagram(s: String, t: String) -> bool {
3+
if s.len() != t.len() {
4+
return false;
5+
}
6+
let (s, t) = (s.as_bytes(), t.as_bytes());
7+
let mut record = [0; 26];
8+
let n = s.len();
9+
for i in 0..n {
10+
record[(s[i] - b'a') as usize] += 1;
11+
record[(t[i] - b'a') as usize] -= 1;
12+
}
13+
record.iter().all(|&c| c == 0)
14+
}
15+
}

‎solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,45 @@ class Solution {
9696
}
9797
```
9898

99+
### **TypeScript**
100+
101+
```ts
102+
function sortByBits(arr: number[]): number[] {
103+
const countOnes = (num: number) => {
104+
let count = 0;
105+
while (num !== 0) {
106+
num &= num - 1;
107+
count++;
108+
}
109+
return count;
110+
};
111+
return arr.sort((a, b) => {
112+
let res = countOnes(a) - countOnes(b);
113+
if (res === 0) {
114+
return a - b;
115+
}
116+
return res;
117+
});
118+
}
119+
```
120+
121+
### **Rust**
122+
123+
```rust
124+
impl Solution {
125+
pub fn sort_by_bits(mut arr: Vec<i32>) -> Vec<i32> {
126+
arr.sort_unstable_by(|a, b| {
127+
let res = a.count_ones().cmp(&b.count_ones());
128+
if res == std::cmp::Ordering::Equal {
129+
return a.cmp(&b);
130+
}
131+
res
132+
});
133+
arr
134+
}
135+
}
136+
```
137+
99138
### **...**
100139

101140
```

‎solution/1300-1399/1356.Sort Integers by The Number of 1 Bits/README_EN.md‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,45 @@ class Solution {
6868
}
6969
```
7070

71+
### **TypeScript**
72+
73+
```ts
74+
function sortByBits(arr: number[]): number[] {
75+
const countOnes = (num: number) => {
76+
let count = 0;
77+
while (num !== 0) {
78+
num &= num - 1;
79+
count++;
80+
}
81+
return count;
82+
};
83+
return arr.sort((a, b) => {
84+
let res = countOnes(a) - countOnes(b);
85+
if (res === 0) {
86+
return a - b;
87+
}
88+
return res;
89+
});
90+
}
91+
```
92+
93+
### **Rust**
94+
95+
```rust
96+
impl Solution {
97+
pub fn sort_by_bits(mut arr: Vec<i32>) -> Vec<i32> {
98+
arr.sort_unstable_by(|a, b| {
99+
let res = a.count_ones().cmp(&b.count_ones());
100+
if res == std::cmp::Ordering::Equal {
101+
return a.cmp(&b);
102+
}
103+
res
104+
});
105+
arr
106+
}
107+
}
108+
```
109+
71110
### **...**
72111

73112
```
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
impl Solution {
2+
pub fn sort_by_bits(mut arr: Vec<i32>) -> Vec<i32> {
3+
arr.sort_unstable_by(|a, b| {
4+
let res = a.count_ones().cmp(&b.count_ones());
5+
if res == std::cmp::Ordering::Equal {
6+
return a.cmp(&b);
7+
}
8+
res
9+
});
10+
arr
11+
}
12+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function sortByBits(arr: number[]): number[] {
2+
const countOnes = (num: number) => {
3+
let count = 0;
4+
while (num !== 0) {
5+
num &= num - 1;
6+
count++;
7+
}
8+
return count;
9+
};
10+
return arr.sort((a, b) => {
11+
let res = countOnes(a) - countOnes(b);
12+
if (res === 0) {
13+
return a - b;
14+
}
15+
return res;
16+
});
17+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /