欢迎关注“踏进职场” 公众号,分享职场故事,以及定时更新面试题目
测试面试题目
链表:
public class 合并两个有序的链表 {
public static void main(String[] args) {
}
public static ListNode mergeSort(ListNode list1,ListNode list2) {
ListNode head=new ListNode(-1);
head.next=null;
ListNode root=head;
while(list1!=null && list2!=null) {
if(list1.data<list2.data) {
head.next=list1;
head=list1;
list1=list1.next;
}else {
head.next=list2;
head=list2;
list2=list2.next;
}
}
if(list1!=null) {
head.next=list1;
}
if(list2!=null) {
head.next=list2;
}
return root.next;
}
}
public class 环形链表 {
public static void main(String[] args) {
}
public static boolean hasCycle(ListNode head) {
if(head==null || head.next==null) {
return false;
}
ListNode fast=head.next.next;
ListNode slow=head;
while(slow!=fast) {
if(fast==null ||fast.next==null) {
return false;
}
fast=fast.next.next;
slow=slow.next;
}
return true;
}
}
public static void main(String[] args) {
ListNode list1=new ListNode(1);
ListNode list2=new ListNode(2);
ListNode list3=new ListNode(3);
ListNode list4=new ListNode(4);
ListNode list5=new ListNode(5);
list1.next=list2;
list2.next=list3;
list3.next=list4;
list4.next=list5;
//System.out.println(ReverseList(list1).data);
ReverseList(list1);
}
public static void ReverseList(ListNode head) {
ListNode pre=null;
ListNode tmp=null;
while(head!=null) {
tmp=head.next;
head.next=pre;
pre=head;
head=tmp;
}
while(pre!=null) {
System.out.print(pre.data+">");
pre=pre.next;
}
//return pre.next;
}
public ListNode deleteDuplicates(ListNode head) {
ListNode current=head;
while(current!=null && current.next!=null) {
if(current.data==current.next.data) {
current.next=current.next.next;
}else {
current=current.next;
}
}
return head;
}
public static void bubblesort(int []nums) {
int len=nums.length-1;
//执行的次数
for(int i=0;i<nums.length;i++) {
//进行数字交换
for(int j=len;j>i;j--) {
if(nums[j-1]>nums[j]) {
int temp=nums[j-1];
nums[j-1]=nums[j];
nums[j]=temp;
}
}
}
}
public static void quicksort(int []nums,int i,int j) {
int middle=partition(nums,i,j);
quicksort(nums,i,middle-1);
quicksort(nums,middle+1,j);
}
//排序是从小到大进行排序
public static int partition(int[] nums, int i, int j) {
//先定一个基准元素
int temp=nums[i];
while(i<j) {
//从后往前进行查找数据,当后面的数据比前面是数据小的时候,退出循环
while(i<j && nums[j]>=temp) {
j--;
}
//把找到更小的放到右边
nums[i]=nums[j];
//从前往后进行查找数据,当前面的数比后面的数大的时候,退出循环
while(i<j && nums[i]<temp) {
i++;
}
//把找到更大的数放到了右边
nums[j]=nums[i];
}
nums[i]=temp;
return i;
}
7.简单选择排序,将数组分为有序区和无序区,每次从无序区中取出最小的放到有序区中(o(n*n)
public static void selectsort(int []nums) {
int i,j,k;
for(i=0;i<nums.length;i++) {
k=i;
for(j=i+1;j<nums.length;j++) {
if(nums[j-1]>nums[j]) {
k=j;
}
}
if(k!=i) {
int temp=nums[k];
nums[k]=nums[i];
nums[i]=temp;
}
}
}
public static int longestRoad1(TreeNode root) {
if(root==null) {
return 0;
}
int left=longestRoad1(root.left);
int right=longestRoad1(root.right);
return left>right?left+1:right+1;
}
public TreeNode sortedArrayToBST(int []nums) {
return sortedArrayToBST(nums,0,nums.length-1);
}
public TreeNode sortedArrayToBST(int[] nums, int left, int right) {
if(left>right) return null;
int mid=(left+right)/2;
TreeNode root=new TreeNode(nums[mid]);
root.left=sortedArrayToBST(nums,left,mid-1);
root.right=sortedArrayToBST(nums,mid+1,right);
return root;
}
public static boolean isBalanced=true;
public boolean IsBalanced_Solution(TreeNode root) {
getDepth(root);
return isBalanced;
}
public static int getDepth(TreeNode root) {
if(root==null) {
return 0;
}
int left=getDepth(root.left);
int right=getDepth(root.right);
if(Math.abs(left-right)>1) {
isBalanced=false;
}
return left>right?left+1:right+1;
public static void InOrder(TreeNode root) {
if(root==null) return;
Stack
TreeNode p=root;
stack.add(p);
System.out.println("中序遍历");
while(!stack.isEmpty()) {
//只有你有左孩子,就把它压入栈
if(p!=null && p.left!=null) {
stack.add(p.left);
p=p.left;
}else {
p=stack.pop();
System.out.print(p.val+" ");
if(p!=null &&p.right!=null) {
stack.add(p.right);
p=p.right;
}else {
p=null;//已经访问过p,就设置为null
}
}
}
}
public static void cengxu(TreeNode root) {
Queue
if(root==null) return;
queue.add(root);
System.out.println();
System.out.println("层序遍历");
while(!queue.isEmpty()) {
TreeNode p=queue.poll();
if(p.left!=null) {
queue.add(p.left);
}
if(p.right!=null) {
queue.add(p.right);
}
System.out.print(p.val+" ");
}
}
13 前序遍历
public static void preOrder(TreeNode root) {
if(root==null) return;
Stack
if(root!=null) {
stack.add(root);
}
System.out.println();
System.out.println("前序遍历");
while(!stack.isEmpty()) {
TreeNode p=stack.pop();
if(p.right!=null) {
stack.add(p.right);
}
if(p.left!=null) {
stack.add(p.left);
}
System.out.print(p.val+" ");
}
}
public static void afterOrder(TreeNode root) {
if(root==null) return;
Stack
Map
TreeNode p=root;
System.out.println();
System.out.println("后序遍历");
while(!stack.isEmpty()||p!=null) {
if(p!=null ) {
stack.add(p);
map.put(p.val, 1);
p=p.left;
}else {
p=stack.peek();
if(map.get(p.val)==2) {
stack.pop();
System.out.print(p.val+" ");
p=null;
}else {
map.put(p.val, 2);
p=p.right;
}
}
}
//递归
public static int func(int n) {
if(n==1 || n==2) {
return 1;
}
return func(n-1)+func(n-2);
}
public static void main(String[] args) {
int []num1= {1,2,4,6};
int []num2= {3,5,8};
int []result=merge(num1,num2);
for(int i=0;i<result.length;i++) {
System.out.print(result[i]+" ");
}
}
public static int[] merge(int []num1,int []num2) {
int []result=new int[num1.length+ num2.length];
int i=0,j=0,k=0;
while(i<num1.length&&j<num2.length) {
if(num1[i]<num2[j]) {
result[k]=num1[i];
i++;
}else {
result[k]=num2[j];
j++;
}
k++;
}
while(i<num1.length) {
result[k++]=num1[i++];
}
while(j<num2.length) {
result[k++]=num2[j++];
}
return result;
}
}
public static void main(String[] args) {
int[] nums= {8,9,9};
int []array=plusOne(nums);
for(int i=0;i<array.length;i++) {
System.out.print(array[i]+"");
}
}
public static int[]plusOne(int []digits){
int n=digits.length;
for(int i=n-1;i>=0;i--) {
if(digits[i]<9) {
digits[i]++;
return digits;
}
digits[i]=0;
}
//如果所有的数都等于9
int[]result=new int[n+1];
result[0]=1;
return result;
}
}
public static void main(String[] args) {
int []nums= {1,-1,2,-2,3,4,4,1,1};
List>
list=threenum(nums);
System.out.println(list);
}
public static List> threenum(
int []nums){
List>
res=new ArrayList<>();
int len=nums.length;
Arrays.sort(nums);
for(int i=0;i<len-2;i++) {
if(i>0&&nums[i]==nums[i-1]) {
continue;
}
int low=i+1;
int high=len-1;
while(low<high) {
if(nums[low]+nums[high]==-nums[i]) {
res.add(Arrays.asList(nums[i],nums[low],nums[high]));
//已经找到,将重复的去掉
while(low<high &&nums[low+1]==nums[low]) {
low++;
}
while(low<high &&nums[high-1]==nums[high]) {
high--;
}
low++;
high++;
}else if(nums[low]+nums[high]<-nums[i]) {
low--;
}else {
high--;
}
}
}
return res;
}
}
public static void main(String[] args) {
String str="hello world";
char[]s=str.toCharArray();
reverseString(s);
}
public static void reverseString(char[]s) {
int len=s.length;
int left=0,right=len-1;
char tem;
while(left<right) {
tem=s[left];
s[left]=s[right];
s[right]=tem;
left++;
right--;
}
System.out.println(s);
}
public static void main(String[] args) {
//对于一个数组,分别first、last指向数组的头结点和尾结点
int []array= {1,1,2,2,8,8,8,9};
int target=10;
printTarget(array,target);
}
public static void printTarget(int []array,int target) {
int first=0;
int last=array.length-1;
int sum=0;
while(first<last) {
sum=array[first]+array[last];
if(sum<target) {
first++;
}else if(sum>target) {
last--;
}else {
System.out.println(array[first]+" "+array[last]);
first++;
last--;
}
}
}
}
import java.util.ArrayList;
import java.util.List;
//回文数
public class 算法1 {
public static ArrayList
ArrayList
ArrayList
partitionHepler(lists, list, s);
return lists;
}
public static void partitionHepler(ArrayList
if (null == s || s.length() == 0) {
lists.add(new ArrayList<>(list));
return;
}
int len = s.length();
for (int i = 0; i <= len;i++) {
String subStr = s.substring(0, i);
if (isPalindrome(subStr)) {
list.add(subStr);
partitionHepler(lists, list, s.substring(i, len));
list.remove(list.size() - 1);
}
}
}
public static boolean isPalindrome(String s) {
if (null == s || s.length() == 0) return false;
int length = s.length();
int middle = length / 2;
for (int i = 0; i < middle;i++) {
if (s.charAt(i) != s.charAt(length - 1 - i)) {
return false;
}
}
return true;
}
}
21.public class 找出不含有重复字符串的最长子串长度 {
public static void main(String[] args) {
String s="afdf";
int len=lengthofLongest(s);
System.out.println(len);
}
public static int lengthofLongest(String s) {
List
if(s==null ||s.length()==0) {
return 0;
}
int from=0,to=1,le=1,maxlength=1;
while(to<s.length()) {
int site=s.substring(from, to).indexOf(s.charAt(to));
if(site!=-1) {
le=to-from;
if(le>maxlength) {
maxlength=le;
list.add(s.substring(from,to));
}
from=from+site+1;
}
to++;
}
if(to-from>maxlength) {
maxlength=to-from;
list.add(s.substring(from,to));
}
System.out.println(list.get(list.size()-1));
return maxlength;
}
}
22.public static int reverse(int x) {
int result=0;
while(x!=0) {
result=result*10+x%10;
x=x/10;
}
if(result>Integer.MAX_VALUE || result
return 0;
}
return result;
}
23.public class 只出现一次的元素 {
public static void main(String[] args) {
String []array= {"a","e","w","a","d","e"};
printOne(array);
}
public static void printOne(String []array) {
HashMap
for(int i=0;i<array.length;i++) {
if(map.containsKey(array[i])) {
int count=map.get(array[i]);
map.put(array[i], count+1);
}else {
map.put(array[i], 1);
}
}
for(int i=0;i<array.length;i++) {
if(map.get(array[i])==1) {
System.out.print(array[i]+" ");
}
}
}
public class 字符串排列 {
public static void main(String[] args) {
String str="aac";
ArrayList
}
public static ArrayList
ArrayList
if(str==null ||str.length()==0) {
return list;
}
char[]strArr=str.toCharArray();
permu(list, strArr, 0);
Collections.sort(list);
return list;
}
public static void permu(ArrayList
//递归终止条件
if(start==strArr.length-1) {
String result=String.valueOf(strArr);
if(list.indexOf(result)<0) {
list.add(result);
System.out.println(result);
}
}
//进行交换
for(int i=start;i<strArr.length;i++) {
char temp=strArr[start];
strArr[start]=strArr[i];
strArr[i] = temp;
permu(list,strArr,start+1);
//恢复数组顺序
temp=strArr[start];
strArr[start]=strArr[i];
strArr[i] = temp;
}
}
}
public class 字符串转整数 {
public static void main(String[] args) {
String str="12qqq-";
System.out.println(myAtoi(str));
}
public static int myAtoi(String str) {
//合法性判断
if(str.isEmpty()) return 0;
//正负号标记
int sign=-1;
//转换值
int base=0;
//索引位
int i=0;
//剔除开始空白字符
while(str.charAt(i)==' ') {
i++;
}
//判断正负号
if(str.charAt(i)=='-'||str.charAt(i)=='+') {
sign=str.charAt(i)=='-'?-1:1;
}
//索引有效数字字符
while(i<str.length() && str.charAt(i)>='0' && str.charAt(i)<='9') {
if(base>Integer.MAX_VALUE/10 || (base==Integer.MAX_VALUE/10 &&str.charAt(i)-'0'>7)) {
return (sign==1)?Integer.MAX_VALUE:Integer.MIN_VALUE;
}
//计算转换值
base =base*10 +(str.charAt(i++)-'0');
}
return base * sign;
}
}
public String getLongest(String s1,String s2) {
if(s1==null || s2==null) {
return null;
}
if(s1.equals(s2)) {
return s2;
}
char[]arr1=s1.toCharArray();
char[]arr2=s2.toCharArray();
int[][]temp=new int[arr1.length][arr2.length];
//存储最长公共长度
int length=0;
//start表示公共长度的起点,end表示公共长度的终点
int start=0;
int end=0;
//初始化二维矩阵第一行
for(int i=0;i<arr2.length;i++) {
temp[0][i]=(arr1[0]==arr2[i])?1:0;
}
//初始化二维矩阵第一列
for(int j=0;j<arr1.length;j++) {
temp[j][0]=(arr2[0]==arr1[j])?1:0;
}
for(int i=1;i<arr1.length;i++) {
for(int j=1;j<arr2.length;j++) {
//比较二维矩阵中每一个点对应行字符中是否相等,相等的话设置为1,否则设置为0
if(arr1[i]==arr2[j]) {
temp[i][j]=temp[i-1][j-1]+1;
if(temp[i][j]>length) {
length=temp[i][j];
end=j;
}else {
temp[i][j] = 0;
}
}
}
}
//找出公共字符串的起始点
start=end-length+1;
StringBuilder sb=new StringBuilder();
for(int j=start;j<end;j++) {
sb.append(arr2[j]);
}
return null;
}
}