2016广联达笔试题
一、解释操作系统中heap和stack的区别
1.heap是堆,stack是栈。
2.stack的空间由操作系统自动分配和释放,heap的空间是手动申请和释放的(Java中是由垃圾回收机制负责),heap常用new关键字来分配。
3.stack空间有限,heap的空间是很大的自由区。在Java中,若只是声明一个对象,则先在栈内存中为其分配地址空间,若再new一下,实例化它,则在堆内存中为其分配地址。
4.举例:数据类型 变量名;这样定义的东西在栈区。如:Object a =null; 只在栈内存中分配空间new 数据类型();或者malloc(长度); 这样定义的东西就在堆区如:Object b =new Object(); 则在堆内存中分配空间
http://www.cnblogs.com/perfy/archive/2012/09/06/2672946.html
二、判断一个整数数组,判断其中是否有三个数和为0
三层FoR循环
public class sanshuhe0 {
public static void main(String[] args) {
int[] a = { 5, 2, 1, 14, 6, -3 ,-3};
int i,j,k;boolean isTrue=false;
int sum;
for (i = 0; i < a.length; i++) {
for (j = i+1; j < a.length; j++) {
for (k = j+1; k < a.length; k++) {
sum= a[i]+a[j]+a[k];
if (sum == 0) {
System.out.println("数组中存在这样三个数!");
System.out.println(a[i]+"+"+a[j]+"+"+a[k]+"=0");
}
}
}
}
}
}
三、数组中只有一个元素出现一次,其余元素都出现两次,在线性时间类,不使用额外的空间找出只出现一次的那个元素。
这个题的标准做法是利用异或运算的这两个法则
1. a ^ b = b ^ a
2. a ^ b ^ c = a ^ (b ^ c) = (a ^ b) ^ c;
还有就是0^N=N。
package bishi.one;
public class OnlyOne {
public static void main(String[] args) {
int arr[]={9,1,5,2,2,5,1,8,7,9,8};
System.out.println(getOne(arr));
}
public static int getOne(int[] array) {
int n = array.length;
if(n == 1)
return array[0];
int result = array[0];
for(int i = 1 ; i < n ; i++)
{
result ^= array[i];//异或运算的可交换性
}
return result;
}
}
四、一个数组中所有的元素都出现了三次,只有一个元素出现了一次找到这个元素
(一直没有完全理解,,,)
package bishi.one;
public class OnlyOne2 {
public static void main(String[] args) {
int arr[]={1,2,3,3,2,1,5,2,3,1};
System.out.println(getOne(arr));
}
public static int getOne(int[] arr) {
int ones=0;
int twos=0;
int xthrees=0;
for(int i = 0;i <arr.length;i++){
twos ^= (ones&arr[i]);
ones ^= arr[i];
xthrees = ~(ones&twos);
twos &= xthrees;
ones &=xthrees;
}
return ones;
}
}
五、问题描述:
* 已知某次聚会共有N个人参加,这N个人来自26个不同的地区,先将26个地区用a-z进行表示,
* 使用整数数组Location存储这N个人的地区,请返回一个boolean类型的变量
* true代表所有人的地区都不一样,false代表存在一样地区的人。(这里将题目要求改为输出该地区)
package bishi.one;
import java.util.Scanner;
public class AtoZ {
public static void main(String[] args) {
System.out.println("请输入");
Scanner input = new Scanner(System.in);
String str = input.next();
char[] strChar = str.toCharArray();
judge(strChar);
}
public static void judge(char[] strChar) {
int len = strChar.length;
int[] count = new int[26];
int index = 0;
// 遍历整个strChar数组,统计每一个字母出现的次数
for (int i = 0; i < len; i++) {
index = strChar[i] - 'a';//相减则count[0]存储出现a的次数,以此类推
count[index]++;
}
// 如果出现的次数大于1,即存在相同名族
for (int i = 0; i < 26; i++) {
if (count[i] > 1) {
System.out.println((char)(i+97));
}
}
}
}
六、问题描述: 一个n*m的阵列,每一个位置上都有一个值,我们从左上角开始出发,向右下角方向步进。最终到达右下角,找到我们经过路径上的值的和的最大值,例如图中最大值为53。
矩阵为:
1 10 3 8
12 2 9 6
5 7 4 11
3 7 16 5
动态规划问题(还没有了解动态规划先留着)
http://blog.csdn.net/genius_zz/article/details/52441484
package bishi.one;
public class DongTai {
private static int[][] matrix = {
{1,10,3,8},
{12,2,9,6},
{5,7,4,11},
{3,7,16,5}
};
public static void main(String[] args){
System.out.println("the max value from lefttop to the rightbottom is:"+getMax(matrix));
}
public static int getMax(int[][] grid){
//如果传过来的是一个空的或长度为零的矩阵,特殊情况
if(grid == null || grid.length == 0){
return 0;
}
//分别为矩阵的行,列数。
int row = grid.length;
int colunm = grid[0].length;
int[][] val = new int[row][colunm];
for(int i = 0; i < row; i++){
for(int j = 0;j < row; j++){
if(i == 0 && j == 0){
val[i][j] = grid[i][j];
}else if(i == 0 && j > 0){
val[i][j] = val[i][j-1]+grid[i][j];
}else if(i > 0 && j == 0){
val[i][j] = val[i-1][j] + grid[i][j];
}else{
val[i][j] = Math.max(val[i-1][j], val[i][j-1]) + grid[i][j];
}
}
}
return val[row-1][colunm-1];
}
}