1.统计一篇英文文章单词个数。
public class WordCounting {
public static void main(String[] args) {
try(FileReader fr = new FileReader("a.txt")) {
int counter = 0;
boolean state = false;
int currentChar;
while((currentChar= fr.read()) != -1) {
if(currentChar== ' ' || currentChar == '\n'
|| currentChar == '\t' || currentChar == '\r') {
state = false;
}
else if(!state) {
state = true;
counter++;
}
}
System.out.println(counter);
}
catch(Exception e) {
e.printStackTrace();
}
}
}
补充:这个程序可能有很多种写法,这里选择的是Dennis M. Ritchie和Brian W. Kernighan老师在他们不朽的著作《The C Programming Language》中给出的代码,向两位老师致敬。下面的代码也是如此。

2.输入年月日,计算该日期是这一年的第几天。
public class DayCounting {
public static void main(String[] args) {
int[][] data = {
{31,28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
{31,29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};
Scanner sc = new Scanner(System.in);
System.out.print("请输入年月日(1980 11 28): ");
int year = sc.nextInt();
int month = sc.nextInt();
int date = sc.nextInt();
int[] daysOfMonth = data[(year % 4 == 0 && year % 100 != 0 || year % 400 == 0)?1 : 0];
int sum = 0;
for(int i = 0; i < month -1; i++) {
sum += daysOfMonth[i];
}
sum += date;
System.out.println(sum);
sc.close();
}
}

3.回文素数:所谓回文数就是顺着读和倒着读一样的数(例如:11,121,1991…),回文素数就是既是回文数又是素数(只能被1和自身整除的数)的数。编程找出11~9999之间的回文素数。
public class PalindromicPrimeNumber {
public static void main(String[] args) {
for(int i = 11; i <= 9999; i++) {
if(isPrime(i) && isPalindromic(i)) {
System.out.println(i);
}
}
}
public static boolean isPrime(int n) {
for(int i = 2; i <= Math.sqrt(n); i++) {
if(n % i == 0) {
return false;
}
}
return true;
}
public static boolean isPalindromic(int n) {
int temp = n;
int sum = 0;
while(temp > 0) {
sum= sum * 10 + temp % 10;
temp/= 10;
}
return sum == n;
}
}

4.全排列:给出五个数字12345的所有排列。
public class FullPermutation {
public static void perm(int[] list) {
perm(list,0);
}
private static void perm(int[] list, int k) {
if (k == list.length) {
for (int i = 0; i < list.length; i++) {
System.out.print(list[i]);
}
System.out.println();
}else{
for (int i = k; i < list.length; i++) {
swap(list, k, i);
perm(list, k + 1);
swap(list, k, i);
}
}
}
private static void swap(int[] list, int pos1, int pos2) {
int temp = list[pos1];
list[pos1] = list[pos2];
list[pos2] = temp;
}

public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5};
perm(x);
}
}

5.对于一个有N个整数元素的一维数组,找出它的子数组(数组中下标连续的元素组成的数组)之和的最大值。
下面给出几个例子(最大子数组用粗体表示):
数组:{ 1, -2, 3,5, -3, 2 },结果是:8
2) 数组:{ 0, -2, 3, 5, -1, 2 },结果是:9
3) 数组:{ -9, -2,-3, -5, -3 },结果是:-2
可以使用动态规划的思想求解:
public class MaxSum {
private static int max(int x, int y) {
return x > y? x: y;
}
public static int maxSum(int[] array) {
int n = array.length;
int[] start = new int[n];
int[] all = new int[n];
all[n - 1] = start[n - 1] = array[n - 1];
for(int i = n - 2; i >= 0;i--) {
start[i] = max(array[i], array[i] + start[i + 1]);
all[i] = max(start[i], all[i + 1]);
}
return all[0];
}
public static void main(String[] args) {
int[] x1 = { 1, -2, 3, 5,-3, 2 };
int[] x2 = { 0, -2, 3, 5,-1, 2 };
int[] x3 = { -9, -2, -3,-5, -3 };
System.out.println(maxSum(x1)); // 8
System.out.println(maxSum(x2)); // 9
System.out.println(maxSum(x3)); //-2
}
}

6.用递归实现字符串倒转
public class StringReverse {
public static String reverse(String originStr) {
if(originStr == null || originStr.length()== 1) {
return originStr;
}
return reverse(originStr.substring(1))+ originStr.charAt(0);
}
public static void main(String[] args) {
System.out.println(reverse("hello"));
}
}

7.输入一个正整数,将其分解为素数的乘积。
public class DecomposeInteger {
private static List<Integer> list = new ArrayList<Integer>();
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 <temp.length(); 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> 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;
}

java笔试手写算法面试题大全含答案的更多相关文章

  1. java中String类的面试题大全含答案

    1.下面程序的运行结果是()(选择一项)String str1="hello";String str2=new String("hello");System.o ...

  2. Redis面试题大全含答案

    Redis面试题大全含答案 Redis面试题大全含答案 1.什么是Redis?答:Remote Dictionary Server(Redis)是一个开源的使用ANSI C语言编写.支持网络.可基于内 ...

  3. Java算法面试题(史上最强、持续更新、吐血推荐)

    文章很长,建议收藏起来,慢慢读! 疯狂创客圈为小伙伴奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : <Netty Zookeeper Redis 高并发实战> 面试必备 + 大厂必备 ...

  4. Twitter算法面试题详解(Java实现)

    最近在网上看到一道Twitter的算法面试题,网上已经有人给出了答案,不过可能有些人没太看明白(我也未验证是否正确),现在给出一个比较好理解的答案.先看一下题目. 图1 先看看图图1.可以将方块看做砖 ...

  5. 【转】Twitter算法面试题详解(Java实现)

    原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://androidguy.blog.51cto.com/974126/1319659 ...

  6. 算法实践——Twitter算法面试题(积水问题)的线性时间解法

    问题描述:在下图里我们有不同高度的挡板.这个图片由一个整数数组所代表,数组中每个数是墙的高度.下图可以表示为数组(2.5.1.2.3.4.7.2).假如开始下雨了,那么挡板之间的水坑能够装多少水(水足 ...

  7. 【BAT经典算法面试题系列】求和为n的连续正整数

    马上就要到9月份了,意味着一年一度的秋招就要开始了,相信不论是正在实习的童鞋还是马上就要找工作的童鞋,BAT无疑是国内的"明星企业",是每个学计算机的小伙伴们心之向往的企业,但是呢 ...

  8. 华为Python 算法面试题

    华为算法面试题 """ 算法题: 提供一个序列,完成对这个序列的分割.要求分割后的两个序列彼此差值最小 实现函数,返回两个序列 """ de ...

  9. 常见的js算法面试题收集,es6实现

    1.js 统计一个字符串出现频率最高的字母/数字 let str = 'asdfghjklaqwertyuiopiaia'; const strChar = str => { let strin ...

随机推荐

  1. Linux 定时任务 Crontab 命令详解

    linux 系统则是由 cron (crond) 这个系统服务来控制的.Linux 系统上面原本就有非常多的计划性工作,因此这个系统服务是默认启动的.另 外, 由于使用者自己也可以设置计划任务,所以, ...

  2. jQuery的ajaxFileUpload上传文件插件刷新一次才能再次调用触发change

    jQuery的ajaxFileUpload插件 关于用ajaxfileupload时,遇到一个要刷新一次页面才能再次上传,用live()方法来绑定 file表单 的change事件就能够解决,直接$( ...

  3. RabbitMQ探索之路(一):RabbitMQ简介

    一:何为RabbitMQ? 作为Windows忠实用户,不得不提微软自带的MSMQ,Windows自带的一个服务,message是存放在文件系统的,这是最原始的消息队列了. 然而如今的分布式以及消息处 ...

  4. npm启动报错

    npm 启动报错  event.js 160 报错原因: 端口号被占用 解决方法: 1.重新定义一个端口号. 2.任务管理器关掉node进程,重新运行npm.

  5. 2PC和3PC

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11484077.html 2PC 是计算机网络尤其是在数据库领域内,为了使基于分布式系统架构下的所有节点 ...

  6. 每天一个linux命令:file(11)

    file file命令用来探测给定文件的类型.file命令对文件的检查分为文件系统.魔法幻数检查和语言检查3个过程. 格式 file [选项] [参数] 参数选项 参数 备注 -b 列出辨识结果时,不 ...

  7. qt学习(三):鼠标图标改变

    qt学习 (三):鼠标图标改变 当你进入一个美好的qt软件场景,比如游戏,电脑的黑白图标会让程序逊色不少, 1改图标要加光标的头文件, 2 载入光标图, 3 再设置改光标就可以了 1在头文件中加 #i ...

  8. SPI驱动程序设计

    一.SPI驱动子系统架构 m25p80.c: static int __devinit m25p_probe(struct spi_device *spi) { struct flash_platfo ...

  9. 对象关系型数据库管理系统(PostgresQL )

    PostgresQL是   对象关系型数据库管理系统(ORDBMS).PostgreSQL支持大部分SQL标准并且提供了许多其他现代特性:复杂查询.外键.触发器.视图.事务完整性.MVCC.同样,Po ...

  10. 最短路 Dijkstra模板

    普通dijkstra,复杂度O(n*n) #include<bits/stdc++.h> using namespace std; int n,m,f[105][105],dis[105] ...