# 75. Sort Colors

本文详细解析了一种解决三色排序问题的算法,即在不使用额外空间的情况下,将数组中的红(0)、白(1)、蓝(2)三种颜色对象按顺序排列。通过双指针技术,算法能在一次遍历内完成排序,有效避免了多次遍历的低效问题。

75. Sort Colors

Given an array with n objects colored red, white or blue, sort them in-place so that objects of the same color are adjacent, with the colors in the order red, white and blue.

Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.

Note:

You are not suppose to use the library’s sort function for this problem.

Example:

Input: [2,0,2,1,1,0]
Output: [0,0,1,1,2,2]

Follow up:

A rather straight forward solution is a two-pass algorithm using counting sort.
First, iterate the array counting number of 0’s, 1’s, and 2’s, then overwrite array with total number of 0’s, then 1’s and followed by 2’s.
Could you come up with a one-pass algorithm using only constant space?

  颜色有0,1,2三种,只需要把数组中为0的元素置于数组的前部,数组中为2的元素置于数组的后部,放置好0与2之后,1会被放置在数组的中部

class Solution {
public:
    void sortColors(vector<int>& A) {
        int size = A.size();
        int start = 0;
        int end = size - 1;
        for(unsigned i = 0; i<=end; i++)//i<=end:在重复查找元素之前停止
        {
            int temp;
            while(A[i] == 2 && i<end)//i<end:避免进入死循环[2,0,2,1,1,0]
            {//交换
                temp = A[end];
                A[end] = A[i];
                A[i] = temp;
                end--;
            }
            while(A[i] == 0  && i>start)//i>start:避免进入死循环[2,0,2,1,1,0]
            {
                temp = A[start];
                A[start] = A[i];
                A[i] = temp;
                start++;
            }

        }
    }
};

注意程序中的两个while循环位置不能对换,因为索引i按0->size-1的方向来遍历数组的,在它已经遍历的数组元素中肯定不会有2存在(有的话也已经被移到最后了),但是在它没有遍历的数组元素中,可能会有0的存在比如[1,2,0],此时如果while(A[i] == 0 && i>start)在前,而while(A[i] == 2 && i<end)在后,输出为[1,0,2]

先看效果: https://renmaiwang.cn/s/jkhfz Hue系列产品将具备高度的个性化定制能力,并且借助内置红、蓝、绿三原色LED的灯泡,能够混合生成1600万种不同色彩的灯光。 整个操作流程完全由安装于iPhone上的应用程序进行管理。 这一创新举措为智能照明控制领域带来了新的启示,国内相关领域的从业者也积极投身于相关研究。 鉴于Hue产品采用WiFi无线连接方式,而国内WiFi网络尚未全面覆盖,本研究选择应用更为普及的蓝牙技术,通过手机蓝牙与单片机进行数据交互,进而产生可调节占空比的PWM信号,以此来控制LED驱动电路,实现LED的调光功能以及DIY调色方案。 本文重点阐述了一种基于手机蓝牙通信的LED灯设计方案,该方案受到飞利浦Hue智能灯泡的启发,但考虑到国内WiFi网络的覆盖限制,故而选用更为通用的蓝牙技术。 以下为相关技术细节的详尽介绍:1. **智能照明控制系统**:智能照明控制系统允许用户借助手机应用程序实现远程控制照明设备,提供个性化的调光及色彩调整功能。 飞利浦Hue作为行业领先者,通过红、蓝、绿三原色LED的混合,能够呈现1600万种颜色,实现了全面的定制化体验。 2. **蓝牙通信技术**:蓝牙技术是一种低成本、短距离的无线传输方案,工作于2.4GHz ISM频段,具备即插即用和强抗干扰能力。 蓝牙协议栈由硬件层和软件层构成,提供通用访问Profile、服务发现应用Profile以及串口Profiles等丰富功能,确保不同设备间的良好互操作性。 3. **脉冲宽度调制调光**:脉冲宽度调制(PWM)是一种高效能的调光方式,通过调节脉冲宽度来控制LED的亮度。 当PWM频率超过200Hz时,人眼无法察觉明显的闪烁现象。 占空比指的...
# 2. 提取 Obesity_Type_III 样本 obesity_type_iii = df[df[target_col] == &#39;Obesity_Type_III&#39;] # 显示样本数量 print(f"Obesity Type III 样本数: {len(obesity_type_iii)}") # 3. 可视化1:性别分布柱状图 plt.figure(figsize=(12, 8)) plt.subplot(2, 3, 1) sns.countplot(data=obesity_type_iii, x=&#39;Gender&#39;) plt.title(&#39;Obesity Type III - 性别分布&#39;) plt.xlabel(&#39;性别&#39;) plt.ylabel(&#39;人数&#39;) # 4. 可视化2:年龄分布直方图 plt.subplot(2, 3, 2) sns.histplot(obesity_type_iii[&#39;Age&#39;], bins=20, kde=True, color=&#39;red&#39;) plt.title(&#39;Obesity Type III - 年龄分布&#39;) plt.xlabel(&#39;年龄&#39;) # 5. 可视化3:身高体重散点图 + BMI估算 obesity_type_iii[&#39;BMI&#39;] = obesity_type_iii[&#39;Weight&#39;] / (obesity_type_iii[&#39;Height&#39;] ** 2) plt.subplot(2, 3, 3) sns.scatterplot(data=obesity_type_iii, x=&#39;Height&#39;, y=&#39;Weight&#39;, hue=&#39;Gender&#39;, palette=&#39;Set1&#39;) plt.title(&#39;Obesity Type III - 身高 vs 体重&#39;) plt.xlabel(&#39;身高 (m)&#39;) plt.ylabel(&#39;体重 (kg)&#39;) # 6. 可视化4:BMI 分布箱线图(对比总体) df[&#39;BMI&#39;] = df[&#39;Weight&#39;] / (df[&#39;Height&#39;] ** 2) df[&#39;Obesity_Category&#39;] = pd.cut( df[&#39;BMI&#39;], bins=[0, 18.5, 24, 28, float(&#39;inf&#39;)], labels=[&#39;Underweight&#39;, &#39;Normal&#39;, &#39;Overweight&#39;, &#39;Obese&#39;] ) plt.subplot(2, 3, 4) sns.boxplot(data=df, x=&#39;Obesity_Category&#39;, y=&#39;BMI&#39;) plt.title(&#39;BMI 分布箱线图&#39;) plt.xlabel(&#39;肥胖类别&#39;) plt.ylabel(&#39;BMI&#39;) # 7. 可视化5:家庭肥胖史占比饼图 plt.subplot(2, 3, 5) family_hist = obesity_type_iii[&#39;family_history_with_overweight&#39;].map({1: &#39;有家族史&#39;, 0: &#39;无家族史&#39;}).value_counts() plt.pie(family_hist, labels=family_hist.index, autopct=&#39;%1.1f%%&#39;, startangle=90, colors=[&#39;lightcoral&#39;, &#39;lightskyblue&#39;]) plt.title(&#39;Obesity Type III - 家族肥胖史比例&#39;) # 8. 可视化6:饮食习惯(FAVC)与肥胖等级热力图 # 编码分类变量用于相关性分析 le = LabelEncoder() df_encoded = df.copy() for col in df_encoded.select_dtypes(include=[&#39;object&#39;]).columns: df_encoded[col] = le.fit_transform(df_encoded[col]) # 计算与肥胖等级相关的特征热力图 plt.subplot(2, 3, 6) corr_matrix = df_encoded.corr() sns.heatmap(corr_matrix[[target_col]].sort_values(by=target_col, ascending=False)[:10], annot=True, cmap=&#39;coolwarm&#39;, cbar=True) plt.title(&#39;Top 特征与肥胖等级相关性&#39;) # 调整布局并显示图像 plt.tight_layout() plt.show() # 9. 打印关键统计信息 print("\n--- Obesity Type III 关键统计 ---") print(obesity_type_iii[[&#39;Age&#39;, &#39;Height&#39;, &#39;Weight&#39;, &#39;BMI&#39;]].describe()) # 10. 输出主要影响因素总结 high_corr_features = corr_matrix[target_col].abs().sort_values(ascending=False)[1:6].index.tolist() print(f"\n与 Obesity Type III 相关性最高的前五个特征:\n{high_corr_features}") df改为wujing
11-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值