45. Jump Game II
Hard

Given an array of non-negative integers, you are initially positioned at the first index of the array.

Each element in the array represents your maximum jump length at that position.

Your goal is to reach the last index in the minimum number of jumps.

Example:

Input: [2,3,1,1,4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2.
    Jump 1 step from index 0 to 1, then 3 steps to the last index.

Note:

You can assume that you can always reach the last index.

Main Idea:

when write the code except for the main idea,please pay attention to >= > the difference may cause result different

#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution
{
public:
    int jump(vector<int>& nums)
    {
        int res=0,r=0,l=0,next_r=0;
        int times=nums.size()-1;
        while(r<times)
        {
            for(int i=l;i<=r;i++)
            {
                next_r=max(next_r,nums[i]+i);
            }
            l=r+1;
            r=next_r;
            res++;
        }
        return res;
    }

};
int main()
{
    Solution s;
    vector<int> v;
    v.push_back(2);
    v.push_back(3);
    v.push_back(1);
    v.push_back(1);
    v.push_back(4);
    int res=s.jump(v);
    cout<<res<<endl;
    return 0;
}

 Python Version's idea is similar,but bad thing is I made same mistake with former C++,<= I loss =

Bonus:

Multiple element assignment method

Directly write:

a=1

b=2

or 

a,b=1,2

if a==b value

a=b=1 also ok

class Solution(object):
    def jump(self,nums):
        #len instead of length in C++
        N=len(nums)
        cur=pre=count=pos=0
        while cur<N-1:
            count+=1
            pre=cur
            # pre is the range's right,so it also need to be calculate
            while pos<=pre:
                cur=max(cur,nums[pos]+pos)
                pos+=1
        return count

solu=Solution()
nums= [2,3,1,1,4]
print(solu.jump(nums))

This blog's writer can provide multi ways to solve problems,also compare different problem's similarity.

 https://www.cnblogs.com/grandyang/p/4358848.html

Bonus:

std::vector::pop_back

void pop_back();

Delete last element

Removes the last element in the vector, effectively reducing the container size by one

ATTENTION:

pop_back()  just directly delete the last one so we needn't put things in ()

 

46. Permutations
Medium

 

Given a collection of distinct integers, return all possible permutations.

Example:

Input: [1,2,3]
Output:
[
  [1,2,3],
  [1,3,2],
  [2,1,3],
  [2,3,1],
  [3,1,2],
  [3,2,1]
]
C++ First Solution:
Method one use DFS deep first search
only attention point is that we have transfer nums,so the sizes of nums needn't transfer too
instead we should transfer the res,our result vector,THE reason is &,use this means value transfer
So the res directly been changed during the DFS function
#include<stdio.h>
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Solution {
public:
    vector<vector<int>> permute(vector<int>& nums) {
        vector<vector<int>> res;
        vector<int> out,visited(nums.size(),0);
        int n=nums.size();
        DFS(out,nums,res,visited,0);
        return res;
    }

    void DFS(vector<int>& out,vector<int>& nums,vector<vector<int>>& res,vector<int>& visited,int label)
    {
        if(label==nums.size()) {res.push_back(out);return;}
        for(int i=0;i<nums.size();i++)
        {
            if(visited[i]==1) continue;
            out.push_back(nums[i]);
            visited[i]=1;
            DFS(out,nums,res,visited,label+1);
            out.pop_back();
            visited[i]=0;
        }
        return;

    }
};
int main()
{
    Solution s;
    vector<int> v;
    vector<vector<int>> res;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
//    v.push_back(1);
//    v.push_back(4);
    res=s.permute(v);
    for (vector<vector<int>>::iterator it = res.begin(); it != res.end(); ++it){
        for (int i = 0; i < (*it).size(); ++i)
            cout << (*it)[i] << " " ;
        cout <<endl ;
        }
    //cout<<res<<endl;
    return 0;
}
Python version:
  Bonus:
    inner function:
      sometimes we can use inner function at python.Mainly to use convinence and protect
privacy.But after we definition the inner function we must use it,then it can be work
      Also,this cannot be used by outside
>>> def outer(n):
...     def inner_multiple(n):  # 从外部代码隐藏
...         return n * 2
#must use it by hand
... num = inner_multiple(n) ... print(num) ... >>>
>>> outer(5)
10
>>> 
>>> inner_multiple(5)  # 外部无法访问
...
NameError: name 'inner_multiple' is not defined

Main Part of Code:

class Solution(object):
    def permute(self,nums):
        visited=[0]*len(nums)
        res=[]
        def DFS(path):
            if len(path)==len(nums):
                res.append(path)
            #in range should use len<--int
            for i in range(len(nums)):
                #just use visited[i] not visited[nums[i]] because we only need duplicate checking by index
                if not visited[i]:
                    visited[i]=1
                    #during + need give nums add []
                    DFS(path+[nums[i]])
                    visited[i]=0
        DFS([])
        return res

solu=Solution()
nums= [1,2,3]
print(solu.permute(nums))

 

 


  
 

 

posted on 2019-07-25 19:17  黑暗尽头的超音速炬火  阅读(213)  评论(0编辑  收藏  举报