给定字符串str,任务是在不使用库方法的情况下在Java中查找字符串是否是回文。
例子:
输入: str =“abba”
输出:是
输入: str =“geeks”
输出:不
下面是我写的答案(我用的split不知道算不算对)
public class Palindrome {
public static void main(String[] args) {
/**
给定字符串str,任务是在不使用库方法的情况下在Java中查找字符串是否是回文。
例子:
输入: str =“abba”
输出:是
输入: str =“geeks”
输出:不
*/
boolean bool= getPalindrome("geeks");
System.out.println(bool);
}
static boolean getPalindrome(String str){
String[] strs = str.split("");
boolean bool = true;
for (int i = 0; i < strs.length/2; i++) {
if(!strs[i].equals(strs[strs.length -i-1])){
bool = false;
}
}
return bool;
}
}
下面是网上的答案:
public class GFG2 {
// Function that returns true if
// str is a palindrome
static boolean isPalindrome(String str)
{
// Pointers pointing to the beginning
// and the end of the string
int i = 0, j = str.length() - 1;
// While there are characters toc compare
while (i < j) {
// If there is a mismatch
if (str.charAt(i) != str.charAt(j))
return false;
// Increment first pointer and
// decrement the other
i++;
j--;
}
// Given string is a palindrome
return true;
}
// Driver code
public static void main(String[] args)
{
String str = "geeks";
if (isPalindrome(str))
System.out.print("Yes");
else
System.out.print("No");
}
}
分析:对String的方法没掌握 至少没想到 charAt
这个方法!
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
给定一个小写英文字母的字符串str,任务是检查字符串是否是元音素数。如果字符串中的所有元音仅出现在主要索引处,则称该字符串为元音素数。
例子:
输入: str =“geeksforgeeks”
输出:没有
str [1] ='e’是元音但1不是素数。
输入: str =“bcae”
输出:是
所有元音都在素数指数,即2和3。
这个本人没有写出自己的答案 因为对素数的不了解 ;
网上的答案:
import java.util.Arrays;
public class GFG3 {
// Function that returns true
// if c is a vowel
static boolean isVowel(char c)
{
if (c == 'a' || c == 'e' ||
c == 'i' || c == 'o' ||
c == 'u')
return true;
return false;
}
// Function that returns true if all the vowels in
// the given string are only at prime indices
static boolean isVowelPrime(String str, int n)
{
// Create a boolean array "prime[0..n]" and
// initialize all entries it as true.
// A value in prime[i] will finally be false
// if i is Not a prime, else true.
boolean []prime = new boolean[n];
Arrays.fill(prime, true);
/*
*用法1:接受2个参数
Arrays.fill( a1, value );
注:a1是一个数组变量,value是一个a1中元素数据类型的值,作用:填充a1数组中的每个元素都是value
例如:
boolean[] a1 = new boolean[5];
Arrays.fill( a1,true );
结果 a1[] = {true,true,true,true,true};
用法2:接受4个参数
例如:
String[] a9 = new String[6];
Arrays.fill(a9, "Hello");
Arrays.fill(a9, 3, 5,"World");
结果是 a9[] = {Hello,Hello,Hello,World,World,Hello};
第一个参数指操作的数组,第二个和第三个指在该数组的某个区域插入第四个参数,第二个参数指起始元素下标(包含该下标),第三个参数指结束下标(不包含该下标),注意:java的数组下标从0开始 */
// 0 and 1 are not prime
prime[0] = false;
prime[1] = false;
for (int p = 2; p * p < n; p++)
{
// If prime[p] is not changed,
// then it is a prime
if (prime[p] == true)
{
// Update all multiples of p greater than or
// equal to the square of it
// numbers which are multiple of p and are
// less than p^2 are already been marked.
for (int i = p * p; i < n; i += p)
prime[i] = false;
}
}
// For every character of the given string
for (int i = 0; i < n; i++)
{
// If current character is vowel
// and the index is not prime
if (isVowel(str.charAt(i)) && !prime[i])
return false;
}
return true;
}
// Driver code
public static void main(String[] args)
{
String str = "bcaes";
int n = str.length();
if (isVowelPrime(str, n))
System.out.println("Yes");
else
System.out.println("No");
}
}
// This code is contributed by Princi Singh
科普素数:素数
素数是这样的整数,它除了能表示为它自己和1的乘积以外,不能表示为任
何其它两个整数的乘积。例如,15=3*5,所以15不是素数;又如,12
=6*2=4*3,所以12也不是素数。另一方面,13除了等于13*1以
外,不能表示为其它任何两个整数的乘积,所以13是一个素数。
有的数,如果单凭印象去捉摸,是无法确定它到底是不是素数的。有些数则
可以马上说出它不是素数。一个数,不管它有多大,只要它的个位数是2、4、
5、6、8或0,就不可能是素数。此外,一个数的各位数字之和要是可以被3
整除的话,它也不可能是素数。但如果它的个位数是1、3、7或9,而且它的
各位数字之和不能被3整除,那么,它就可能是素数(但也可能不是素数)。没
有任何现成的公式可以告诉你一个数到底是不是素数。你只能试试看能不能将这
个数表示为两个比它小的数的乘积。
找素数的一种方法是从2开始用“是则留下,不是则去掉”的方法把所有的
数列出来(一直列到你不想再往下列为止,比方说,一直列到10,000)。
第一个数是2,它是一个素数,所以应当把它留下来,然后继续往下数,每隔一
个数删去一个数,这样就能把所有能被2整除、因而不是素数的数都去掉。在留
下的最小的数当中,排在2后面的是3,这是第二个素数,因此应该把它留下,
然后从它开始往后数,每隔两个数删去一个,这样就能把所有能被3整除的数全
都去掉。下一个未去掉的数是5,然后往后每隔4个数删去一个,以除去所有能
被5整除的数。再下一个数是7,往后每隔6个数删去一个;再下一个数是11
,往后每隔10个数删一个;再下一个是13,往后每隔12个数删一个。……
就这样依法做下去。
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
给定N个瓶子,其中一个瓶子中毒。因此,我们的任务是找出识别有毒瓶子所需要的最小数量的老鼠。老鼠一次可以喝很多瓶。
Input: N = 4
Output: 2
Input: N = 100
Output: 7
Input: N = 1025
Output: 11
网上找到java版本的答案 这个是我写的
/*
* 给定N个瓶子,其中一个瓶子中毒。因此,我们的任务是找出识别有毒瓶子所需要的最小数量的老鼠。老鼠一次可以喝很多瓶。
* Input: N = 4
Output: 2
Input: N = 100
Output: 7
Input: N = 1025
Output: 11
* */
//自己分析这是一个二分法的问题
public class Mouse {
public static void main(String[] args) {
int n = 4;
boolean bool = true;
int number = 0;
//这里添加这个判断是因为当n为奇数时 n/2得到的结果会自动将多的那个1排除 所以要加上一
//通过逻辑思维也可以想到 如果是偶数 通过二分法结果剩最后两个 只要加一只鼠就可以解决问题 但如果是奇数 通过整除后剩最后两个时加一只就会有问题 因为最后剩的不是二 而是三
//例如 5 如果正常的话 是 5--2 两只鼠就可以了 但事实是 5分开 2 和 3 如果毒药落到3的这一面就需要3只鼠 所以奇数加一
if(n%2 != 0){
number++;
}
while(n > 2){
number++;
n = n/2;
System.out.println(n);
}
//这一步是以为当数量为3时 n/2直接就为1 了 但正解是还需要两只 所以当n最后不管是n为1 还是为2 只要加一就好了
number++;
System.out.println(number);
}
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
给定一个arr[]数组,其中有N个整数和另一个整数K。任务是找到的最大金额的子序列,不同指标的连续子序列中的元素原始数组就是k .例如,如果arr[我]子序列然后下一个元素的第一个元素必须arr (i + k)然后arr (i + 2 k)等等。
Input: arr[] = {2, -3, -1, -1, 2}, K = 2
Output: 3
Input: arr[] = {2, 3, -1, -1, 2}, K = 3
Output: 5
这个是本人写的答案 , 没有去网上找标准答案 如果有错误,请您帮忙指正!
/*
给定一个arr[]数组,其中有N个整数和另一个整数K。任务是找到的最大金额的子序列,不同指标的连续子序列中的元素原始数组就是k .例如,如果arr[我]子序列然后下一个元素的第一个元素必须arr (i + k)然后arr (i + 2 k)等等。
Input: arr[] = {2, -3, -1, -1, 2}, K = 2
Output: 3
Input: arr[] = {2, 3, -1, -1, 2}, K = 3
Output: 5
*/
public class Maximum {
public static void main(String[] args) {
int arr[] ={2,3,-1,-1,2};
int k = 3;
int number = 0;//定义一个每次计算结束后的值
int max = 0;//最大值
for (int i = 0; i < arr.length-k; i++) {
int flag = 0;
do{
number+=arr[i+flag*k];
flag++;
}while((i+flag*k) < arr.length);
//判断计算结束的值与最大值谁大
if(max<number){
max=number;
}
number = 0;
}
System.out.println(max);
}
}
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<