LeetCode 竞赛
1.删除字符串中的所有相邻重复项
题目:
给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。
在 S 上反复执行重复项删除操作,直到无法继续删除。
在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。
输入:”abbaca”
输出:”ca”
解释:
例如,在 “abbaca” 中,我们可以删除 “bb” 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 “aaca”,其中又只有 “aa” 可以执行重复项删除操作,所以最后的字符串为 “ca”。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
   | class Solution {         public String removeDuplicates(String S) {             int i = 0;             int j = 0;             boolean flag = true;             while(flag == true){                 flag = false;                 for(i = 0; i < S.length() - 1; i++){                     char t1 = S.charAt(i);                     char t2 = S.charAt(i+1);                     if(t1 == t2){                         j = i + 1;                         flag = true;                         break;                     }                 }                 if(flag == true){                     S = S.substring(0, j - 1) + S.substring(j + 1);                 }             }             return S;         }     }
  | 
 
2.最后一块石头的重量
有一堆石头,每块石头的重量都是正整数。
每一回合,从中选出两块最重的石头,然后将它们一起粉碎。假设石头的重量分别为 x 和 y,且 x <= y。那么粉碎的可能结果如下:
如果 x == y,那么两块石头都会被完全粉碎;
如果 x != y,那么重量为 x 的石头将会完全粉碎,而重量为 y 的石头新重量为 y-x。
最后,最多只会剩下一块石头。返回此石头的重量。如果没有石头剩下,就返回 0。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   | class Solution {         public int lastStoneWeight(int[] stones) {             if(stones.length == 1){                 return stones[0];             }             if(stones.length == 2){                 return Math.abs(stones[0] - stones[1]);             }             while(getLen(stones) > 1){                 int maxIDx = getMax(stones);                 int secIDx = getSec(stones, maxIDx);                 stones[maxIDx] = stones[maxIDx] - stones[secIDx];                 stones[secIDx] = 0;             }             for(int i = 0; i < stones.length; i++){                 if(stones[i] > 0){                     return stones[i];                 }             }             return 0;         }                  public static int getMax(int[] stones){             int idx = 0;             for(int i = 1; i < stones.length; i++){                 if(stones[i] > stones[idx]){                     idx = i;                 }             }             return idx;         }              public static int getSec(int[] stones, int index){             int idx = 0;             int tmp = stones[index];             stones[index] = 0;             for(int i = 1; i < stones.length; i++){                 if(stones[i] > stones[idx]){                     idx = i;                 }             }             stones[index] = tmp;             return idx;         }                  public static int getLen(int[] stones){             int len = 0;             for(int i = 0; i < stones.length; i++){                 if(stones[i] > 0){                     len++;                 }             }             return len;         }     }
  |