Topic:

Given an arrayAof non-negative integers, return an array consisting of all the even elements ofA, followed by all the odd elements ofA.

You may return any answer array that satisfies this condition.

Example 1:

Input: [3,1,2,4]
Output: [2,4,3,1]
The outputs [4,2,3,1], [2,4,1,3], and [4,2,1,3] would also be accepted.

Note:

  1. 1 <= A.length <= 5000
  2. 0 <= A[i] <= 5000

Facing Problems:

1.auto

  • 當你想要拷貝範圍的元素時,使用(auto x:range)。
  • 當你想要修改範圍的元素時,使用(auto && x:range)。
  • 當你想要只讀範圍的元素時,使用(const auto&x:range)。

2.static_cast, dynamic_cast, reinterpret_cast, const_cast

static_cast: It uses forcibly to change type of the element.

dynamic_cast: It will inspect backward compatible?

reinterpret_cast:?

const_cast:?

3.vector.emplace(postion, add_element)

http://www.cplusplus.com/reference/vector/vector/emplace

4.iterator

std::vector<int>::iterator it;

5.invalid types'<unresolved overloaded function type>[int]'for array subscript

Turning A.at[i] into A.at(i)

6.no matching function for call to'std::vector<int>::erase(__gnu_cxx::__alloc_traits<std::allocator<int>>::value_type&)'

Answer:

class Solution {
public:
    vector<int> sortArrayByParity(vector<int>& A) {
       vector<int>::iterator it;
       int temp=0;
        for(int i=0;i<A.size();i++){
           temp=A.at[i];
           if(temp%2){
               A.erase(A.at[i]);
               A.emplace(A.end(),temp);

           }
       }
       for(it=A.begin();it!=A.end();it++)
          return *it;
    }

};

results matching ""

    No results matching ""