其他分享
首页 > 其他分享> > 1046. 最后一块石头的重量

1046. 最后一块石头的重量

作者:互联网

1046. 最后一块石头的重量

多种写法多种思路,重在参考,莫要争辩!

s

class Solution {
    public int lastStoneWeight(int[] stones) {
                if(stones.length == 1){
            return stones[0];
        }
        int js = 2;
        int end = 0;
        while(js > 1){
            int x = 0;
            int y = 0;
            int i,j;
            int js1=0,js2=0;
            for(i=0;i<stones.length;++i){
                if(stones[i]>y){
                    y = stones[i];
                    js1 = i;
                }
            }
            for(i=stones.length-1;i>=0;--i){
                if(stones[i]==y){
                    stones[i] = 0;
                    break;
                }
            }
            for(i=0;i<stones.length;++i){
                if(stones[i]>x){
                    x = stones[i];
                    js2 = i;
                }
            }
            stones[js1] = y-x;
            stones[js2] = 0;
            js = 0;
            end =0;
            for(int s : stones){
                if(s != 0){
                    js++;
                    if(js>1){
                        break;
                    }
                    end = s;
                }
            }
        }
        return end;
    }
}

h

1

标签:stones,end,1046,int,重量,js,js1,js2,石头
来源: https://blog.csdn.net/SYoooooh/article/details/120923263