leetcode #75 in cpp

原创 2016年06月01日 04:32:39

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;
            }
        }
       
    }
};


相关文章推荐

leetcode题解-75. Sort Colors && 442. Find All Duplicates in an Array

75, 题目:Given an array with n objects colored red, white or blue, sort them so that objects of the sa...

leetcode #36 in cpp.

The question is to determine a Sudoku is valid.  Solution:  if a Sudoku is valid, each row, each co...

*leetcode 138 in cpp

A linked list is given such that each node contains an additional random pointer which could point t...

leetcode #17 in cpp

The question is : Given a digit string, return all possible letter combinations that the number coul...

leetcode $42 in cpp

Code: (This method is not the best one. There are other methods using two pointers) Key: a bar coul...

*leetcode #80 in cpp

Follow up for "Remove Duplicates": What if duplicates are allowed at most twice? For example, G...

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)

Leetcode 116. Populating Next Right Pointers in Each Node (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月27日 02:35
  • 215

Leetcode 25. Reverse Nodes in k-Group (Hard) (cpp)

Leetcode 25. Reverse Nodes in k-Group (Hard) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年12月10日 04:04
  • 159

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)

Leetcode 230. Kth Smallest Element in a BST (Medium) (cpp)
  • Niko_Ke
  • Niko_Ke
  • 2016年08月23日 11:17
  • 278

leetcode #60 in cpp

The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode #75 in cpp
举报原因:
原因补充:

(最多只允许输入30个字)