算法60天:day1

数组-2.二分查找

力扣链接

方法1-左闭右闭区间

class Solution {
public:
    int search(vector<int>& nums, int target) {
        //二分查找法,左闭右闭
        
        int leftindex = 0;
        int rightdex = nums.size() - 1;
        int middle;
        while(leftindex <= rightdex ){
		//middle = (leftindex + rightdex) / 2; //两个int相加可能会造成int类型越界问题
        //也可以使用middle = leftindex + ((rightdex - leftindex) >> 1);
            middle = leftindex + (rightdex - leftindex) / 2;
            if(nums[middle] > target){
                rightdex = middle - 1;
            }else if(nums[middle] < target){
                leftindex = middle + 1;
            }else{
                return middle;
            }
        }
        return -1;
    }
};

方法2-左闭右开区间

class Solution {
public:
    int search(vector<int>& nums, int target) {
        //二分查找法,左闭右开
        int leftindex = 0;
        int rightdex = nums.size();
        int middle;
        while(leftindex < rightdex){
            middle = leftindex + (rightdex - leftindex) / 2;
            if(nums[middle] > target){
                rightdex = middle;
            }else if(nums[middle] < target){
                leftindex = middle + 1;
            }else{
                return middle;
            }
        }
        return -1;
    }
};

数组-3.移除元素

力扣链接

双指针法-快慢指针法

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        //双指针法-快慢指针法
        int slowindex = 0;
        for(int fastindex = 0;fastindex < nums.size();fastindex++){
            if(nums[fastindex] != val){
                nums[slowindex] = nums[fastindex];
                slowindex++;
                //可以简写成这样:nums[slowindex++] = nums[fastindex];
            }
        }
        return slowindex;
    }
};

双指针法-双向指针法

注意点:&&,两边使用规则:左边的优先级要高一点,即左边同时是右边的条件

class Solution {
public:
    int removeElement(vector<int>& nums, int val) {
        int leftindex = 0;
        int rightindex = nums.size() - 1;
        while(leftindex <= rightindex){
        	//这里顺序不能调换,会出错,比如只有nums[]=[1]时,第一次是满足的,第二次再次执行时,nums[1]就不存在,所以肯定会报错
            while(leftindex <= rightindex && nums[leftindex] != val){
                leftindex++;
            }
            while(leftindex <= rightindex && nums[rightindex] == val){
                rightindex--;
            }
            if(leftindex < rightindex){
                nums[leftindex++] = nums[rightindex--];
            }
        }
        return leftindex;
    }
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: 您可以参考下面的代码,它可以帮助您编写计算300753股票最近60天BOLL算法的Java程序:import java.util.*;public class BOLL { public static void main(String[] args) { ArrayList<Double> priceList = new ArrayList<Double>(); // 将股票300753最近60天的价格添加到priceList中 // 计算平均价格 double sum = 0; for (Double price : priceList) { sum += price; } double average = sum / priceList.size(); // 计算标准差 double sumDiff = 0; for (Double price : priceList) { sumDiff += Math.pow(price - average, 2); } double sigma = Math.sqrt(sumDiff / priceList.size()); // 计算BOLL double bollUpper = average + 2 * sigma; double bollLower = average - 2 * sigma; System.out.println("BOLL upper: " + bollUpper); System.out.println("BOLL lower: " + bollLower); } } ### 回答2: BOLL算法是一种常用的技术指标,用于描述股票价格变动的趋势和波动情况。下面是用Java编写股票300753近60天BOLL算法的示例代码: ```java import java.util.ArrayList; import java.util.List; public class BOLLAlgorithm { public static void main(String[] args) { // 定义股票300753近60天的收盘价数据 double[] closingPrices = {10.5, 10.3, 9.8, 9.9, 10.1, 10.6, 11.0, 10.8, 11.5, 11.2, 10.9, 10.7, 10.4, 10.2, 9.9, 9.7, 9.5, 9.8, 10.1, 10.3, 10.6, 10.9, 11.2, 10.7, 10.4, 10.1, 9.9, 9.6, 9.4, 9.2, 9.0, 9.3, 9.7, 10.0, 10.3, 10.6, 11.0, 11.4, 11.8, 11.2, 10.7, 10.2, 9.7, 9.3, 9.5, 9.8, 10.1, 10.5, 10.9, 11.3, 11.7, 11.2, 10.8, 10.4, 10.0, 9.6, 9.2, 9.7, 10.2, 10.7}; // 计算均线数据 List<Double> maList = new ArrayList<>(); for (int i = 0; i < closingPrices.length; i++) { double sum = 0.0; for (int j = i - 19; j <= i; j++) { if (j >= 0) { sum += closingPrices[j]; } } maList.add(sum / 20); } // 计算标准差数据 List<Double> stdList = new ArrayList<>(); for (int i = 0; i < closingPrices.length; i++) { double sum = 0.0; for (int j = i - 19; j <= i; j++) { if (j >= 0) { sum += Math.pow(closingPrices[j] - maList.get(i), 2); } } stdList.add(Math.sqrt(sum / 20)); } // 计算BOLL上轨和下轨数据 List<Double> upperList = new ArrayList<>(); List<Double> lowerList = new ArrayList<>(); for (int i = 0; i < closingPrices.length; i++) { double upper = maList.get(i) + 2 * stdList.get(i); double lower = maList.get(i) - 2 * stdList.get(i); upperList.add(upper); lowerList.add(lower); } // 打印BOLL算法结果 for (int i = 0; i < closingPrices.length; i++) { System.out.println("Date: Day " + (i + 1) + ", Closing Price: " + closingPrices[i] + ", MA: " + maList.get(i) + ", Upper BOLL: " + upperList.get(i) + ", Lower BOLL: " + lowerList.get(i)); } } } ``` 以上代码是一个简单的BOLL算法示例,利用股票300753近60天的收盘价数据计算了该股票的均值、标准差和BOLL上下轨。通过循环遍历收盘价数据,并结合滑动窗口的思想,逐步计算出均线、标准差以及上下轨数据,并打印出每一天的结果。 ### 回答3: BOLL(布林线)是一种常用的技术分析指标,用于判断股票价格的波动情况。它由上、中、下三条线组成,分别代表上轨、中轨和下轨,中轨为股票价格的简单移动平均线,上下轨根据股票的标准差计算而得。 要用Java编写股票300753近60天BOLL算法,我们首先需要获取60天的股票价格数据。这可以通过股票数据接口或者网络API来获取,然后以合适的数据结构进行存储。 然后,我们需要编写一个函数来计算股票价格数据的简单移动平均线。简单移动平均线是将一段时间内的股票价格求和,并除以这段时间的长度。在这个算法中,我们将取60天的价格数据,并计算出其移动平均线。 接下来,我们需要计算标准差。标准差是一种测量数据波动的指标。在这个算法中,我们将取60天的价格数据,并计算出其标准差。 最后,我们根据标准差计算上轨和下轨。上轨是中轨加上标准差的2倍,而下轨是中轨减去标准差的2倍。 下面是一个简单的Java代码示例来实现股票300753近60天BOLL算法: ```java import java.util.ArrayList; import java.util.List; public class BOLLAlgorithm { public static void main(String[] args) { List<Double> prices = new ArrayList<>(); // 存储股票价格数据 // 假设从股票数据接口或网络API获取了60天的股票价格数据,并存储在prices列表中 double sum = 0; for (double price : prices) { sum += price; } double avg = sum / prices.size(); // 计算移动平均线 double temp = 0; for (double price : prices) { temp += Math.pow(price - avg, 2); } double stdDev = Math.sqrt(temp / prices.size()); // 计算标准差 double upperBand = avg + 2 * stdDev; // 上轨 double lowerBand = avg - 2 * stdDev; // 下轨 System.out.println("上轨:" + upperBand); System.out.println("中轨:" + avg); System.out.println("下轨:" + lowerBand); } } ``` 以上代码仅提供了简单的实现示例,实际应用中还需要考虑更多的细节和异常处理。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值