leetcode [#258] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (19)
目录1. 题目2. 解决方案3. 注意事项
题目Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.
For example:
Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.
解决方案12345678910111213141516public class Solution {    public int addDigits(int num) {        int result;        if (num < 10) {            result = num;        } else {            String num2str = Integer.toString(num);            int newNum = 0;            f...阅读全文

leetcode [#27] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (21)
目录1. 题目2. 解决方案3. 注意事项
题目Given an array and a value, remove all instances of that value in place and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example:Given input array nums = [3,2,2,3], val = 3
Your function should return length = 2, with the first two elements of nums being 2.
解决方案12345678910111213141516171819202122232425...阅读全文

leetcode [#26] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (16)
目录1. 题目2. 解决方案3. 注意事项
题目Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this in place with constant memory.
Example:Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.
解决方案12345678910111213141516171819202122232425262728pub...阅读全文

leetcode [#28] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (18)
目录1. 题目2. 解决方案3. 注意事项
题目Implement strStr().
Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.
解决方案123456789101112131415161718192021222324252627282930public class Solution {    public int strStr(String haystack, String needle) {        int result = -1;        int lenHaystack = haystack.length();        int lenHayNeedle = needle.length();        if(lenHayNeedle > lenHaystack) return -1;        if(lenHayNeedle == 0) return 0;        char[] ...阅读全文

leetcode [#263] | GCidea's blog

作者:JerryXia | 发表于 , 阅读 (12)
目录1. 题目2. 解决方案3. 注意事项
题目Write a program to check whether a given number is an ugly number.
Ugly numbers are positive numbers whose prime factors only include 2, 3, 5.
ExampleFor example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7.
Note that 1 is typically treated as an ugly number.
解决方案123456789101112public class Solution {    public boolean isUgly(int num) {        if(num <= 0) return false;        if(num == 1) return true;        for (int i = 2; i <= 5 && ...阅读全文