let 75 Sort Colors

Given an array with n objects colored red, white or blue, sort them 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.

click to show follow up.

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 an one-pass algorithm using only constant space?

主题思想: ; 采用in-place 的方法对三种数字进行排序,思路是:借鉴3路快排,

class Solution {
    public void sortColors(int[] nums) {

        if(nums==null||nums.length==0) return ;

       int p1=0,p2=nums.length-1,index=0;
        while(index<=p2){


            if(nums[index]==0){
                //swap p1 index
                nums[index]=nums[p1];
                nums[p1]=0;
                p1++;
            }
            //swap index p2,  
            if(nums[index]==2){
                nums[index]=nums[p2];
                nums[p2]=2;
                p2--;
                //because p2 has not be handle ,may equal 0,so index need to handle so back index by index--
                //important
                index--;
            }
            index++;
        }
    }
}
// 为不同部门生成颜色 function getDepartmentColor(department) { const colors = [ '#3498db', '#2ecc71', '#e74c3c', '#f39c12', '#9b59b6', '#1abc9c', '#d35400', '#c0392b', '#16a085', '#8e44ad' ]; // 简单哈希 let hash = 0; for (let i = 0; i < department.length; i++) { hash = department.charCodeAt(i) + ((hash << 5) - hash); } return colors[Math.abs(hash) % colors.length]; } // 渲染员工健康趋势图 function renderCharts(employeeId, data) { if (!data || !data.employees) { console.error('员工数据未定义'); return; } // 获取员工数据 const employee = data.employees[employeeId]; if (!employee) { console.error(`未找到员工ID: ${employeeId}`); return; } // 获取记录并按日期排序 const records = [...employee.records]; records.sort((a, b) => new Date(a.date) - new Date(b.date)); // 提取数据 const dates = records.map(r => r.date); const systolic = records.map(r => r.systolic || 0); const diastolic = records.map(r => r.diastolic || 0); const bmi = records.map(r => r.bmi || 0); const standardLine1Value = 18.5; const standardLine2Value = 23.9; const standardLine1Data = new Array(dates.length).fill(standardLine1Value); const standardLine2Data = new Array(dates.length).fill(standardLine2Value); if (window.bmiChart && typeof window.bmiChart.destroy === 'function') { window.bmiChart.destroy(); } // 渲染BMI图表 const bmiCtx = document.getElementById('bmiChart').getContext('2d'); window.bmiChart = new Chart(bmiCtx, { type: 'line', data: { labels: dates, datasets: [{ label: 'BMI', data: bmi, borderColor: '#2ecc71', backgroundColor: 'rgba(46, 204, 113, 0.1)', borderWidth: 2, pointRadius: 5, tension: 0.3, fill: true }, { label:'正常下限', data:standardLine1Data, borderColor:'#3498db', borderDash:[5, 5], borderWidth:2, pointRadius:0, fill:false, tension:0, }, { label:'正常上限', data:standardLine2Data, borderColor:'#e74c3c', borderDash:[5, 5], borderWidth:2, pointRadius:0, fill:false, tension:0, } ] }, options: { responsive: true, maintainAspectRatio: false, plugins: { title: { display: true, text: `${employee.name} BMI趋势图` }, tooltip: { callbacks: { label: function(context) { return `BMI: ${context.parsed.y}`; } } } }, scales: { y: { title: { display: true, text: 'BMI值' }, min: 15, max: 40 }, x: { title: { display: true, text: '测量日期' } } } } }); }显示最近30条数据
09-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值