|
@@ -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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|