先来个数组题热热身 数组中出现次数超过一半的数字 第一反应hashset,每个key给value+1,最后遍历value发现比数组长度一半大的数就返回 写的稀巴烂,wa的机会都没有一直报错
public static int MoreThanHalfNum_Solution(int [] array) { HashMap<Integer,Integer> map=new HashMap<>(); for(int i=0;i<array.length;i++){ int k=array[i]; if(map.containsKey(k)){ Integer value=map.get(k);map.put(k,value+1);} else{map.put(array[i],1);} } for (Integer key :map.keySet()){ Integer value=map.get(key); System.out.println(key+"-->"+value); if(value>(array.length)/2){return key;} } return 0; } public static void main(String[] args) { int [] array={1, 2, 3, 2, 2, 2, 5, 4, 2}; int x=MoreThanHalfNum_Solution(array); System.out.println(x); }然后是个sql的题获取员工其当前的薪水比其manager当前薪水还高的相关信息 先构成两个表,然后再从俩表里选要的东西 关于inner join 和left join left join(左联接) 返回包括左表中的所有记录和右表中联结字段相等的记录 right join(右联接) 返回包括右表中的所有记录和左表中联结字段相等的记录 inner join(等值连接) 只返回两个表中联结字段相等的行
select emps.emp_no as emp_no,mans.emp_no as manager_no,emps.salary as emp_salary,mans.salary as manager_salary from (select dept_emp.emp_no,dept_emp.dept_no,salaries.salary from dept_emp inner join salaries on dept_emp.emp_no=salaries.emp_no and salaries.to_date='9999-01-01') as emps, (select dept_manager.dept_no,dept_manager.emp_no,salaries.salary from dept_manager inner join salaries on dept_manager.emp_no=salaries.emp_no and salaries.to_date='9999-01-01') as mans where (emps.dept_no=mans.dept_no and emps.salary>mans.salary);然后是个java题 编写代码,移除未排序链表中的重复节点。保留最开始出现的节点
自己想了个双指针的,效率差的一比,而且很神奇的是空间使用也极惨,见鬼了,是不是评判有问题
class Solution { public ListNode removeDuplicateNodes(ListNode head) { ArrayList<Integer> arr=new ArrayList<>(); int flag; ListNode cur=new ListNode(); ListNode pre=new ListNode(); ListNode temp=new ListNode(); if(head==null||head.next==null){return head;} arr.add(head.val); pre=head; cur=head.next; temp=head; while(cur!=null) { flag=0; for(int i=0;i<arr.size();i++){ if(arr.get(i)==cur.val){flag=1;break;} } if(flag==1){temp.next=cur.next;pre=cur;cur=cur.next;} else{arr.add(cur.val);temp=cur;pre=cur;cur=cur.next;} } return head; } }其实根本不用n^2,n的时间复杂度就能解决,利用set的不可重复性
public ListNode removeDuplicateNodes(ListNode head) { if(head==null||head.next==null){return head;} Set<Integer> set=new HashSet<>(); ListNode cur=head; while(cur!=null&&cur.next!=null){ set.add(cur.val); if(set.contains(cur.next.val)){cur.next=cur.next.next;} else{cur=cur.next;} } return head; }不见鬼了,人家也没用空间,我还比人家多个指针