Browse Source

feat: single_number

Zhang Li 1 year ago
parent
commit
1d53f6566f

+ 2 - 0
src/solutions/array/mod.rs

@@ -13,3 +13,5 @@ mod pascal_triangle2;
 mod triangle;
 mod longest_consecutive_sequence;
 mod longest_palindromic_substring;
+mod single_number;
+mod single_number2;

+ 32 - 0
src/solutions/array/single_number.rs

@@ -0,0 +1,32 @@
+struct Solution;
+
+impl Solution {
+    pub fn single_number(nums: Vec<i32>) -> i32 {
+        let mut res = 0;
+        for i in nums {
+            res ^= i;
+        }
+
+        res
+
+    }
+}
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_single_number() {
+        let nums = vec![2,2,1];
+        let res = Solution::single_number(nums);
+        assert_eq!(res, 1);
+    }
+
+    #[test]
+    fn test_single_number2() {
+        let nums = vec![4,1,2,1,2];
+        let res = Solution::single_number(nums);
+        assert_eq!(res, 4);
+    }
+}

+ 38 - 0
src/solutions/array/single_number2.rs

@@ -0,0 +1,38 @@
+struct Solution;
+
+impl Solution {
+    pub fn single_number(nums: Vec<i32>) -> i32 {
+
+        let mut nums = nums;
+        nums.sort();
+        let mut count = 1;
+        let mut prev = nums[0];
+
+        for i in 1..nums.len() {
+            if prev != nums[i] {
+                if count == 1 {
+                    break;
+                }
+                prev = nums[i];
+                count = 0;
+            }
+            count += 1;
+        }
+
+        prev
+
+    }
+}
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_single_number() {
+        let  nums = vec![0,1,0,1,0,1,99];
+        let res = Solution::single_number(nums);
+        assert_eq!(res, 99);
+    }
+}

+ 3 - 3
src/solutions/bt/gas_station.rs

@@ -2,8 +2,8 @@ struct Solution;
 
 impl Solution {
     pub fn can_complete_circuit(gas: Vec<i32>, cost: Vec<i32>) -> i32 {
-        let mut gs = gas.iter().sum::<i32>();
-        let mut cs = cost.iter().sum::<i32>();
+        let gs = gas.iter().sum::<i32>();
+        let cs = cost.iter().sum::<i32>();
 
         if gs < cs {
             return -1;
@@ -88,7 +88,7 @@ mod tests {
     fn test_can_complete_circuit1() {
         let gas = vec![1,2,3,4,5];
         let cost = vec![3,4,5,1,2];
-        let res = Solution::can_complete_circuit(gas, cost);
+        let res = Solution::can_complete_circuit1(gas, cost);
         assert_eq!(res, 3);
     }
 }

+ 42 - 0
src/solutions/dp/candy.rs

@@ -0,0 +1,42 @@
+struct Solution;
+
+impl Solution {
+    pub fn candy(ratings: Vec<i32>) -> i32 {
+        let l = ratings.len();
+        let mut dp = vec![1; l];
+        for i in 1..l {
+            if ratings[i] > ratings[i-1] {
+                dp[i] = dp[i-1] + 1;
+            }
+        }
+
+        for i in (0..l-1).rev() {
+            if ratings[i] > ratings[i+1] {
+                dp[i] = dp[i].max(dp[i+1] + 1);
+            }
+
+        }
+
+        dp.into_iter().sum()
+    }
+}
+
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn test_candy() {
+        let ratings = vec![1,0,2];
+        let res = Solution::candy(ratings);
+        assert_eq!(res, 5);
+    }
+
+    #[test]
+    fn test_candy1() {
+        let ratings = vec![1,3,2,2,1];
+        let res = Solution::candy(ratings);
+        assert_eq!(res, 7);
+    }
+}

+ 2 - 1
src/solutions/dp/mod.rs

@@ -15,4 +15,5 @@ mod best_time_to_buy_and_sell_stock;
 mod best_time_to_buy_and_sell_stock2;
 mod best_time_to_buy_and_sell_stock3;
 mod palindrome_partitioning;
-mod palindrome_partitioning2;
+mod palindrome_partitioning2;
+mod candy;