Solve implement-strstr.
This commit is contained in:
		
							parent
							
								
									919af81094
								
							
						
					
					
						commit
						bb5741755e
					
				| 
						 | 
					@ -0,0 +1,14 @@
 | 
				
			||||||
 | 
					pub fn str_str(haystack: String, needle: String) -> i32 {
 | 
				
			||||||
 | 
					  if needle.is_empty() {
 | 
				
			||||||
 | 
					    return 0;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  haystack.find(&needle).map(|n| n as i32).unwrap_or(-1)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#[test]
 | 
				
			||||||
 | 
					fn test_implement_strstr() {
 | 
				
			||||||
 | 
					  assert_eq!(str_str("hello".to_string(), "ll".to_string()), 2);
 | 
				
			||||||
 | 
					  assert_eq!(str_str("aaaaa".to_string(), "bba".to_string()), -1);
 | 
				
			||||||
 | 
					  assert_eq!(str_str("aaaaa".to_string(), "".to_string()), 0);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
| 
						 | 
					@ -1,3 +1,4 @@
 | 
				
			||||||
 | 
					pub mod implement_strstr;
 | 
				
			||||||
pub mod longest_common_prefix;
 | 
					pub mod longest_common_prefix;
 | 
				
			||||||
pub mod palindrome_number;
 | 
					pub mod palindrome_number;
 | 
				
			||||||
pub mod reverse_integer;
 | 
					pub mod reverse_integer;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue