本文主要是介绍SPD BANK,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1. 大小写转换
if (character >= 'a' && character <= 'z') {// 将小写字母转换为大写字母character = (char) (character - 32);
} else {// 将大写字母转换为小写字母character = (char) (character + 32);
}
class Solution {public String toLowerCase(String str) { String result = "";for (int i = 0; i < str.length(); i ++) {char a = str.charAt(i);if (a >= 'A' && a <= 'Z') {a = (char)(a + 32);} result = result + a;}return result;}
}
public static String upLowCase(String str){return str.toLowerCase();
}public static String lowUpCase(String str){return str.toUpperCase();
}
2. 判断闰年
class Solution {public boolean leapYear(int input) { if (input % 4 == 0 && input % 100 != 0) {return true;} else if (input % 400 == 0) {return true;}return false;}
}
public boolean isleapyear(int year){return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) ;
}
3. 判断素数/质数
public boolean isPrimeNumber(int num){if(num == 2) return true;//2特殊处理if(num < 2 || num % 2 == 0) return false;//识别小于2的数和偶数for(int i=3; i<=Math.sqrt(num); i+=2){if(num % i == 0){//识别被奇数整除return false;}}return true;
}
public boolean isPrimeNumber(int num){if (num < 2) return 0;for(int i = 2; i <= num-1; i ++){if(num % i == 0){return 0;}}return 1;
}
4. 反转字符串
class Solution {public void reverseString(char[] s) { int i = 0, j = s.length - 1;while (i < j) {char tmp = s[i];s[i] = s[j];s[j] = tmp;i ++;j --;} }
}
class Solution {public void reverseString(char[] s) { for(int i=0;i<s.length/2;i++) {s[s.length-1-i] ^= s[i];s[i] ^= s[s.length-1-i];s[s.length-1-i] ^= s[i];}}
}
public String reverse(String str){StringBuffer buffer = new StringBuffer(str);return buffer.reverse().toString();
}
5. 数组的冒泡排序
class Solution {public void BubbleSort(int[] array) {for (int i = 0; i < array.length - 1; i ++) {for (int j = 0; j < array.length - 1 - i; j ++) {if (array[j] > array[j+1]) {int tmp = array[j];array[j] = array[j+1];array[j] = tmp;} }}}
}
6. 数组的快速排序
class Solution {public void quickSort(int[] array, int left, int right) {int middle = array[left];int start = left;int end = right;while(start < end){while(start < end && middle <= array[end]){end--;}array[start] = array[end];while(start < end && middle > array[start]){start++;}array[end] = array[start];}array[start] = middle;if(start > left) quickSort(array, left, start-1);if(end < right) quickSort(array, end+1, right);}}
7. 找出数组中的偶数,并输出和
public long selectEvenNumber(int number) {int sum = 0;while (number > 0) {int remainder = number % 10;if (remainder % 2 ==0) {sum += remainder;}number = number / 10;}return sum;
}
public static int sum(String str){int sum = 0 ;for(int i = 0 ; i < str.length() ; i ++){int num = Integer.parseInt(str.charAt(i)+"");if( num % 2 == 0)sum += num;}return sum;
}public static void main(String args[]){Scanner scann = new Scanner(System.in);String str = scann.nextLine();scann.close();System.out.println(sum(str));
}
8. 爬台阶问题
public class Climb {public int Climb(int stairNumber) {if (stairNumber <= 2) {return stairNumber;} else {return Climb(stairNumber-1) + Climb(stairNumber-2);} }
}
动态规划算法
class Solution {public int climbStairs(int n) {int[] result = new int[n+1];result[0] = 1;result[1] = 1;for (int i = 2; i <= n; i ++) {result[i] = result[i-1] + result[i-2];}return result[n];}
}
9. 加密问题 - 将 第 i 个字符修改为 第 26+i-1 个字符
public String Char26i1(String str) {String newStr = "";for (int i = 0; i < str.length(); i ++) {if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {newStr += (char) ('A' + 'Z' - str.charAt(i));} else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {newStr += (char) ('a' + 'z' - str.charAt(i));}}return newStr;
}
public String Char26i1_2(String str) {StringBuilder sb = new StringBuilder(str);for (int i = 0; i < str.length(); i ++) {if (str.charAt(i) >= 'A' && str.charAt(i) <= 'Z') {sb.setCharAt(i, (char) ('A' + 'Z' - str.charAt(i)));} else if (str.charAt(i) >= 'a' && str.charAt(i) <= 'z') {sb.setCharAt(i, (char) ('a' + 'z' - str.charAt(i)));}}str = sb.toString();return str;
}
10. ASCII
A B ... Z
65 66 ... 90a b ... z
97 98 ... 122
11. 凯撒密码
public class KaiSa {public String Encryption(String str, int key) { String newStr = "";for (int i = 0; i < str.length(); i++) {char e = str.charAt(i);if (e >= 'a' && e <= 'z') {e = (char) (e + key);if (e > 'z') {e = (char) (e - 26);}} else if (e >= 'A' && e <= 'Z') {e = (char) (e + key);if (e > 'Z') {e = (char) (e - 26);}}newStr = newStr + e;}return newStr;}public String Decryption(String str, int key) {String newStr = "";for (int i = 0; i < str.length(); i++) {char e = str.charAt(i);if (e >= 'a' && e <= 'z') {e = (char) (e - key);if (e < 'a') {e = (char) (e + 26);}} else if (e >= 'A' && e <= 'Z') {e = (char) (e - key);if (e < 'A') {e = (char) (e + 26);}}newStr = newStr + e;}return newStr;}
}
12. 1 + 2/3 + 3/5 + 4/7 + 5/9 + ...
public float FractionSum(int n) {float sum = 0;for (int i = 1; i <= n; i ++) {sum += (i * 1.0) / (2.0 * i - 1.0);}return sum;}
13. 输入一组数N和数字b,求出改组数字中能被 b 整除的个数
public static int div(String str,int b){int count = 0 ;String [] strArr = str.split(" ");for(int i = 0 ; i < strArr.length ; i++ ){int num = Integer.parseInt(strArr[i]);if( num % b == 0)count++;}return count;
}public static void main(String args[]){Scanner scann = new Scanner(System.in);String str = scann.nextLine();int b = scann.nextInt();scann.close();System.out.println(div(str,b));
}
14. 判断一个数是不是平方数
public static boolean isSqrtSum (int num) {int i = 1;while (num > 0) {num = num - i;i = i + 2;}return num == 0;
}
public static boolean isSqrtSum (int num) {for (int i = 1; num > 0; i += 2) {num -= i;}return num == 0;
}
15. 取出一段字符中每个单词的首字母,并转化为大写字母输出
public static String FirstLetterTransfer(String str){String[] strArray = str.split(" ");String newStr = ""; for (int i = 0; i < strArray.length; i ++) {char e = strArray[i].charAt(0);if ((e >= 97) && (e <= 122)) {e = (char) (e - 32);newStr += e;}}return newStr;
}
16. 给定年月日,求出是这一年的第几天?
public static int NumberOfDays(int year, int month, int day) {int days = 0;switch (month - 1) {case 12 : days += 31;case 11 : days += 30;case 10 : days += 31;case 9 : days += 30;case 8 : days += 31;case 7 : days += 31;case 6 : days += 30;case 5 : days += 31;case 4 : days += 30;case 3 : days += 31;case 2 : days += 28;case 1 : days += 31;}days += day;if (((year / 4 == 0) && (year / 100 != 0)) || (year / 400 == 0)){days ++;}return days;
}
17. 小球从100米的高空下落,每次弹起之前下落距离的一半,求第n次落地的总距离
public static double BallDrop(int n){double high = 100.0d;double sum = 100;for (; n > 1; n --) {high = high / 2;sum += 2 * high;}return sum;
}
18. 从数组中找出仅出现一次的数
public static int OnlyOnce(int[] array){ for (int i = 0; i < array.length; i++) {int count = 0;for (int j = 0; j < array.length; j++) {if (array[i] == array[j]){count ++;}}if (count == 1) {return array[i];}}return -1;
}
19. 求第一个字符串出现,但在第二个字符串中没有出现过的字符
public static String UniqueStr(String strA, String strB){String newStr = "";for (int i = 0; i < strA.length(); i++) {String e = strA.charAt(i) + "";if (!strB.contains(e) && !newStr.contains(e)) {newStr += e;}}return newStr;
}
20. 去除字符串最后的空字符
public static String RemoveStrSpace(String str) {int i = str.length() - 1;for (; i >= 0; i--) {if (str.charAt(i) != ' '){break;}}return str.substring(0, i + 1);
}
21.
这篇关于SPD BANK的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!