LeetCode 171 JAVA

public class Solution {
    private static Scanner sc = null;
    private static final int RADIX = 26;

    enum Alphabet {
        A(1), B(2), C(3), D(4), E(5), F(6), G(7), H(8), I(9), J(10), K(11), L(12), M(13), N(14), O(15), P(16), Q(17),
        R(18), S(19), T(20), U(21), V(22), W(23), X(24), Y(25), Z(26);

        private int value;

        private Alphabet(int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(int value) {
            this.value = value;
        }

        public int getValueByName(String c) {
            for (Alphabet alp : Alphabet.values()) {
                if (alp.name().equals(c)) {
                    return alp.value;
                }
            }
            return 0;
        }
    }

    public int titleToNumber(String str) {
        int result = 0;
        int alpLength = str.length() - 1;
        for (int i = 0; i <= alpLength; i++) {
            result = result + Alphabet.valueOf(String.valueOf(str.charAt(alpLength - i))).value
                * (int) Math.pow(RADIX, i);
        }
        return result;
    }
}

时间: 2024-08-01 23:15:57

LeetCode 171 JAVA的相关文章

Linked List Cycle leetcode II java (寻找链表环的入口)

题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Follow up: Can you solve it without using extra space? 题解: 这个连同I都是很经典的题啦,刷CC150时候就折磨了半天. 其实就推几个递推公式就好..首先看图(图引用自CC150): 从链表起始处到环入口长度为:a,从环入口到Faster和Sl

leetcode Sudoku java

package com.sogou.hadoop.test; public class Sudoku { /**验证该值是否合法*/ public boolean isValidSudoku(char[][] board,int x,int y){ int row,col; //same value in the same column for(row=0;row<9;row++){ if((x!=row)&&(board[row][y]==board[x][y])){ return

leetcode中Java关于Json处理的依赖

leetcode的java代码提供的main函数中,往往有关于json的依赖...我找了许久才找到他们用的是这个json实现 <dependency> <groupId>com.eclipsesource.minimal-json</groupId> <artifactId>minimal-json</artifactId> <version>0.9.5</version> </dependency> 原文地址

一天三题LeetCode(C++&amp;JAVA)-1~3

转载请注明出处: http://blog.csdn.net/miaoyunzexiaobao 1.      Two Sum https://leetcode.com/problems/two-sum/ 给定一个数组,在其中找两个数,使两个数之和为给定的target.返回这两个数在数组中的位置.两个数字的位置分别用index1和index2表示,要求index1<index2.例: Input: numbers={2, 7, 11, 15}, target=9 Output: index1=1,

【leetcode with java】32 Longest Valid Parentheses O(n)

这个题目leetcode上提示用动态规划,但是那样要O(n^2).我自己想出了一个O(n)的算法,并提交通过. [题目] Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring

一天三题LeetCode(C++&amp;JAVA)-4~6

转载请注明出处: http://blog.csdn.net/miaoyunzexiaobao 4.     Median ofTwo Numbers https://leetcode.com/problems/median-of-two-sorted-arrays/ 给定两个有序数组A(长度为m)和B(长度为n),找到两个数组的中位数.若两个数组长度和为偶数,则返回中间两个数的平均值.例:A={1,5,14},B={3,7,30},则返回(5+7)/2=6.A={1,5,14},B={3,7},

[leetcode]multiply-strings java代码

题目: Given two numbers represented as strings, return multiplication of the numbers as a string. Note: The numbers can be arbitrarily large and are non-negative. 给两个用字符串表示的数字,求它们的乘积. 注意:数字非负,且可以无限大 ------------------------ 之前未考虑数字可以无限大的情况,采用的方法是 将字符串转

【leetcode with java】18 4Sum (On^2)

我看了几个人气比较高的博客,他们这个算法都没做到O(n^2),所以提前将我的解法贴出来分享,供大家参考(前面略过的题目近期都会补上的). [题目]: Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target

Leetcode 171 Excel Sheet Column Number 难度:0

https://leetcode.com/problems/excel-sheet-column-number/ class Solution { public: int titleToNumber(string s) { int ans = 0; for(int i = 0;i < s.length();i++) { ans *= 26; ans += s[i] - 'A' + 1; } return ans; } };