ソースを参照

#feat:leetcode 3751题题解

yangyi 2 週間 前
コミット
40508f536f
2 ファイル変更60 行追加0 行削除
  1. 36 0
      src/leetcode/p3751/Solution.java
  2. 24 0
      src/leetcode/p3751/SolutionTest.java

+ 36 - 0
src/leetcode/p3751/Solution.java

@@ -0,0 +1,36 @@
+package leetcode.p3751;
+
+/**
+ * @fileName: Solution
+ * @projectName: ccsp
+ * @package: leetcode.p3751
+ * @author: yangyi
+ * @date:4/6/2026 7:19 pm
+ * @description: https://leetcode.cn/problems/total-waviness-of-numbers-in-range-i/?envType=daily-question&envId=2026-06-04
+ * 3751. 范围内总波动值 I
+ */
+public class Solution {
+    public int totalWaviness(int num1, int num2) {
+        int ans = 0;
+        int start = Math.max(num1, 100);
+        int end = num2;
+        for (int i = start; i <= end; i++) {
+            ans +=f(i);
+        }
+        return ans;
+    }
+
+    private int f(int v) {
+        int res = 0;
+        String s = v+"";
+        int length = s.length();
+        for (int i = 1; i < length-1; i++) {
+            char c1 = s.charAt(i - 1);
+            char c2 = s.charAt(i);
+            char c3 = s.charAt(i + 1);
+            if (c2 > c1 && c2 > c3)res++;
+            if (c2 < c1 && c2 < c3)res++;
+        }
+        return res;
+    }
+}

+ 24 - 0
src/leetcode/p3751/SolutionTest.java

@@ -0,0 +1,24 @@
+package leetcode.p3751;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @fileName: SolutionTest
+ * @projectName: ccsp
+ * @package: leetcode.p3751
+ * @author: yangyi
+ * @date:4/6/2026 7:36 pm
+ * @description:
+ */
+public class SolutionTest {
+
+    @Test
+    public void totalWaviness() {
+        int num1=120,num2=130;
+        int expected = 3;
+        Solution solution = new Solution();
+        assertEquals(expected,solution.totalWaviness(num1,num2));
+    }
+}