(Easy) Last Stone Weight LeetCode

class Solution {
    public int lastStoneWeight(int[] stones) {

        int len = stones.length;
        int i = len -1;
        int minus = 0;
        int remain = len;

       if(stones.length ==1){

           return stones[0];
       } 

        else {

            Arrays.sort(stones);

          do{
                if(stones[i]==stones[i-1]){

                    stones[i]   = 0;
                    stones[i-1] = 0;

                    remain = remain -2;
                }

                else{

                    stones[i-1]= stones[i]-stones[i-1];
                    stones[i]=0;

                   remain = remain -1;
                }

                Arrays.sort(stones);

          }
          while(remain >1);  

            return stones[len-1];
        }

    }
}

原文地址:https://www.cnblogs.com/codingyangmao/p/11278051.html

时间: 2024-08-30 14:02:44

(Easy) Last Stone Weight LeetCode的相关文章

LeetCode 1049. Last Stone Weight II

原题链接在这里:https://leetcode.com/problems/last-stone-weight-ii/ 题目: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose any two rocks and smash them together.  Suppose the stones have weights x and y with x <= y. 

LeetCode 1046. Last Stone Weight

原题链接在这里:https://leetcode.com/problems/last-stone-weight/ 题目: We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose the two heaviest rocks and smash them together.  Suppose the stones have weights x and y with x

LeetCode 1046. Last Stone Weight (最后一块石头的重量 )

题目标签:Greedy 利用priority queue, 把石头重量都存入 pq, 每次取最大两个比较,存入差值,直到pq 只剩最后一个. Java Solution: Runtime:  1 ms, faster than 92.5% Memory Usage: 37.1 MB, less than 100% 完成日期:02/14/2020 关键点:priority queue class Solution { public int lastStoneWeight(int[] stones)

1046. Last Stone Weight

We have a collection of rocks, each rock has a positive integer weight. Each turn, we choose the two heaviest rocks and smash them together.  Suppose the stones have weights x and y with x <= y.  The result of this smash is: If x == y, both stones ar

(Easy) Diet Plan Performance LeetCode Contest

Description: 5174. Diet Plan Performance My SubmissionsBack to Contest User Accepted:0 User Tried:0 Total Accepted:0 Total Submissions:0 Difficulty:Easy A dieter consumes calories[i] calories on the i-th day.  For every consecutive sequence of k days

(Easy) To Lower Case LeetCode

Description Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. Example 1: Input: "Hello" Output: "hello" Example 2: Input: "here" Output: "here" Example 3: In

(Easy) Occurences After Bigram LeetCode

Description: Given words first and second, consider occurrences in some text of the form "first second third", where second comes immediately after first, and third comes immediately after second. For each such occurrence, add "third"

(Easy) Flipping an Image LeetCode

Description: Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed.  For example, flipping [1, 1, 0] horizontally re

(Easy) Find Common Characters LeetCode

Description Given an array A of strings made only from lowercase letters, return a list of all characters that show up in all strings within the list (including duplicates).  For example, if a character occurs 3 times in all strings but not 4 times,