Signature: void showOddNumbers() Solution:
void showOddNumbers(){ for(int i = 0; i <= 100; i++) if(i % 2 == 1) System.out.println(i); }Boolean isLeapYear(int year) Solution
Boolean isLeapYear(int year){ return year % 4 == 0 && year % 100 != 0 || year % 400 == 0; }Signature: int factorial(int n) Solution
int factorial(int n){ return n > 1 ? n * factorial(n - 1) : 1; }Signature: int countOnes(int n) Solution:
int countOnes(int n){ int count = 0; while(n > 0){ if(n % 10 == 1) count ++; n /= 10; } return count; }Signature: int roteateInteger(int n)
int countOnes(int n){ int output = 0; while(n > 0){ output = output * 10 + n % 10; n /= 10; } return output ; }Signature: Boolean isPrime(int n)
Boolean isPrime(int n){ for(int i = 2; i < n - 1; i++) if( n % i == 0) return false; return true; }Signature: void reverseArray(int[] arr)
void reverseArray(int[] arr){ for(int i = 0; i < arr.length / 2; i++){ int tmp = arr[i]; arr[i] = arr[arr.length - i - 1]; arr[arr.length - i - 1] = tmp; } }Signature: int[] find(int[] arr)
int[] find(int[] arr){ // 统计所有只出现1次的值 int total = 0; int[] marks = new int[arr.length]; for(int i = 0; i < arr.length; i++) { int occ = 0; for(int j = 0; j < arr.length; j++) if(arr[j] == arr[i]) occ++; if(occ == 1) { total++; marks[i] = 1; } } // 整理输出数组 int[] outarr = new int[total]; int p = 0; for (int i = 0; i < arr.length; i++) if (marks[i] == 1) outarr[p++] = arr[i]; return outarr; }Signature: int locate(String s, char c)
int locate(String s, char c){ for(int i = s.length() - 1; i >=0; i--) if(s.charAt(i) == c) return i; return -1; }Signature: String replace(String s)
String replace(String s){ char[] chs = new char[s.length()]; for(int i = 0; i < chs.length; i++) chs[i] = s.charAt(i) == 'd' ? 'f' : s.charAt(i); return new String(chs); }Signature: String toLower(String s)
String toLower(String s){ char[] chs = new char[s.length()]; for(int i = 0; i < chs.length; i++){ char c = s.charAt(i); chs[i] = c >= 65 && c <= 97 ? (char) (c + 32) : c; } return new String(chs); }Signature: String toUpper(String s)
String toUpper(String s){ char[] chs = new char[s.length()]; for(int i = 0; i < chs.length; i++){ char c = s.charAt(i); chs[i] = c >= 97 && c <= 122 ? (char) (c - 32) : c; } return new String(chs); }Signature: char[] getMiddleCharacters(String s)
char[] getMiddleCharacters(String s){ char[] chs = new char[s.length() % 2 == 1 ? 1 : 2]; if(chs.length==1){ chs[0] = s.charAt(s.length() / 2); }else{ chs[0] = s.charAt(s.length() / 2); chs[1] = s.charAt(s.length() / 2 + 1); } return chs; }Signature: int getLongestConsecutives(int[] arr)
int getLongestConsecutives(int[] arr){ Arrays.sort(arr); int max = 0; for(int i = 0; i < arr.length; i++) max = arr[i+1] == arr[i] + 1 ? max + 1 : 1; return max; }Signature: int getMedian(int[] arr)
int getMedian(int[] arr){ int temp = 0; int size = numbers.length; for(int i = 0 ; i < size - 1; i ++){ for(int j = 0 ;j < size - i - 1 ; j++){ if(numbers[j] > numbers[j+1]) { temp = numbers[j]; numbers[j] = numbers[j + 1]; numbers[j + 1] = temp; } } } return arr[arr.length / 2]; }Signature: Boolean isArmstrongNumber(int n)
Boolean isArmstrongNumber(int n){ int h = n / 100; int t = n / 10 % 10; int d = n % 10; return h * h * h + t * t * t + d * d * d == n; }double getLength(), returns the length of the circle;double getArea(), returns the area of the circle;boolean isContained(Cirlce c), returns a boolean indicating whether Circle c is contained in the circle;boolean isCollidedWith(Cirlce c), returns a boolean indicating whether Circle c is collided with the circle.
class Circle{ double x, y, r; public double getLength(){ return 2 * Math.PI * r;} public double getArea(){ return Math.PI * r * r}; public Boolean isContained(Circle c){ return Math.sqrt((c.x - x)*(c.x - x)+(c.x - x)*(c.x - x)) < r; } public Boolean isCollidedWith(Circle c){ double d = Math.sqrt((c.x - x)*(c.x - x)+(c.x - x)*(c.x - x)); return d < r + c.r && d > Math.Max(r, c.r); } }Signature: int[] getCommonElements(int[] arr1, int[] arr2)
int[] getCommonElements(int[] arr1, int[] arr2){ Arrays.sort(arr1); Arrays.sort(arr2); List<Integer> list = new ArrayList<Integer>(); int i = 0, j = 0; while(i < arr1.length && j < arr2.length) { if(arr1[i] > arr2[j]) j++; else if(arr1[i] < arr2[j]) i++; else{ list.add(arr1[i]); i++; j++; } } return list.toArray(); }Signature: int[] merge(int[] arr1, int[] arr2)
int[] merge(int[] arr1, int[] arr2){ int[] c = new int[arr1.length + arr2.length]; int i = 0, j = 0, k = 0; while(i < arr1.length && j < arr2.length) if(arr1[i] < b[j]) c[k++] = arr1[i++]; else c[k++] = arr2[j++]; while(i < arr1.length) c[k++] = arr1[i++]; while(j<b.length) c[k++] = arr2[j++]; return c; }Signature: void printNumbes(String s)
void printNumbes(String s){ char[] chs = {'a', 'A', 'e', 'E', 'o', 'O', 'i', 'I', 'u', 'U'}; int num_vowels = 0; int num_spaces = 0; for(int i = 0; i < s.length(); i++){ char c = s.charAt(i); boolean existed = false; for(int j = 0; j < chs.length(); j ++){ if(c == chs[j]){ existed = true; break; } } if(!existed) num_vowels++; else if(c == ' ') num_spaces++; } System.out.printf("Number of vowels: %d\nNumber of spaces: %d\n", num_vowels, num_spaces); }