mirror of
				https://github.com/krahets/hello-algo.git
				synced 2025-10-31 10:26:48 +08:00 
			
		
		
		
	Bug fixes and improvements (#1627)
* Update Copyright 2024 to 2025. * Fix the flex of .profile-cell for mobile devices. * Update performance_evaluation.md * 抛出 -> 给出 * Sync zh and zh-hant version. * Improve the landing page of the English version. * Bug fixes * Fix readme-typing-svg * Update readme-typing-svg * Bug fixes * sync zh and zh-hant versions.
This commit is contained in:
		| @ -69,7 +69,7 @@ class LinkedListDeque: | ||||
|             val: int = self._front.val  # 暫存頭節點值 | ||||
|             # 刪除頭節點 | ||||
|             fnext: ListNode | None = self._front.next | ||||
|             if fnext != None: | ||||
|             if fnext is not None: | ||||
|                 fnext.prev = None | ||||
|                 self._front.next = None | ||||
|             self._front = fnext  # 更新頭節點 | ||||
| @ -78,7 +78,7 @@ class LinkedListDeque: | ||||
|             val: int = self._rear.val  # 暫存尾節點值 | ||||
|             # 刪除尾節點 | ||||
|             rprev: ListNode | None = self._rear.prev | ||||
|             if rprev != None: | ||||
|             if rprev is not None: | ||||
|                 rprev.next = None | ||||
|                 self._rear.prev = None | ||||
|             self._rear = rprev  # 更新尾節點 | ||||
|  | ||||
| @ -21,9 +21,8 @@ fn extend(nums: &[i32], enlarge: usize) -> Vec<i32> { | ||||
|     // 初始化一個擴展長度後的陣列 | ||||
|     let mut res: Vec<i32> = vec![0; nums.len() + enlarge]; | ||||
|     // 將原陣列中的所有元素複製到新 | ||||
|     for i in 0..nums.len() { | ||||
|         res[i] = nums[i]; | ||||
|     } | ||||
|     res[0..nums.len()].copy_from_slice(nums); | ||||
|  | ||||
|     // 返回擴展後的新陣列 | ||||
|     res | ||||
| } | ||||
| @ -54,7 +53,8 @@ fn traverse(nums: &[i32]) { | ||||
|         _count += nums[i]; | ||||
|     } | ||||
|     // 直接走訪陣列元素 | ||||
|     for num in nums { | ||||
|     _count = 0; | ||||
|     for &num in nums { | ||||
|         _count += num; | ||||
|     } | ||||
| } | ||||
|  | ||||
| @ -19,9 +19,6 @@ pub fn insert<T>(n0: &Rc<RefCell<ListNode<T>>>, P: Rc<RefCell<ListNode<T>>>) { | ||||
| /* 刪除鏈結串列的節點 n0 之後的首個節點 */ | ||||
| #[allow(non_snake_case)] | ||||
| pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) { | ||||
|     if n0.borrow().next.is_none() { | ||||
|         return; | ||||
|     }; | ||||
|     // n0 -> P -> n1 | ||||
|     let P = n0.borrow_mut().next.take(); | ||||
|     if let Some(node) = P { | ||||
| @ -31,26 +28,39 @@ pub fn remove<T>(n0: &Rc<RefCell<ListNode<T>>>) { | ||||
| } | ||||
|  | ||||
| /* 訪問鏈結串列中索引為 index 的節點 */ | ||||
| pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Rc<RefCell<ListNode<T>>> { | ||||
|     if index <= 0 { | ||||
|         return head; | ||||
|     }; | ||||
|     if let Some(node) = &head.borrow().next { | ||||
|         return access(node.clone(), index - 1); | ||||
| pub fn access<T>(head: Rc<RefCell<ListNode<T>>>, index: i32) -> Option<Rc<RefCell<ListNode<T>>>> { | ||||
|     fn dfs<T>( | ||||
|         head: Option<&Rc<RefCell<ListNode<T>>>>, | ||||
|         index: i32, | ||||
|     ) -> Option<Rc<RefCell<ListNode<T>>>> { | ||||
|         if index <= 0 { | ||||
|             return head.cloned(); | ||||
|         } | ||||
|  | ||||
|         if let Some(node) = head { | ||||
|             dfs(node.borrow().next.as_ref(), index - 1) | ||||
|         } else { | ||||
|             None | ||||
|         } | ||||
|     } | ||||
|  | ||||
|     return head; | ||||
|     dfs(Some(head).as_ref(), index) | ||||
| } | ||||
|  | ||||
| /* 在鏈結串列中查詢值為 target 的首個節點 */ | ||||
| pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T, index: i32) -> i32 { | ||||
|     if head.borrow().val == target { | ||||
|         return index; | ||||
|     }; | ||||
|     if let Some(node) = &head.borrow_mut().next { | ||||
|         return find(node.clone(), target, index + 1); | ||||
| pub fn find<T: PartialEq>(head: Rc<RefCell<ListNode<T>>>, target: T) -> i32 { | ||||
|     fn find<T: PartialEq>(head: Option<&Rc<RefCell<ListNode<T>>>>, target: T, idx: i32) -> i32 { | ||||
|         if let Some(node) = head { | ||||
|             if node.borrow().val == target { | ||||
|                 return idx; | ||||
|             } | ||||
|             return find(node.borrow().next.as_ref(), target, idx + 1); | ||||
|         } else { | ||||
|             -1 | ||||
|         } | ||||
|     } | ||||
|     return -1; | ||||
|  | ||||
|     find(Some(head).as_ref(), target, 0) | ||||
| } | ||||
|  | ||||
| /* Driver Code */ | ||||
| @ -82,9 +92,9 @@ fn main() { | ||||
|  | ||||
|     /* 訪問節點 */ | ||||
|     let node = access(n0.clone(), 3); | ||||
|     println!("鏈結串列中索引 3 處的節點的值 = {}", node.borrow().val); | ||||
|     println!("鏈結串列中索引 3 處的節點的值 = {}", node.unwrap().borrow().val); | ||||
|  | ||||
|     /* 查詢節點 */ | ||||
|     let index = find(n0.clone(), 2, 0); | ||||
|     let index = find(n0.clone(), 2); | ||||
|     println!("鏈結串列中值為 2 的節點的索引 = {}", index); | ||||
| } | ||||
|  | ||||
| @ -10,9 +10,15 @@ pub struct Vertex { | ||||
|     pub val: i32, | ||||
| } | ||||
|  | ||||
| impl From<i32> for Vertex { | ||||
|     fn from(value: i32) -> Self { | ||||
|         Self { val: value } | ||||
|     } | ||||
| } | ||||
|  | ||||
| /* 輸入值串列 vals ,返回頂點串列 vets */ | ||||
| pub fn vals_to_vets(vals: Vec<i32>) -> Vec<Vertex> { | ||||
|     vals.into_iter().map(|val| Vertex { val }).collect() | ||||
|     vals.into_iter().map(|val| val.into()).collect() | ||||
| } | ||||
|  | ||||
| /* 輸入頂點串列 vets ,返回值串列 vals */ | ||||
|  | ||||
		Reference in New Issue
	
	Block a user
	 Yudong Jin
					Yudong Jin