java题编写一个方法,java笔试手写算法面试题大全含答案(2)

7.输入一个正整数,将其分解为素数的乘积。

public class DecomposeInteger {

private static List list = new ArrayList();

public static void main(String[] args) {

System.out.print("请输入一个数: ");

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

decomposeNumber(n);

System.out.print(n + " = ");

for(int i = 0; i < list.size() - 1; i++) {

System.out.print(list.get(i) + " * ");

}

System.out.println(list.get(list.size() - 1));

}

public static void decomposeNumber(int n) {

if(isPrime(n)) {

list.add(n);

list.add(1);

}

else {

doIt(n, (int)Math.sqrt(n));

}

}

public static void doIt(int n, int div) {

if(isPrime(div) && n % div == 0) {

list.add(div);

decomposeNumber(n / div);

}

else {

doIt(n, div - 1);

}

}

public static boolean isPrime(int n) {

for(int i = 2; i <= Math.sqrt(n);i++) {

if(n % i == 0) {

return false;

}

}

return true;

}

}

8、一个有n级的台阶,一次可以走1级、2级或3级,问走完n级台阶有多少种走法。

public class GoSteps {

public static int countWays(int n) {

if(n < 0) {

return 0;

}

else if(n == 0) {

return 1;

}

else {

return countWays(n - 1) + countWays(n - 2) + countWays(n -3);

}

}

public static void main(String[] args) {

System.out.println(countWays(5)); // 13

}

}

9.写一个算法判断一个英文单词的所有字母是否全都不同(不区分大小写)

public class AllNotTheSame {

public static boolean judge(String str) {

String temp = str.toLowerCase();

int[] letterCounter = new int[26];

for(int i = 0; i

int index = temp.charAt(i)- 'a';

letterCounter[index]++;

if(letterCounter[index] > 1) {

return false;

}

}

return true;

}

public static void main(String[] args) {

System.out.println(judge("hello"));

System.out.print(judge("smile"));

}

}

10.有一个已经排好序的整数数组,其中存在重复元素,请将重复元素删除掉,例如,A= [1, 1, 2, 2, 3],处理之后的数组应当为A= [1, 2, 3]。

public class RemoveDuplication {

public static int[] removeDuplicates(int a[]) {

if(a.length <= 1) {

return a;

}

int index = 0;

for(int i = 1; i < a.length; i++) {

if(a[index] != a[i]) {

a[++index] = a[i];

}

}

int[] b = new int[index + 1];

System.arraycopy(a, 0, b, 0, b.length);

return b;

}

public static void main(String[] args) {

int[] a = {1, 1, 2, 2, 3};

a = removeDuplicates(a);

System.out.println(Arrays.toString(a));

}

}

11.给一个数组,其中有一个重复元素占半数以上,找出这个元素。

public class FindMost {

public static T find(T[] x){

T temp = null;

for(int i = 0, nTimes = 0; i< x.length;i++) {

if(nTimes == 0) {

temp= x[i];

nTimes= 1;

}

else {

if(x[i].equals(temp)) {

nTimes++;

}

else {

nTimes--;

}

}

}

return temp;

}

public static void main(String[] args) {

String[]strs = {"hello","kiss","hello","hello","maybe"};

System.out.println(find(strs));

}

}

12.编写一个方法求一个字符串的字节长度?

public int getWordCount(String s){

int length = 0;

for(int i = 0; i < s.length(); i++)

{

int ascii = Character.codePointAt(s, i);

if(ascii >= 0 && ascii <=255)

length++;

else

length += 2;

}

return length;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值