Compare commits
93
Commits
aa1f5ac8c1
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
551db6decf | ||
|
|
c1fe8fd658 | ||
|
|
23a4622f11 | ||
|
|
ebd3f9445c | ||
|
|
50fba25511 | ||
|
|
ece3af5149 | ||
|
|
a19c7573a7 | ||
|
|
fe0577dd06 | ||
|
|
fcf3f21a52 | ||
|
|
cb73a780cf | ||
|
|
86ec2ee252 | ||
|
|
fc8857537e | ||
|
|
8cc7ce864c | ||
|
|
b88bfb137b
|
||
|
|
985c978958
|
||
|
|
25645920ff
|
||
|
|
44ce8b9fd4
|
||
|
|
afa91fe380
|
||
|
|
6e59cabd84
|
||
|
|
00bc762b5e
|
||
|
|
9452c4e658 | ||
|
|
a9da2f1227
|
||
|
|
fa937a142f
|
||
|
|
6213d83856
|
||
|
|
d0da066c58
|
||
|
|
4460a2f028
|
||
|
|
4ee2acd245
|
||
|
|
6074be16ce
|
||
|
|
11a7d6775d
|
||
|
|
93d4b712d5
|
||
|
|
a467de459b
|
||
|
|
653e68c8f1
|
||
|
|
671347dfde
|
||
|
|
023fe9ec14
|
||
|
|
a6ca63686a
|
||
|
|
283b4c1518
|
||
|
|
444863b22d
|
||
|
|
8cac824082
|
||
|
|
8cc2ab780c
|
||
|
|
98eafcac0e
|
||
|
|
ecb53e3f3a
|
||
|
|
cca3dc4a88
|
||
|
|
8a381984c9 | ||
|
|
d7f5b98838 | ||
|
|
f1f47604f8 | ||
|
|
60447f6bf8 | ||
|
|
b0641080b5 | ||
|
|
bb6a7dc773 | ||
|
|
bb640829ee | ||
|
|
6c8fa06ce1 | ||
|
|
143ff15f87 | ||
|
|
d5eafc86f4 | ||
|
|
f91a4b2fe6 | ||
|
|
3d72b848d2 | ||
|
|
bde9d0f8b2 | ||
|
|
e615d4f611 | ||
|
|
f80a39e514 | ||
|
|
bd8207767a | ||
|
|
c18ad8c1b0 | ||
|
|
566c6a8414 | ||
|
|
f8fd02dcb8 | ||
|
|
563c0b3589 | ||
|
|
1772ba6c99 | ||
|
|
495075eef2 | ||
|
|
0f894b29a8 | ||
|
|
54bea679f0 | ||
|
|
873bf741e6 | ||
|
|
7efe4d98bb | ||
|
|
0f8400d0c9 | ||
|
|
9a82ec82f7 | ||
|
|
376065acb6 | ||
|
|
cd8b2b31e3 | ||
|
|
08f6ce4581 | ||
|
|
702b016368 | ||
|
|
1da8b6b735 | ||
|
|
0babcf1472 | ||
|
|
9648737245 | ||
|
|
93577c7ff2 | ||
|
|
4b8594e8e3 | ||
|
|
67d79e3553 | ||
|
|
30e2df43c4 | ||
|
|
8c281d865f | ||
|
|
742a5b6137 | ||
|
|
fc7f57b9ca | ||
|
|
941d05d9ee | ||
|
|
c54a750759 | ||
|
|
4f601269e8 | ||
|
|
292f597d2b | ||
|
|
8a581be8b5 | ||
|
|
b8cc12db71 | ||
|
|
f0bfce2ee6 | ||
|
|
6d6b8f6f3d | ||
|
|
60f0b5cd02 |
+186
-7
@@ -1,3 +1,9 @@
|
||||
pub fn make_string_arr(s: &str) -> Vec<String> {
|
||||
s.trim_matches(&['[', ']'][..])
|
||||
.split(',')
|
||||
.map(|s| s.trim_matches(&['"',' ']).to_string())
|
||||
.collect()
|
||||
}
|
||||
pub fn make_arr(s: &str) -> Vec<i32> {
|
||||
s.trim_matches(&['[', ']'][..])
|
||||
.split(',')
|
||||
@@ -13,13 +19,186 @@ pub fn make_arr(s: &str) -> Vec<i32> {
|
||||
}
|
||||
|
||||
pub fn make_matrix(s: &str) -> Vec<Vec<i32>> {
|
||||
s.trim_matches(&['[', ']'][..])
|
||||
.split("],[")
|
||||
.map(|row| {
|
||||
row.trim_matches(&['[', ']'][..])
|
||||
let s = s.trim();
|
||||
|
||||
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
.trim();
|
||||
|
||||
if inner.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let bytes = inner.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut rows: Vec<Vec<i32>> = Vec::new();
|
||||
while i < bytes.len() {
|
||||
// find next '['
|
||||
while i < bytes.len() && bytes[i] != b'[' {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() {
|
||||
break;
|
||||
}
|
||||
let start = i + 1; // byte index after '['
|
||||
i = start;
|
||||
// find matching ']'
|
||||
while i < bytes.len() && bytes[i] != b']' {
|
||||
i += 1;
|
||||
}
|
||||
let end = i; // exclusive
|
||||
// move i past ']' for next iteration (if in bounds)
|
||||
if i < bytes.len() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
// empty row like []
|
||||
rows.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let row_str = &inner[start..end];
|
||||
let row_vec: Vec<i32> = row_str
|
||||
.split(',')
|
||||
.map(|x| x.trim().parse::<i32>().unwrap())
|
||||
.collect()
|
||||
.map(|x| {
|
||||
let x = x.trim();
|
||||
if x == "null" || x.is_empty() {
|
||||
-1
|
||||
} else {
|
||||
x.parse::<i32>().unwrap()
|
||||
}
|
||||
})
|
||||
.collect()
|
||||
.collect();
|
||||
rows.push(row_vec);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
|
||||
pub fn make_char_matrix(s: &str) -> Vec<Vec<char>> {
|
||||
let s = s.trim();
|
||||
|
||||
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
.trim();
|
||||
|
||||
if inner.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let bytes = inner.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut rows: Vec<Vec<char>> = Vec::new();
|
||||
while i < bytes.len() {
|
||||
// find next '['
|
||||
while i < bytes.len() && bytes[i] != b'[' {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() {
|
||||
break;
|
||||
}
|
||||
let start = i + 1; // byte index after '['
|
||||
i = start;
|
||||
// find matching ']'
|
||||
while i < bytes.len() && bytes[i] != b']' {
|
||||
i += 1;
|
||||
}
|
||||
let end = i; // exclusive
|
||||
// move i past ']' for next iteration (if in bounds)
|
||||
if i < bytes.len() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
// empty row like []
|
||||
rows.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let row_str = &inner[start..end];
|
||||
let row_vec: Vec<char> = row_str
|
||||
.split(',')
|
||||
.map(|x| {
|
||||
let x = x.trim().trim_matches('"');
|
||||
if x == "null" || x.is_empty() {
|
||||
' '
|
||||
} else {
|
||||
x.to_string().chars().nth(0).unwrap()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
rows.push(row_vec);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
pub fn make_string_matrix(s: &str) -> Vec<Vec<String>> {
|
||||
let s = s.trim();
|
||||
|
||||
// Remove only the outermost pair of brackets if present, e.g. "[[1,2],[3,4]]" -> "[1,2],[3,4]"
|
||||
let inner = if s.len() >= 2 && s.starts_with('[') && s.ends_with(']') {
|
||||
&s[1..s.len() - 1]
|
||||
} else {
|
||||
s
|
||||
}
|
||||
.trim();
|
||||
|
||||
if inner.is_empty() {
|
||||
return Vec::new();
|
||||
}
|
||||
|
||||
let bytes = inner.as_bytes();
|
||||
let mut i = 0usize;
|
||||
let mut rows: Vec<Vec<String>> = Vec::new();
|
||||
while i < bytes.len() {
|
||||
// find next '['
|
||||
while i < bytes.len() && bytes[i] != b'[' {
|
||||
i += 1;
|
||||
}
|
||||
if i >= bytes.len() {
|
||||
break;
|
||||
}
|
||||
let start = i + 1; // byte index after '['
|
||||
i = start;
|
||||
// find matching ']'
|
||||
while i < bytes.len() && bytes[i] != b']' {
|
||||
i += 1;
|
||||
}
|
||||
let end = i; // exclusive
|
||||
// move i past ']' for next iteration (if in bounds)
|
||||
if i < bytes.len() {
|
||||
i += 1;
|
||||
}
|
||||
|
||||
if end <= start {
|
||||
// empty row like []
|
||||
rows.push(Vec::new());
|
||||
continue;
|
||||
}
|
||||
|
||||
let row_str = &inner[start..end];
|
||||
let row_vec: Vec<String> = row_str
|
||||
.split(',')
|
||||
.map(|x| {
|
||||
let x = x.trim().trim_matches('"');
|
||||
if x == "null" || x.is_empty() {
|
||||
"".to_string()
|
||||
} else {
|
||||
x.to_string()
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
rows.push(row_vec);
|
||||
}
|
||||
|
||||
rows
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
// Definition for singly-linked list.
|
||||
#[derive(PartialEq, Eq, Clone, Debug)]
|
||||
pub struct ListNode {
|
||||
pub val: i32,
|
||||
pub next: Option<Box<ListNode>>
|
||||
}
|
||||
|
||||
impl ListNode {
|
||||
#[inline]
|
||||
fn new(val: i32) -> Self {
|
||||
ListNode {
|
||||
next: None,
|
||||
val
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn make_list(arr: Vec<i32>) -> Option<Box<ListNode>> {
|
||||
let mut head: Option<Box<ListNode>> = None;
|
||||
let mut current = &mut head;
|
||||
|
||||
for &value in arr.iter() {
|
||||
let new_node = Box::new(ListNode::new(value));
|
||||
if current.is_none() {
|
||||
*current = Some(new_node);
|
||||
} else {
|
||||
current.as_mut().unwrap().next = Some(new_node);
|
||||
current = &mut current.as_mut().unwrap().next;
|
||||
}
|
||||
}
|
||||
|
||||
head
|
||||
}
|
||||
+20
-45
@@ -1,54 +1,29 @@
|
||||
mod arr;
|
||||
struct Solution {}
|
||||
|
||||
impl Solution {
|
||||
pub fn dfs(heights: &Vec<Vec<i32>>,arr: &mut Vec<Vec<bool>>,i:usize,j:usize ) {
|
||||
let dirs = vec![(0,1),(0,-1),(1,0),(-1,0)];
|
||||
for dir in dirs {
|
||||
let (x,y) = (i as i32 + dir.0, j as i32 + dir.1);
|
||||
if x>=0 && x<heights.len() as i32 && y>=0 && y<heights[0].len() as i32 && !arr[x as usize][y as usize] && heights[x as usize][y as usize]>=heights[i][j] {
|
||||
arr[x as usize][y as usize] = true;
|
||||
Self::dfs(heights,arr,x as usize,y as usize);
|
||||
pub fn min_distance(word1: String, word2: String) -> i32 {
|
||||
let mut dp = vec![vec![0; word2.len() + 1]; word1.len() + 1];
|
||||
let word1 = word1.chars().collect::<Vec<char>>();
|
||||
let word2 = word2.chars().collect::<Vec<char>>();
|
||||
for i in 1..=word1.len() {
|
||||
dp[i][0] = dp[i - 1][0] + 1;
|
||||
}
|
||||
for i in 1..=word2.len() {
|
||||
dp[0][i] = dp[0][i - 1] + 1;
|
||||
}
|
||||
for i in 0..word1.len() {
|
||||
for j in 0..word2.len() {
|
||||
dp[i + 1][j + 1] = (dp[i][j] + if word1[i] != word2[j] { 1 } else { 0 })
|
||||
.min(dp[i][j + 1] + 1)
|
||||
.min(dp[i + 1][j] + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn pacific_atlantic(heights: Vec<Vec<i32>>) -> Vec<Vec<i32>> {
|
||||
let (m,n) = (heights.len(),heights[0].len());
|
||||
let mut pacific = vec![vec![false;n];m];
|
||||
let mut atlantic = vec![vec![false;n];m];
|
||||
let mut ans = vec![];
|
||||
for i in 0..m {
|
||||
pacific[i][0] = true;
|
||||
atlantic[i][n-1] = true;
|
||||
Self::dfs(&heights,&mut pacific,i,0);
|
||||
Self::dfs(&heights,&mut atlantic,i,n-1);
|
||||
}
|
||||
for j in 0..n {
|
||||
pacific[0][j] = true;
|
||||
atlantic[m-1][j] = true;
|
||||
Self::dfs(&heights,&mut pacific,0,j);
|
||||
Self::dfs(&heights,&mut atlantic,m-1,j);
|
||||
}
|
||||
for i in 0..m {
|
||||
for j in 0..n {
|
||||
if pacific[i][j] && atlantic[i][j] {
|
||||
ans.push(vec![i as i32,j as i32]);
|
||||
}
|
||||
}
|
||||
}
|
||||
ans
|
||||
dp[word1.len()][word2.len()]
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// [[1,2,2,3,5],[3,2,3,4,4],[2,4,5,3,1],[6,7,1,4,5],[5,1,1,2,4]]
|
||||
// initial as a 2D vector
|
||||
let arr = vec![
|
||||
vec![1, 2, 2, 3, 5],
|
||||
vec![3, 2, 3, 4, 4],
|
||||
vec![2, 4, 5, 3, 1],
|
||||
vec![6, 7, 1, 4, 5],
|
||||
vec![5, 1, 1, 2, 4],
|
||||
];
|
||||
println!("{:?}", Solution::pacific_atlantic(arr));
|
||||
println!(
|
||||
"{:?}",
|
||||
Solution::min_distance("intention".to_string(), "execution".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user