leetcode [#168] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (8)
目录1. 题目2. 解决方案3. 注意事项
题目Given a positive integer, return its corresponding column title as appear in an Excel sheet.
Example:1 -> A2 -> B3 -> C…26 -> Z27 -> AA28 -> AB 
解决方案123456789101112131415161718public class Solution {    public String convertToTitle(int n) {        if(n <= 0) return "";        List<Integer> list = new ArrayList<>();        while(n > 0){            if(n % 26 == 0) {                list.add(26);                n = n / 26 - 1;            } else {                list.add(n ...阅读全文

leetcode [#165] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (8)
目录1. 题目2. 解决方案3. 注意事项
题目Compare two version numbers version1 and version2.If version1 > version2 return 1, if version1 < version2 return -1, otherwise return 0.
You may assume that the version strings are non-empty and contain only digits and the . character.The . character does not represent a decimal point and is used to separate number sequences.For instance, 2.5 is not “two and a half” or “half way to version three”, it is the fifth second-level revision of the second first-level revision....阅读全文

leetcode [#169] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (5)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.
You may assume that the array is non-empty and the majority element always exist in the array.
解决方案123456public class Solution {    public int majorityElement(int[] nums) {        Arrays.sort(nums);        return nums[nums.length / 2];    }}注意事项将数组排序,返回下标为 N/2 的元素即可。...阅读全文

leetcode [#155] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (9)
目录1. 题目2. 解决方案3. 注意事项
题目Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
push(x) – Push element x onto stack.pop() – Removes the element on top of the stack.top() – Get the top element.getMin() – Retrieve the minimum element in the stack.ExampleMinStack minStack = new MinStack();minStack.push(-2);minStack.push(0);minStack.push(-3);minStack.getMin();   –> Returns -3.minStack.pop();minStack.top();      –> Returns 0.minStack.getMin();   –> Returns ...阅读全文

leetcode [#167] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (9)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.
You may assume that each input would have exactly one solution.
Example:Input: numbers={2, 7, 11, 15}, target=9Ou...阅读全文