Skip to content

Commit a4ff8bb

Browse files
committed
solve problems
1 parent 4f20ca0 commit a4ff8bb

10 files changed

+252
-4
lines changed

src/problem/mod.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +0,0 @@
1-
mod p0447_number_of_boomerangs;
2-
mod p0448_find_all_numbers_disappeared_in_an_array;
3-
mod p0453_minimum_moves_to_equal_array_elements;
4-
mod p0455_assign_cookies;

src/solution/mod.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -256,3 +256,11 @@ mod s0704_binary_search;
256256
mod s0969_pancake_sorting;
257257
mod s1018_binary_prefix_divisible_by_5;
258258
mod s1046_last_stone_weight;
259+
mod s0459_repeated_substring_pattern;
260+
mod s0461_hamming_distance;
261+
mod s0447_number_of_boomerangs;
262+
mod s0448_find_all_numbers_disappeared_in_an_array;
263+
mod s0453_minimum_moves_to_equal_array_elements;
264+
mod s0455_assign_cookies;
265+
mod s0463_island_perimeter;
266+
mod s0476_number_complement;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* [459] Repeated Substring Pattern
3+
*
4+
* Given a non-empty string check if it can be constructed by taking a substring of it and appending multiple copies of the substring together. You may assume the given string consists of lowercase English letters only and its length will not exceed 10000.
5+
*
6+
*
7+
*
8+
* Example 1:
9+
*
10+
*
11+
* Input: "abab"
12+
* Output: True
13+
* Explanation: It's the substring "ab" twice.
14+
*
15+
*
16+
* Example 2:
17+
*
18+
*
19+
* Input: "aba"
20+
* Output: False
21+
*
22+
*
23+
* Example 3:
24+
*
25+
*
26+
* Input: "abcabcabcabc"
27+
* Output: True
28+
* Explanation: It's the substring "abc" four times. (And the substring "abcabc" twice.)
29+
*
30+
*
31+
*/
32+
pub struct Solution {}
33+
34+
// submission codes start here
35+
36+
impl Solution {
37+
pub fn repeated_substring_pattern(s: String) -> bool {
38+
let chars: Vec<char> = s.chars().collect();
39+
let mut pattern_len = s.len() / 2;
40+
while pattern_len >= 1 {
41+
if s.len() % pattern_len == 0 {
42+
let substring = &s[0..pattern_len];
43+
let mut start = 0;
44+
while start + pattern_len <= s.len() {
45+
if &s[start..start + pattern_len] != substring {
46+
break;
47+
}
48+
start += pattern_len;
49+
}
50+
if start == s.len() {
51+
return true;
52+
}
53+
}
54+
pattern_len -= 1;
55+
}
56+
false
57+
}
58+
}
59+
60+
// submission codes end
61+
62+
#[cfg(test)]
63+
mod tests {
64+
use super::*;
65+
66+
#[test]
67+
fn test_459() {}
68+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* [461] Hamming Distance
3+
*
4+
* The <a href="https://en.wikipedia.org/wiki/Hamming_distance" target="_blank">Hamming distance</a> between two integers is the number of positions at which the corresponding bits are different.
5+
*
6+
* Given two integers x and y, calculate the Hamming distance.
7+
*
8+
* Note:<br />
9+
* 0 &le; x, y < 2^31.
10+
*
11+
*
12+
* Example:
13+
*
14+
* Input: x = 1, y = 4
15+
*
16+
* Output: 2
17+
*
18+
* Explanation:
19+
* 1 (0 0 0 1)
20+
* 4 (0 1 0 0)
21+
* &uarr; &uarr;
22+
*
23+
* The above arrows point to positions where the corresponding bits are different.
24+
*
25+
*
26+
*/
27+
pub struct Solution {}
28+
29+
// submission codes start here
30+
31+
impl Solution {
32+
pub fn hamming_distance(x: i32, y: i32) -> i32 {
33+
let mut ret = 0;
34+
let mut x = x;
35+
let mut y = y;
36+
while x != 0 || y != 0 {
37+
if x & 1 != y & 1 {
38+
ret += 1;
39+
}
40+
x >>= 1;
41+
y >>= 1;
42+
}
43+
ret
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn test_461() {}
55+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/**
2+
* [463] Island Perimeter
3+
*
4+
* You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water.
5+
*
6+
* Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells).
7+
*
8+
* The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.
9+
*
10+
*
11+
*
12+
* Example:
13+
*
14+
*
15+
* Input:
16+
* [[0,1,0,0],
17+
* [1,1,1,0],
18+
* [0,1,0,0],
19+
* [1,1,0,0]]
20+
*
21+
* Output: 16
22+
*
23+
* Explanation: The perimeter is the 16 yellow stripes in the image below:
24+
*
25+
* <img src="https://assets.leetcode.com/uploads/2018/10/12/island.png" style="width: 221px; height: 213px;" />
26+
*
27+
*
28+
*/
29+
pub struct Solution {}
30+
31+
// submission codes start here
32+
33+
impl Solution {
34+
pub fn island_perimeter(grid: Vec<Vec<i32>>) -> i32 {
35+
let mut ret = 0;
36+
for i in 0..grid.len() {
37+
for j in 0..grid[0].len() {
38+
if grid[i][j] == 1 {
39+
if i == 0 || grid[i - 1][j] == 0 {
40+
ret += 1;
41+
}
42+
if i == grid.len() - 1 || grid[i + 1][j] == 0 {
43+
ret += 1;
44+
}
45+
if j == 0 || grid[i][j - 1] == 0 {
46+
ret += 1;
47+
}
48+
if j == grid[0].len() - 1 || grid[i][j + 1] == 0 {
49+
ret += 1;
50+
}
51+
}
52+
}
53+
}
54+
ret
55+
}
56+
}
57+
58+
// submission codes end
59+
60+
#[cfg(test)]
61+
mod tests {
62+
use super::*;
63+
64+
#[test]
65+
fn test_463() {}
66+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* [476] Number Complement
3+
*
4+
* Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation.
5+
*
6+
* Note:<br>
7+
* <ol>
8+
* The given integer is guaranteed to fit within the range of a 32-bit signed integer.
9+
* You could assume no leading zero bit in the integer’s binary representation.
10+
* </ol>
11+
*
12+
*
13+
* Example 1:<br />
14+
*
15+
* Input: 5
16+
* Output: 2
17+
* Explanation: The binary representation of 5 is 101 (no leading zero bits), and its complement is 010. So you need to output 2.
18+
*
19+
*
20+
*
21+
* Example 2:<br />
22+
*
23+
* Input: 1
24+
* Output: 0
25+
* Explanation: The binary representation of 1 is 1 (no leading zero bits), and its complement is 0. So you need to output 0.
26+
*
27+
*
28+
*/
29+
pub struct Solution {}
30+
31+
// submission codes start here
32+
33+
impl Solution {
34+
pub fn find_complement(num: i32) -> i32 {
35+
let mut num = num;
36+
let mut ret = 0;
37+
let mut offset = 0;
38+
while num != 0 {
39+
ret |= (1 - (num & 1)) << offset;
40+
num >>= 1;
41+
offset += 1;
42+
}
43+
ret
44+
}
45+
}
46+
47+
// submission codes end
48+
49+
#[cfg(test)]
50+
mod tests {
51+
use super::*;
52+
53+
#[test]
54+
fn test_476() {}
55+
}

0 commit comments

Comments
 (0)