#两数之和_Java版 @FDDLC

这是一个关于算法实现的问题,给定一个整数数组和一个目标值,通过Java代码实现找到数组中两个数的下标,使得它们相加等于目标值。题目中给出的例子是数组{20, 70, 110, 150}

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

给出一个整数数组,请在数组中找出两个加起来等于目标值的数,

你给出的函数twoSum 需要返回这两个数字的下标(index1,index2),需要满足 index1 小于index2.。注意:下标是从1开始的

假设给出的数组中只存在唯一解

例如:

给出的数组为 {20, 70, 110, 150},目标值为90
输出 index1=1, index2=2

 

示例1

输入

[3,2,4],6

输出

[2,3]

 

AC代码(Java版)

import java.util.*;


public class Solution {
    public int[] twoSum (int[] numbers, int target) {
        int[] answer = new int[2];
        for(int i = 0, size = numbers.length; i < size; i++) {
            for(int j = i+1; j < size; j++) {
                if(numbers[i] + numbers[j] == target) {
                    answer[0] = i+1;
                    answer[1] = j+1;
                    return answer;
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        int[] answer = new Solution().twoSum(new int[]{3, 2, 4}, 6);
        System.out.println(answer[0] + ", " + answer[1]);
    }
}
### FDDLC 陷波滤波器 技术实现与相关概念 #### 1. FDDLC 的定义与背景 FDDLC(Frequency Domain Digital Loop Closure)是一种在频域中实现数字闭环控制的技术。它主要用于信号处理控制系统设计,特别是在需要精确抑制特定频率干扰的应用场景中。通过结合陷波滤波器的设计,FDDLC 能够有效去除或削弱目标频率的干扰信号[^1]。 #### 2. 陷波滤波器在 FDDLC 中的作用 陷波滤波器是 FDDLC 系统中的关键组件之一,用于消除特定频率范围内的干扰。其传递函数可以表示为: \[ H(s) = \frac{s^2 + \omega_0^2}{s^2 + \frac{\omega_0}{Q}s + \omega_0^2} \] 其中 \( \omega_0 \) 是陷波频率的角频率,\( Q \) 是品质因子,决定了陷波带宽的窄度[^2]。在 FDDLC 系统中,陷波滤波器通常被设计为无限脉冲响应(IIR)滤波器结构,以确保高效且稳定的频率抑制效果。 #### 3. FDDLC 中陷波滤波器的技术实现 以下是一个基于 Python 的 FDDLC 陷波滤波器实现示例: ```python import numpy as np from scipy import signal import matplotlib.pyplot as plt # 参数设置 fs = 1000 # 采样频率 (Hz) f0 = 50 # 陷波频率 (Hz) Q = 30 # 品质因子 # 计算归一化频率 w0 = f0 / (fs / 2) # 设计陷波滤波器 b, a = signal.iirnotch(w0, Q) # 生成测试信号 t = np.linspace(0, 1, fs, endpoint=False) x = np.sin(2 * np.pi * 50 * t) + np.sin(2 * np.pi * 100 * t) # 应用滤波器 y = signal.filtfilt(b, a, x) # 绘制结果 plt.figure(figsize=(10, 6)) plt.subplot(2, 1, 1) plt.plot(t, x) plt.title(&#39;Input Signal&#39;) plt.xlabel(&#39;Time [s]&#39;) plt.ylabel(&#39;Amplitude&#39;) plt.subplot(2, 1, 2) plt.plot(t, y) plt.title(&#39;Filtered Signal&#39;) plt.xlabel(&#39;Time [s]&#39;) plt.ylabel(&#39;Amplitude&#39;) plt.tight_layout() plt.show() ``` 上述代码中使用了 `scipy.signal.iirnotch` 函数来设计陷波滤波器,并通过 `filtfilt` 方法对输入信号进行零相位滤波[^1]。 #### 4. FDDLC 在图像处理中的应用 FDDLC 还可以应用于图像处理领域,特别是在去除周期性噪声方面。以下是一个基于 OpenCV NumPy 的图像陷波滤波器实现示例: ```python import cv2 import numpy as np # 加载灰度图像 img = cv2.imread(&#39;input_image.jpg&#39;, cv2.IMREAD_GRAYSCALE) # 离散傅里叶变换 dft = cv2.dft(np.float32(img), flags=cv2.DFT_COMPLEX_OUTPUT) dft_shift = np.fft.fftshift(dft) # 创建陷波滤波器掩模 rows, cols = img.shape crow, ccol = rows // 2, cols // 2 mask = np.ones((rows, cols, 2), np.uint8) size = 10 mask[crow - size:crow + size, ccol - size:ccol + size] = 0 # 应用滤波器 dft_shift = dft_shift * mask f_ishift = np.fft.ifftshift(dft_shift) filtered_img = cv2.idft(f_ishift) filtered_img = cv2.magnitude(filtered_img[:, :, 0], filtered_img[:, :, 1]) # 归一化并保存结果 filtered_img = cv2.normalize(filtered_img, None, 0, 255, cv2.NORM_MINMAX) filtered_img = np.uint8(filtered_img) cv2.imwrite(&#39;output_image.jpg&#39;, filtered_img) ``` 此代码通过在频域中创建一个陷波滤波器掩模,有效去除了图像中的周期性噪声[^4]。 #### 5. FDDLC 的优势与局限性 FDDLC 技术的优势在于能够精确地抑制特定频率的干扰,同时保持其他频率信号的完整性[^3]。然而,其局限性包括对系统参数敏感以及可能引入额外的计算复杂度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值