java学得啥也不是,暂停codewars,先充充电
Sorted? yes? no? how?(7kyu)
Complete the method which accepts an array of integers, and returns one of the following:
“yes, ascending” - if the numbers in the array are sorted in an ascending order
“yes, descending” - if the numbers in the array are sorted in a descending order
“no” - otherwise
You can assume the array will always be valid, and there will always be one correct answer.
完成接受整数数组的方法,并返回以下内容之一:
“是,升序”-如果数组中的数字按升序排序
“是,降序”-如果数组中的数字按降序排序
“否”-否则
您可以假设数组总是有效的,并且总是有一个正确的答案。
Solution:
class Solution {
public static String isSortedAndHow(int[] array) {
return null;
}
}
Sample Tests:
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SolutionTest {
@Test
public void test1() {
int[] array = new int[] {1, 2};
assertEquals("yes, ascending", Solution.isSortedAndHow(array));
}
@Test
public void test2() {
int[] array = new int[] {15, 7, 3, -8};
assertEquals("yes, descending", Solution.isSortedAndHow(array));
}
@Test
public void test3() {
int[] array = new int[] {4, 2, 30};
assertEquals("no", Solution.isSortedAndHow(array));
}
}
大佬的答案1:
class Solution {
public static String isSortedAndHow(int[] array) {
boolean desc = false;
boolean asc = false;
for(int i = 1; i < array.length; i++) {
if(array[i] > array[i-1]) asc = true;
if(array[i] < array[i-1]) desc = true;
}
if(asc && !desc) return "yes, ascending";
if(desc && !asc) return "yes, descending";
return "no";
}
}
大佬的答案2:
class Solution {
public static String isSortedAndHow(final int[] array) {
final boolean ascending = array[0] < array[1];
for (int i = 2; i < array.length; i++) {
if (ascending ? (array[i - 1] > array[i]) : (array[i - 1] < array[i])) {
return "no";
}
}
return String.format("yes, %s", ascending ? "ascending" : "descending");
}
}
Palindrome chain length(7kyu)
Number is a palindrome if it is equal to the number with digits in reversed order. For example, 5, 44, 171, 4884 are palindromes, and 43, 194, 4773 are not.
Write a function which takes a positive integer and returns the number of special steps needed to obtain a palindrome. The special step is: “reverse the digits, and add to the original number”. If the resulting number is not a palindrome, repeat the procedure with the sum until the resulting number is a palindrome.
If the input number is already a palindrome, the number of steps is 0.
All inputs are guaranteed to have a final palindrome which does not overflow long.
如果数字等于以相反顺序排列的数字,则该数字为回文。例如,5、441714884是回文,431944773不是。
编写一个函数,该函数接受一个正整数,并返回获取回文所需的特殊步骤数。特殊的步骤是:“倒数,加上原来的数字”。如果生成的数字不是回文,则对总和重复此过程,直到生成的数字是回文。
如果输入编号已经是回文,则步骤数为0。
所有输入都保证有一个不会溢出很长时间的最终回文。
Example
For example, start with 87:
87 + 78 = 165 - step 1, not a palindrome
165 + 561 = 726 - step 2, not a palindrome
726 + 627 = 1353 - step 3, not a palindrome
1353 + 3531 = 4884 - step 4, palindrome!
4884 is a palindrome and we needed 4 steps to obtain it, so answer for 87 is 4.
4884是一个回文,我们需要4个步骤来获得它,所以87的答案是4。
Solution:
public class Palindromes {
public static int palindromeChainLength (long n) {
return 0;
}
}
Sample Tests:
import org.junit.Test;
import static org.junit.Assert.*;
public class PalindromesTest {
@Test
public void testPalindrome() {
assertEquals(0, Palindromes.palindromeChainLength(1));
assertEquals(0, Palindromes.palindromeChainLength(88));
assertEquals(0, Palindromes.palindromeChainLength(393));
}
@Test
public void testNonPalindrome() {
assertEquals(1, Palindromes.palindromeChainLength(10));
assertEquals(1, Palindromes.palindromeChainLength(134));
assertEquals(4, Palindromes.palindromeChainLength(87));
assertEquals(7, Palindromes.palindromeChainLength(2897));
assertEquals(24, Palindromes.palindromeChainLength(89));
}
}
大佬的答案:
public class Palindromes {
public static int palindromeChainLength (long n) {
String ns = "" + n, nrs = "" + new StringBuilder(ns).reverse();
return ns.equals(nrs) ? 0 : 1 + palindromeChainLength(n + Long.valueOf(nrs));
}
}