- 二分搜索
public void binarySearch(int[] a,int target){
int left=0;int right=a.length-1;
while(left<=right&&left<a.length&&right<a.length)
{
int mid=left+(right-left)/2;
if(a[mid]==target)
return mid;
else if(a[mid]<target)
left=mid+1;
else
right=mid-1;
}
}
- 从100W个数中,找出100个最大数(堆)
分析:
最大堆,快排, - 链表模拟加法,比如 1->2->3 , 4->-5>6->7 输出结果:4->6->9->0 (主要考察链表逆置)
public ListNode addTwoNumbers(ListNode l1, ListNode l2)
{
ListNode root=new ListNode(0);
ListNode node=root;
int carry=0;
while(l1!=null||l2!=null||carry!=0){
int sum=(l1!=null?l1.val:0)+(l2!=null?l2.val:0)+carry;
node.next=sum%10;
int carry=sum/10;
node=node.next;
l1=(l1!=null?l1.next:null);
l2=(l2!=null?l2.next:null);
}
return root.next;
}
- 用两个栈实现一个队列。队列的生命如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。
public void stackimplementQueue(){
LinkedList<Integer> stack1=new LinkedList<Integer> ();
LinkedList<Integer> stack2=new LinkedList<Integer>();
public Integer offer(Integer i){
stack1.push(i);
return i;
}
public Integer remove(){
Integer re=null;
if(!stack2.isEmpty()){
re=stack2.pop();
}else{
while(!stack1.isEmpty()){
stack2.push(stack1.pop());
}
if(!stack2.isEmpty())
re=stack2.pop();
}
return re;
}
}
- 写一个将字符串转换为数字的方法,不能使用Java现有的方法(如:Integer.parseInt)
public class Solution {
public int StrToInt(String str) {
if(str==null||str.length==0)
return 0;
char[] a=str.toCharArray();
int sum=0;
int fuhao=0;
if(a[0]=='-')
fuhao=1;
for(int i=fuhao;i<a.length;i++){
if(a[i]=='+')
continue;
if(a[i]<48&&a[i]>57){
//48为0的ASIIC码值
return 0;
}
sum=sum*10+a[i];
}
return fuhao==0?sum:sum*(-1);
}
}
- 一百个台阶,一步一步走,也可以两步两步走,一共有多少种走法?
递归
return f(n-1)+f(n-2); - 递归实现下 public String replace(String ss, char ch);
- 快排
public void quickSort(int[] a, int left,int right){
int index=patition(a,left,right);
if(left<index-1)
quickSort(a,left,index-1);
if(index<right)
quickSort(a,index,right);
}
public int patition(int[] a,int left,int right){
int mid=a[(left+right)/2];
while(left<=right){
while(a[left]<mid)
left++;
while(mid<a[right])
right--;
if(left<=right){
int tmp=a[left] ;
a[left]=a[right];
a[right]=tmp;
left++;
right--;
}
}
return left;
}
9,归并排序
public void mergeSort(int [] A,int left,int right){
if(left<=right){
int mid=left+(right-left)/2;
mergeSort(A,left,mid);
mergeSort(A,mid+1,right);
merge(A,left,mid,right);
}
}
public void merge(int[] A,int left,int mid,int right){
int l=left,r=right;
int m=mid+1;
int index=0;
int[] helper=new int[right-left+1];
while(l<=mid&&m<=r){
if(A[l]<A[m])
helper[index++]=A[l++];
else
helper[index++]=A[m++];
}
while(l<=mid)
helper[index++}=A[l++];
while(m<=r)
helper[index++]=A[m++];
for(int i=0;i<helper.length;i++){
A[left+i]=helper[i];
}
}
10,OOM是怎么出现的,有哪几块JVM区域会产生OOM,如何解决
分析:
OOM gc与非gc时间超过了GCTimeRatio的限制
堆(不断地new新的对象),本地方法栈(不断提出生成线程请求),Java虚拟机栈,方法区(String.intern()),常量池。
11,Java里面的观察者模式实现
public interface watcher{
public void update(String str);
}
public interface watched{
public void addWatcher(Watcher watcher);
public void removeWatcher(Watcher watcher);
public void notifyWatcher(String str);
}
public class ConcreteWatcher implements Watcher{
public void update(String str){
System.out.println(str) ;
}
}
public class ConcreteWatched implements watched{
private List<Watcher> list=new ArrayList<Watcher>();
@Override
public void addWatcher(Watcher watcher) {
// TODO Auto-generated method stub
list.add(watcher);
}
@Override
public void removeWatcher(Watcher watcher) {
// TODO Auto-generated method stub
list.remove(watcher);
}
@Override
public void notifyWatcher(String str) {
// TODO Auto-generated method stub
for(Watcher watcher: list){
watcher.update(str);
}
}
}
public class testWatcher {
public static void main(String[] args) {
// TODO Auto-generated method stub
Watched girl=new ConcreteWatched();
Watcher watcher1=new ConcreteWatcher();
Watcher watcher2=new ConcreteWatcher();
Watcher watcher3=new ConcreteWatcher();
girl.addWatcher(watcher1);
girl.addWatcher(watcher2);
girl.addWatcher(watcher3);
girl.notifyWatcher("Happy");
}
}
12,Rotate List
1-2-3-4-5-null, k=2,
返回 4-5-1-2-3-null
分析:
首先求出list的长度len ,然后求出 k=len-k%len;
然后形成一个环,再for循环k,
接着head=p.next;生成新的头结点p.next=null;断开环
public ListNode rotateRight(ListNode head,int k){
if(head==null||k==0)
return head;
int len=1;
while(p.next!=null){
len++;
p=p.next;
}
k=len-k%len;
p.next=head;//首尾相连,形成环
for(int i=0;i<k;i++){
p=p.next;
}
head=p.next;//生成新的头结点
p.next=null;//断开环
return head;
}