leetcode #520: Detect Capital

public class Solution {
    public boolean detectCapitalUse(String word) {
        int count = 0;
        for (char c : word.toCharArray()) {
            if (c < 97) count++;
        }
        int result = count % word.length();

        return result == 0 || ((result == 1) && word.charAt(0) < 97);
    }
}

leetcode #496: Next Greater Element I

  • Use stack
import java.util.HashMap;
import java.util.Stack;

public class Solution {
    public int[] nextGreaterElement(int[] findNums, int[] nums) {
        HashMap&lt;Integer, Integer&gt; hashMap = new HashMap&lt;&gt;();
        Stack&lt;Integer&gt; stack = new Stack&lt;&gt;();

        for (int num : nums) {
            while (!stack.isEmpty() &amp;&amp; stack.peek() &lt; num) hashMap.put(stack.pop(), num);
            stack.add(num);
        }

        int[] result = new int[findNums.length];
        for (int k = 0; k &lt; findNums.length; k++) {
            result[k] = hashMap.getOrDefault(findNums[k], -1);
        }
        return result;
    }
}
  • Do not use stack
class Solution(object):
    def nextGreaterElement(self, findNums, nums):
        """
        :type findNums: List[int]
        :type nums: List[int]
        :rtype: List[int]
        """
        return [next((y for y in nums[nums.index(x):] if y &gt; x), -1) for x in findNums]

leetcode #504: Base 7

public class Solution {
    public String convertToBase7(int num) {
        int base = 1, result = 0;
        while (num != 0) {
            result += base * (num % 7);
            num /= 7;
            base *= 10;
        }
        return String.valueOf(result);
    }
}

leetcode #506: Relative Ranks

import java.util.Arrays;
import java.util.HashMap;

public class Solution {
    public String[] findRelativeRanks(int[] nums) {
        int count = nums.length, tmp[] = new int[count];
        System.arraycopy(nums, 0, tmp, 0, count);
        String[] prize = new String[]{"Gold Medal", "Silver Medal", "Bronze Medal"};

        Arrays.sort(tmp);
        HashMap<Integer, Integer> cache = new HashMap<>();
        for (int n : tmp) cache.put(n, count--);

        String[] result = new String[nums.length];
        for (int i = 0; i < nums.length; i++) {
            int x = cache.get(nums[i]);
            result[i] = x <= 3 ? prize[x - 1] : String.valueOf(x);
        }
        return result;
    }
}

leetcode #485: Max Consecutive Ones

public class Solution {
    public int findMaxConsecutiveOnes(int[] nums) {
        int result = 0, start = 0;
        while (start < nums.length) {
            if (nums[start] == 0) {
                start++;
            } else {
                int tmp = start + 1;
                while (tmp < nums.length && nums[tmp] == 1) tmp++;
                result = Math.max(result, tmp - start);
                start = tmp + 1;
            }
        }
        return result;
    }
}

leetcode #500: Keyboard Row

import java.util.ArrayList;
import java.util.HashMap;

public class Solution {
    public String[] findWords(String[] words) {
        String[] kb = new String[]{"qwertyuiop", "asdfghjkl", "zxcvbnm"};
        HashMap<Integer, Integer> table = new HashMap<>();
        for (int i = 0; i < 3; i++) {
            for (char c : kb[i].toCharArray()) {
                table.put((int) c, i);
                table.put(c - 32, i);
            }
        }

        ArrayList<String> result = new ArrayList<>();
        outer:
        for (String word : words) {
            for (int k = 0; k < word.length() - 1; k++) {
                if (!table.get((int) word.charAt(k)).equals(table.get((int) word.charAt(k + 1)))) continue outer;
            }
            result.add(word);
        }

        return result.toArray(new String[0]);
    }
}

leetcode #86: Partition List

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */

public class Solution {
    public ListNode partition(ListNode head, int x) {
        ListNode a = new ListNode(0), b = new ListNode(0), tmpA = a, tmpB = b;
        while (head != null) {
            if (head.val < x) {
                tmpA.next = head;
                tmpA = tmpA.next;
            } else {
                tmpB.next = head;
                tmpB = tmpB.next;
            }
            head = head.next;
        }
        tmpB.next = null;
        tmpA.next = b.next;
        return a.next;
    }
}

leetcode #448: Find All Numbers Disappeared in an Array

import java.util.ArrayList;
import java.util.List;

public class Solution {
    public List<Integer> findDisappearedNumbers(int[] nums) {
        List<Integer> result = new ArrayList<>();
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] - 1 != i && nums[nums[i] - 1] != nums[i]) {
                int v = nums[i];
                nums[i] = nums[v - 1];
                nums[v - 1] = v;
            }
        }

        for (int k = 0; k < nums.length; k++) {
            if (nums[k] != k + 1) result.add(k + 1);
        }
        return result;
    }
}