最近开始学习java后台,记录一下自己每天项目和基础知识的学习以及面试中遇到的一些问题。
vivo笔试
- 已知整型数组A、B,求数组A中存在而数组B中不存在的元素,按原顺序输出
暂时只想到O(n^2)的解法,有更好解法望在评论提及;
每次遍历时,通过覆盖重复元素来达到减少交换次数的目的;
输入异常时需要抛出异常并在main函数中处理,如果不处理main函数需要继续抛出异常。
import java.io.IOException;
public class DeleteElement {
public void deleteElement(int[] arrA,int[] arrB) throws IOException {
if(arrA.length == 0){
throw new IOException("非法输入");
}
if(arrB.length == 0){
for (int i = 0; i < arrA.length; i++) {
System.out.print(arrA[i]+" ");
}
}
int sum = arrA.length;
for (int i = 0; i < arrB.length; i++) {
if(arrA.length!=0) {
int index = 0;
for (int j = 0; j < sum; j++) {
if (arrB[i] != arrA[j]) {
arrA[index] = arrA[j];
index++;
}
}
sum = index;
}
}
for (int i = 0; i < sum; i++) {
System.out.print(arrA[i]+" ");
}
}
public static void main(String[] args) throws IOException{
int[] arrA = {1,2,2,4,5};
int[] arrB = {2,5};
DeleteElement de = new DeleteElement();
de.deleteElement(arrA,arrB);
}
}
输出:1 4
- 反转单链表的第M到N个节点,然后打印反转后的链表(与leetcode 92题类似);
package com.leetcode.medium;
class ListNode{
int val;
ListNode next;
ListNode(int x){
this.val = x;
}
}
public class ReverseBetween {
public ListNode reverseBetween(ListNode head, int m, int n){
if(head == null | m > n){
return null;
}
if(m == n){
return head;
}
int step = n - m;
//反转完成的头节点
ListNode nNode = null;
//头节点的下一节点
ListNode nNodeNext = null;
ListNode mPrev = head;
ListNode curr = head;
for (int i = 1; i < m; i++) {
if(curr.next.next != null){
mPrev = curr;
curr = curr.next;
}
else{
return head;
}
}
//反转完成后的尾节点
ListNode mNode = curr;
//反转完成后指向反转链表头节点的节点
ListNode prev = mPrev;
for (int i = 0; i <= step; i++) {
if(curr == null){
break;
}
ListNode next = curr.next;
curr.next = prev;
nNode = curr;
prev = curr;
curr = next;
nNodeNext = curr;
}
mPrev.next = nNode;
mNode.next = nNodeNext;
//m=1时此时的头节点为nNode
if(mPrev == mNode){
return nNode;
}
return head;
}
public static void main(String[] args) {
int[] arr = {3,5};
int m =1;
int n = 2;
ListNode head = null;
ListNode prev = null;
for (int i = 0; i < arr.length; i++) {
ListNode curr = new ListNode(arr[i]);
if(head == null){
head = curr;
prev = head;
}
else{
prev.next = curr;
prev = curr;
}
}
ReverseBetween rb = new ReverseBetween();
ListNode newHead = rb.reverseBetween(head,m,n);
while(newHead != null){
System.out.print(newHead.val + " ");
newHead = newHead.next;
}
}
}
输出:5 3
- 每个商品有两个属性,热度和单价,限定总钱数,求热度最大的购买方法。
解法参考:
背包问题(对算法描述比较清楚)
动态规划之01背包问题(有图)
关于第二层循环倒序的思考:
计算当前循环的totalValue[j]需要上一次循环的计算得到的totalValue数组,因此需要从尾部开始覆盖。
/**
* 给定物品,有两个属性,热度(价值)与单价,求给定总钱数的情况下价值最大的买法
*/
public class MaxValue {
public int[] maxValue(int[] value,int[] price,int total){
int n = value.length;
int[] totalValue = new int[total+1];
for (int i = 1; i <= n; i++) {
for (int j = total; j >= price[i-1]; j--) {
totalValue[j] = totalValue[j] > (totalValue[j-price[i-1]]+value[i-1])?totalValue[j]:(totalValue[j-price[i-1]]+value[i-1]);
}
}
return totalValue;
}
public static void main(String[] args) {
int[] price = {2,2,6,5,4};
int[] value = {6,3,5,4,6};
int total = 10;
MaxValue maxValue = new MaxValue();
int[] max = maxValue.maxValue(value,price,total);
for (int i = 1; i <= total; i++) {
System.out.print(max[i]+" ");
}
}
}
输出:0 6 6 9 9 12 12 15 15 15
jeesite
公司基于jeesite框架开发的平台的学习
1.heightcharts、echarts,echarts3
java基础知识
java i/o操作
基础IO类
字节流输入:InputStream
字节流输出:OutputStream
字符流输入:Reader
字符流输出:Writer
IO采用装饰者模式,
线程
我是一个线程
作者:码农翻身
七种排序算法
漫画版的七种排序算法
作者: 河北小博博
一致性哈希算法
五分钟理解一致性哈希算法
作者:cywosp
我的github
我的github:https://github.com/pyymoon
里面有一些leetcode和剑指offer的java实现,还有一个前端输入与后端数据库互动的小demo。