leetcode [#58] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目Given a string s consists of upper/lower-case alphabets and empty space characters ‘ ‘, return the length of last word in the string.
If the last word does not exist, return 0.
ExampleGiven s = “Hello World”,return 5.
NoteA word is defined as a character sequence consists of non-space characters only.
解决方案1234567891011public class Solution {    public int lengthOfLastWord(String s) {        if(s == null || s.length() == 0) return 0;        String[] arr = s.split(" ")...阅读全文

leetcode [#66] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (14)
目录1. 题目2. 解决方案3. 注意事项
题目Given a non-negative number represented as an array of digits, plus one to the number.
The digits are stored such that the most significant digit is at the head of the list.
解决方案12345678910111213141516171819202122232425262728293031323334353637383940public class Solution {    public static int[] plusOne(int[] digits) {        int N = digits.length;        int[] result = handle(digits, N - 1);        return result;    }    private static int[] handle(int[] a, int index){...阅读全文

leetcode [#7] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目Reverse digits of an integer.
Example1: x = 123, return 321Example2: x = -123, return -321
解决方案1234567891011public class Solution {    public int reverse(int x) {        String s = String.valueOf(x);        if(x < 0) s = s.substring(1, s.length());        StringBuilder builder = new StringBuilder();        for(int i = s.length() - 1; i>=0; i--) builder.append(s.charAt(i));        if((x > 0 && builder.length() == 10 && builder.toString().compareTo("2147483647") > 0) || ...阅读全文

leetcode [#75] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (27)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.
Here, we will use the integers 0, 1, and 2 to represent the color red, white, and blue respectively.
Note:You are not suppose to use the library’s sort function for this problem.
解决方案1234567891011121314151617181920212223242526272829303132public class Solution {    public void sortColors(int[] nums) {     ...阅读全文

leetcode [#67] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目Given two binary strings, return their sum (also a binary string).
Examplea = “11”b = “1”Return “100”.
解决方案123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960public class Solution {    public String addBinary(String a, String b) {        int len1 = a.length();        int len2 = b.length();        int delta = Math.abs(len1 - len2);        int max = Math.max(len1, len2);        int[] intVal1 = new int[max];     ...阅读全文