java面试(二十五)--(1)redis为什么读写速率快性能好(2)说说web.xml文件中可以配置哪些内容(3)&和&&的区别(4)扑克牌顺子

1. redis为什么读写速率快性能好?

1.Redis将数据存储在内存上,避免了频繁的IO操作
2.Redis其本身采用字典的数据结构,时间复杂度为O(1),且其采用渐进式的扩容手段
3.Redis是单线程的,避免了上下文切换带来的消耗,采用网络IO多路复用技术来保证在多连接的时候,系统的高吞吐量。

2.说说web.xml文件中可以配置哪些内容?

web.xml用于配置Web应用的相关信息,如:监听器(listener)、过滤器(filter)、Servlet、相关参数、会话超时时间、安全验证方式、错误页面等,下面是一些开发中常见的配置:
①配置Spring上下文加载监听器,加载Spring配置文件并创建IoC容器:

<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:applicationContext.xml</param-value>
</context-param>
 
<listener>
	<listener-class>
		org.springframework.web.context.ContextLoaderListener
	</listener-class>
</listener>

②配置Spring的OpenSessionInView过滤器来解决延迟加载和Hibernate会话关闭的矛盾:

<filter>
	<filter-name>openSessionInView</filter-name>
	<filter-class>
		org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
	</filter-class>
</filter>
 
<filter-mapping>
	<filter-name>openSessionInView</filter-name>
	<url-pattern>/*</url-pattern>
</filter-mapping>

③配置会话超时时间为10分钟:

<session-config>
	<session-timeout>10</session-timeout>
</session-config>

④配置404和Exception的错误页面:

<error-page>
	<error-code>404</error-code>
	<location>/error.jsp</location>
</error-page>
 
<error-page>
	<exception-type>java.lang.Exception</exception-type>
	<location>/error.jsp</location>
</error-page>

⑤配置安全认证方式:

<security-constraint>
	<web-resource-collection>
		<web-resource-name>ProtectedArea</web-resource-name>
		<url-pattern>/admin/*</url-pattern>
		<http-method>GET</http-method>
		<http-method>POST</http-method>
	</web-resource-collection>
	<auth-constraint>
		<role-name>admin</role-name>
	</auth-constraint>
</security-constraint>
 
<login-config>
	<auth-method>BASIC</auth-method>
</login-config>
 
<security-role>
	<role-name>admin</role-name>
</security-role>

说明:对Servlet(小服务)、Listener(监听器)和Filter(过滤器)等Web组件的配置,Servlet 3规范提供了基于注解的配置方式,可以分别使用@WebServlet、@WebListener、@WebFilter注解进行配置。

补充:如果Web提供了有价值的商业信息或者是敏感数据,那么站点的安全性就是必须考虑的问题。安全认证是实现安全性的重要手段,认证就是要解决“Are you who you say you are?”的问题。认证的方式非常多,简单说来可以分为三类:
A. What you know? — 口令
B. What you have? — 数字证书(U盾、密保卡)
C. Who you are? — 指纹识别、虹膜识别
在Tomcat中可以通过建立安全套接字层(Secure Socket Layer, SSL)以及通过基本验证或表单验证来实现对安全性的支持。

3. &和&&的区别?

相同点:
&和&&都可以用作逻辑与的运算符,表示逻辑与(and)。
不同点:
(1)&&具有短路的功能,而&不具备短路功能。
(2)当&运算符两边的表达式的结果都为true时,整个运算结果才为true。而&&运算符第一个表达式为false时,则结果为false,不再计算第二个表达式。
(3)&还可以用作位运算符,当&操作符两边的表达式不是boolean类型时,&表示按位与操作,我们通常使用0x0f来与一个整数进行&运算,来获取该整数的最低4个bit位,例如:0x31 & 0x0f的结果为0x01。

4.扑克牌顺子

LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。

示例代码:

public class Solution {
    public boolean isContinuous(int [] numbers) {

    }
}

思路:题干那么长,具体就是这样的信息,0-13,一共14个数字,0可以充当任何数字,然后输入是五个数字,判断是不是顺子连续的。

这里隐含信息就是如果连续,那肯定最大和最小差值为4,因为只有五个数字,加了0也不可能变出花来。

还有一点是,如果数字重复,那么返回错误即可。

最后就需要判断0的个数,去除0以后的数组,进行排序,最大和最小的差值必须小于4即可(因为0可能充当头尾数)。

实现代码:

import java.util.HashMap;
import java.util.Map;
public class Solution {
    public boolean isContinuous(int [] numbers) {
        //determine the array length
        if(numbers.length!=5) return false;
        //determine whether there is a repeat number
        Map<Integer,Integer> map=new HashMap<>();
        for(int i=0;i<numbers.length;i++){
            if(numbers[i]==0) continue;
            if(!map.containsKey(i)){
                map.put(i,numbers[i]);
            }else{
                return false;
            }
        }

        int zeroCount=0;
        //store non-zero element
        int[] temp=new int[numbers.length];

        for(int i=0;i<numbers.length;i++){
            if(numbers[i]==0){
                zeroCount++;
            }else{
                temp[i]=numbers[i];
            }
        }
        int tempLength=0;
        int j=0;
        while(temp[j]!=0){
            tempLength++;
            j++;
        }
        //find the min element and max element by temp array
        int max=temp[0];
        int min=temp[0];
        for(int i=1;i<tempLength;i++){
            if(temp[i]<min){
                min=temp[i];
            }
            if(temp[i]>max){
                max=temp[i];
            }
        }
        //determine whether isContinuous
        if(max-min<=4){
            return true;
        }else{
            return false;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值