1. Excel table column name

Ideas:

class Solution {
    public String convertToTitle(int columnNumber) {
        StringBuffer sb = new StringBuffer();
        while(columnNumber ! =0) {
            columnNumber--;
            sb.append((char)(columnNumber % 26 + 'A'));
            columnNumber /= 26;
        }
        returnsb.reverse().toString(); }}Copy the code
  1. Most of the elements

Ideas:

class Solution {
    public int majorityElement(int[] nums) {
        if (nums.length == 1) {
            return nums[0];
        }
        int count = 1;
        int number = nums[0];
        for (int i = 1; i < nums.length; i++) {
            if (nums[i] == number) {
                count++;
            } else {
                if (count == 0) {
                    number = nums[i];
                    count = 1;
                } else{ count--; }}}returnnumber; }}Copy the code
  1. Excel table number

Thinking:…

class Solution {
    public int titleToNumber(String columnTitle) {
        int result = 0;
        int pow = 0;
        for (int i = columnTitle.length() - 1; i >= 0; i--) {
            result += (((int) columnTitle.charAt(i)) - 64) * Math.pow(26, pow++);
        }
        returnresult; }}Copy the code