batcoding
JAVA基础代码练习
练习题
Given a string, take the first 2 chars and return the string with the
2 chars added at both the front and back, so "kitten" yields"kikittenki".
If the string length is less than 2, use whatever chars are there.**front22("kitten") → "kikittenki" front22("Ha") → "HaHaHa" front22("abc") → "ababcab"**
my code:
public String front22(String str) {
if(str.length() >= 2){
str = str.substring(0,2) + str + str.substring(0,2);
}
else
str = str + str + str;
return str;
}
solution:
public String formt22(String str){
int len = 2;
if(str.length() < len){
len = str.length();
}
str = str.subString(0 , len) + str + str.subString(0 , len);
}
We'll say that a number is "teen" if it is in the range 13..19 inclusive. Given 2 int values, return true if one or the other is teen, but not both.
**loneTeen(13, 99) → true loneTeen(21, 19) → true loneTeen(13, 13) → false**
my code:
public boolean loneTeen(int a, int b) {
if(a >= 13 && a <=19 && (b < 13 || b > 19))
return true;
if(b >= 13 && b <= 19 && (a < 13 || a > 19))
return true;
return false;
}
sulution:
public boolean longTeen(int a, int b){
boolean teenA = (a >= 13 && a <= 19)
booelan teenB = (b > =13 && b <= 19)
if((teenA && !teenB) || (!teenA && teenB))
return true;
return false;
}
Given a string, we'll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.
**front3("Java") → "JavJavJav" front3("Chocolate") → "ChoChoCho" front3("abc") → "abcabcabc"**
mycode:
public String front3(String str) {
if(str.length() < 3)
return str + str + str;
else
return str.substring(0,3) + str.substring(0,3) + str.substring(0,3);
}
solution:
public String front3(String str) {
String front;
if (str.length() >= 3) {
front = str.substring(0, 3);
}else {
front = str;
}
return front + front + front;
}
Given a string, return a string made of the first 2 chars (if present), however include first char only if it is 'o' and include the second only if it is 'z', so "ozymandias" yields "oz".
**startOz("ozymandias") → "oz" startOz("bzoo") → "z" startOz("oxx") → "o"**
mycode:
public String startOz(String str) {
if(str.equals("") || str.equals("o"))
return str;
else{
if(str.length() == 1)
return "";
else{
char a = str.charAt(0);
char b = str.charAt(1);
String str2 = "";
if(a == 'o')
str2 += "o";
if(b == 'z')
str2 += "z";
return str2;
}
}
}
solution:
public String startOz(String str) {
String result = "";
if (str.length() >= 1 && str.charAt(0)=='o') {
result = result + str.charAt(0);
}
if (str.length() >= 2 && str.charAt(1)=='z') {
result = result + str.charAt(1);
}
return result;
}
Given 2 positive int values, return the larger value that is in the range 10..20 inclusive, or return 0 if neither is in that range.
max1020(11, 19) → 19 max1020(19, 11) → 19 max1020(11, 9) → 11
mycode:
public int max1020(int a, int b) {
if( a >= 10 && a <= 20 && b >= 10 && b <=20)
return a > b ? a : b;
else if( a >= 10 && a <=20 && (b < 10 || b > 20))
return a;
else if((a < 10 || a > 20) && ( b >= 10 && b <= 20))
return b;
else
return 0;
}
solution:
public int max1020(int a, int b) {
// First make it so the bigger value is in a
if (b > a) {
int temp = a;
a = b;
b = temp;
}
// Knowing a is bigger, just check a first
if (a >= 10 && a <= 20) return a;
if (b >= 10 && b <= 20) return b;
return 0;
}
Given a string and a non-negative int n, we'll say that the front of the string is the first 3 chars, or whatever is there if the string is less than length 3. Return n copies of the front;
frontTimes("Chocolate", 2) → "ChoCho" frontTimes("Chocolate", 3) → "ChoChoCho" frontTimes("Abc", 3) → "AbcAbcAbc"
mycode:
public String frontTimes(String str, int n) {
String font = "";
String result = "";
if(str.length() <= 3)
font= str;
else
font = str.substring(0,3);
for(int i = 0 ; i < n ; i++){
result += font;
}
return result;
}
solution:
public String frontTimes(String str, int n) {
int frontLen = 3;
if (frontLen > str.length()) {
frontLen = str.length();
}
String front = str.substring(0, frontLen);
String result = "";
for (int i=0; i<n; i++) {
result = result + front;
}
return result;
}
Given an array of ints, return true if the sequence of numbers 1, 2, 3 appears in the array somewhere.
array123([1, 1, 2, 3, 1]) → true array123([1, 1, 2, 4, 1]) → false array123([1, 1, 2, 1, 2, 3]) → true 这道题基本上是我读错提了,愿意是出现连续的1,2,3,我把他理解成了,只要数组中存在元 素1,2,3就符合条件,针对这种理解方式,给出了下面的解题方式之一:
mycode:
public static boolean array123(int[] nums) {
if(nums == null || nums.length < 1)
return false;
int max = nums[0]; //获取最大值
for(int i = 1 ; i < nums.length ; i++){
if(nums[i] > max)
max = nums[i];
}
if(max < 3) //最大数小于3时,一定不满足条件
return false;
int [] array = new int [max + 1]; //把数组的值与下标互换,如array[10] = 99 , 改成 array[99] == 1,表示存在99这个数
for(int j = 0 ; j < nums.length ; j++){
array[nums[j]] = 1;
}
if(array[1] == 1 && array[2] == 1 && array[3] == 1)
return true;
else
return false;
}
Given a string, if the first or last chars are 'x', return the string without those 'x' chars, and otherwise return the string unchanged.
withoutX("xHix") → "Hi" withoutX("xHi") → "Hi" withoutX("Hxix") → "Hxi"
mycode:
public String withoutX(String str) {
int place = str.length();
if(place == 0)
return str;
if(str.equals("x"))
return "";
else{
if(str.substring(0,1).equals("x"))
str= str.substring(1);
if(str.substring(str.length()-1).equals("x"))
str = str.substring(0 , str.length()-1);
return str;
}
}
solution:
public String withoutX(String str) {
if (str.length() > 0 && str.charAt(0) == 'x') {
str = str.substring(1);
}
if (str.length() > 0 && str.charAt(str.length()-1) == 'x') {
str = str.substring(0, str.length()-1);
}
return str;
// Solution notes: check for the 'x' in both spots. If found, use substring()
// to grab the part without the 'x'. Check that the length is greater than 0
// each time -- the need for the second length check is tricky to see.
// One could .substring() instead of .charAt() to look into the string.
}
知识点积累
1.String字符串的大小写转换
str.toUpperCase(); return String
str.toLowerCase();
2.String已固定字符串传开始/结束是否与指定字符串匹配
str.startsWith(String); return boolean
str.endsWith(String);
都有个s不要漏掉了
3.获取String上指定位置的char
str.charAt(int); return char;
3.替换String中的内容
str.replace(String,String); return String;
str.replaceAll(reg , String);
replace中第一个参数是字符串,replaceAll中第一个参数是正则表达式,他们都是替换字符串中全部的子串
4.查找String中子串的位置
str.indexOf(String); return int; //返回第一个子串开始的位置
str.indexOf(String, int); //从指定位置开始查找第一个子串的位置
str.lastIndexOf(String); //从后开始查找第一个子串出现的位置
str.lastIndexOf(String , int);//从后开始指定位置前第一个子串出现的位置
5.获取两个数
Math.min(int a, int b) //获取较小的数
Math.max(int a, int b) //获取较大的数
6.字符串截取
str.substring(int , int)
str.substring(int)
第一个参数表示的是字符在字符串中的位置,且范围为 0 - str.length,如果取最大值str.length,则表示什么都不截取到,而不是报数组越界的异常