leetcode #75 in cpp

来源:互联网 发布:打印机端口里没有usb 编辑:程序博客网 时间:2024/05/16 03:18

Solution:

We can use any kind of sort. 

I implemented bubble sort for this problem. Obviously this is not the best method. 

Code: 

class Solution {public:    void sortColors(vector<int>& nums) {                //bubble sort, one pass        for(int i = 0; i < nums.size(); i++){            int j = i-1;            int temp = nums[i];            if(nums[j] > temp){                while(j>=0 && nums[j] > temp){                    nums[j+1] = nums[j];                    j--;                }                nums[j+1] = temp;            }        }           }};


0 0
原创粉丝点击