2018.12.5——第九天

题目

 前面的最大利益1问题就暂且不做,做难一点的二。我的思路是贪心,在每个峰值抛售,每个谷值买入。因此需要加入状态标记现在处于的状态

         设立 state:         0   处于无货状态在谷值买入:即prices[curr]<prices[curr+1]时买入prices[curr];

                                      1  处于有货状态在峰值卖出:即prices[curr]>prices[curr+1]时卖出prices[curr];

C++实现

class Solution {
public:
    int flag=0;
    int income=0;
    int expense=0;
    
    void sell(int price){
        income+=price;
        flag=0;
    }
    
    void buy(int price){
        expense+=price;
        flag=1;
    }
    int maxProfit(vector<int>& prices) {
        int curr=0;
        if(prices.size()==0||prices.size()==1)
            return 0;
        if(prices.size()==2){
            if(prices[0]<prices[1])
                return prices[1]-prices[0];
            else
                return 0;
        }
        for(curr=0;curr<=prices.size()-2;curr++){
            if(flag==0){
                if(prices[curr]<prices[curr+1])
                    Solution::buy(prices[curr]);
            }
            
            else if(flag==1){
                if(prices[curr]>prices[curr+1]){
                    Solution::sell(prices[curr]);
                }
            }
        }
        if(flag==1)
            Solution::sell(prices[curr]);
        
        return income-expense;
    }
};

出了点小问题,很快解决~

然后就是要慢慢转型的JAVA实现

JAVA实现

class Solution {
        int flag=0;
        int income=0;
        int expense=0;
    
        public void sell(int price){
            income+=price;
            flag=0;
         }
    
        public void buy(int price){
           expense+=price;
           flag=1;
        }
         public int maxProfit(int[] prices) {
            int curr=0;
            if(prices.length==0||prices.length==1)
                return 0;
            if(prices.length==2){
                if(prices[0]<prices[1])
                   return prices[1]-prices[0];
               else
                  return 0;
             }
            for(curr=0;curr<=prices.length-2;curr++){
               if(flag==0){
                  if(prices[curr]<prices[curr+1])
                       this.buy(prices[curr]);
                  }
            
                else if(flag==1){
                     if(prices[curr]>prices[curr+1]){
                            this.sell(prices[curr]);
                        }
                }
            }
            if(flag==1)
                 this.sell(prices[curr]);
        
                return income-expense;
        }
}

所做的转变是prices.size()-->prices.length

Solution::sell--->this.sell

Solution::buy--->this.buy

所学知识汇总

JAVA零碎小知识

在java类中调用同类的函数可以用this.函数名(){}

而main是静态函数,不可直接调用非静态函数sell。要在main中定义一个类的对象才可以调用sell方法。
在main中也可以用this.sell

public static void main(String[] args) {
    Solution s = new Solution();
    s.sell(1);
}

float nums;

System.out.println(nums);//全部输出

System.out.println("%.2f",nums);//保留两位

int[] num=new int[10];

System.out.println(nums.length);//数组长度

for(int k : nums){}//把nums里的每个数赋值给k做循环   (FOR-EACH循环)

int[] a=new int[10];
        a[0]=5;
        int []b=a;
        System.out.println(a[0]);
        
        b[0]=16;
        System.out.println(a[0]);

事实证明java里的“数组”更偏向于c里面的指针,而非数组。

因此若是复制数组,只能用循环每个值拷贝或者用(https://blog.csdn.net/u011669700/article/details/79323251

目前在Java中数据拷贝提供了如下方式:

clone
System.arraycopy
Arrays.copyOf
Arrays.copyOfRange
下面分别介绍他们的用法

1、clone 方法

clone方法是从Object类继承过来的,基本数据类型(String ,boolean,char,byte,short,float ,double,long)都可以直接使用clone方法进行克隆,注意String类型是因为其值不可变所以才可以使用。

int 类型示例

int[] a1 = {1, 3};
int[] a2 = a1.clone();

a1[0] = 666;
System.out.println(Arrays.toString(a1));   //[666, 3]
System.out.println(Arrays.toString(a2));   //[1, 3]

String类型示例

String[] a1 = {"a1", "a2"};
String[] a2 = a1.clone();

a1[0] = "b1"; //更改a1数组中元素的值
System.out.println(Arrays.toString(a1));   //[b1, a2]
System.out.println(Arrays.toString(a2));   //[a1, a2]

2、System.arraycopy

System.arraycopy方法是一个本地的方法,源码里定义如下:

public static native void arraycopy(Object src, int srcPos, Object dest, int desPos, int length)
1
其参数含义为:

(原数组, 原数组的开始位置, 目标数组, 目标数组的开始位置, 拷贝个数)

用法示例

int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = new int[10];

System.arraycopy(a1, 1, a2, 3, 3);
System.out.println(Arrays.toString(a1)); // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2)); // [0, 0, 0, 2, 3, 4, 0, 0, 0, 0]

当使用这个方法的时候,需要复制到一个已经分配内存单元的数组。

3、 Arrays.copyOf

Arrays.copyOf底层其实也是用的System.arraycopy 源码如下:

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    @SuppressWarnings("unchecked")
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;
}

参数含义:

(原数组,拷贝的个数)

用法示例:

int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = Arrays.copyOf(a1, 3);

System.out.println(Arrays.toString(a1)) // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2)) // [1, 2, 3]

使用该方法无需我们事先使用new关键字对对象进行内存单元的分配

4、 Arrays.copyOfRange

Arrays.copyOfRange底层其实也是用的System.arraycopy,只不过封装了一个方法

public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
    int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    @SuppressWarnings("unchecked")
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, from, copy, 0,
                     Math.min(original.length - from, newLength));
    return copy;
}

参数含义

(原数组,开始位置,拷贝的个数)
1
用法示例:

int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = Arrays.copyOfRange(a1, 0, 1);

System.out.println(Arrays.toString(a1)) // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2)) // [1]

最后需要注意的是基本类型的拷贝是不影响原数组的值的,如果是引用类型,就不能在这用了,因为数组的拷贝是浅拷贝,对于基本类型可以,对于引用类型是不适合的。
 

5、那么如何实现对象的深度拷贝呢?

5.1 实现Cloneable接口

实现Cloneable接口,并重写clone方法,注意一个类不实现这个接口,直接使用clone方法是编译通不过的。

/**
 * Created by Joe on 2018/2/13.
 */
public class Dog implements Cloneable {
    private String id;
    private String name;

    public Dog(String id, String name) {
        this.id = id;
        this.name = name;
    }

    // 省略 getter 、 setter 以及 toString 方法

    @Override
    public Dog clone() throws CloneNotSupportedException {
        Dog dog = (Dog) super.clone();

        return dog;
    }
}

示例:

Dog dog1 = new Dog("1", "Dog1");
Dog dog2 = dog1.clone();

dog2.setName("Dog1 changed");

System.out.println(dog1); // Dog{id='1', name='Dog1'}
System.out.println(dog2); // Dog{id='1', name='Dog1 changed'}

5.2 组合类深拷贝

如果一个类里面,又引用其他的类,其他的类又有引用别的类,那么想要深度拷贝必须所有的类及其引用的类都得实现Cloneable接口,重写clone方法,这样以来非常麻烦,简单的方法是让所有的对象实现序列化接口(Serializable),然后通过序列化反序列化的方法来深度拷贝对象。

public Dog myClone() {
    Dog dog = null;

    try {
        //将对象序列化成为流,因为写在流是对象里的一个拷贝
        //而原始对象扔在存在JVM中,所以利用这个特性可以实现深拷贝
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(this);

        //将流序列化为对象
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
        dog = (Dog) objectInputStream.readObject();
    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

    return dog;
}

总结:

本文介绍了关于Java里面的数组拷贝的几种方式和用法,并给出了如何在Java里面实现对象的深度拷贝,注意除非必需,一般情况下不要使用对象的深度拷贝,因为性能较差。除了自己实现深度拷贝的功能外,网上也有一些开源的工具类也集成了这些功能,如Apache Common Lang3,但原理都大同小异,感兴趣的同学可以自己去学习下。
--------------------- 
作者:寒沧 
来源:CSDN 
原文:https://blog.csdn.net/u011669700/article/details/79323251 

 

发现问题汇总

c++跟java的知识差距还是有的,还需要重点学习,要专型了

 

总结

java掌握太差了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值