Category Archives: Uncategorized

leetcode MID

424. Longest Repeating Character Replacement

Q: given a string, replace its char for at most k times, then find the longest substring containing repeating characters
A: sliding window both s&e resides at idx 0. initially e going forward, and a count array counts the # of 26 characters inside the window. longest repeating substring inside the window = the char with max count + k, when e reach this longest length, we get the first possible answer and update the global max. while the window size > the longest length we let s++ and update the count array
Sliding window template:
s&e all resides at idx 0, outer while loop e keeps moving, inner while loop when certain conditions are met s++ until the condition breaks and do sth then e++.

油腻的社会

看到空姐打滴滴被害的消息,心里很不是滋味。很愤怒,很难受,花样年华的姑娘,死的时候身上还有遗精,被刺了20多刀,我都能想象在被施暴的时候女孩有多屈辱,多痛苦。操蛋,这种悲剧发生了无数无数次了,可是我知道除非人类灭绝了这种事情就根本不会避免!知道最可恨的是什么吗?是无论你接受了多么良好的教育多么有仁义道德,可是你遇到了这么一个贱人他不讲仁义道德他不遵守这一套,这个时候剩下什么?最原始的厮杀!一个弱女孩厮杀的过一个正常的成年男的?这个时候你就知道你受到的什么教育,什么知识,什么算法,毛用没有,你干不过他,你只能忍受屈辱要么死,可是哪个女孩受得了这个屈辱?21岁被父母宠大的菇凉能受得了吗?社会教育女性在这种情况下:准备套套,忍受一下,保住自己的小命最重要。社会,家人都这么苦口婆心的教育自己的女儿,是的,留得青山在不怕没柴烧,可是凭什么这种屈辱要女性受着?

我不敢想象当时的场景,想到就心里说不出的难受!年轻的女孩子在大城市打拼,深夜加班是很多时候很多人都有的情景,刚开始除了家境好的,怎么买车?买了车自己租的那小房子还不知道有没有车位。想来想去除了打车真的没有什么选择了,如果有谁能告诉我?这种事情真的避免不了吗?这种时候除了忍受屈辱别无选择?凭什么!!所有的女性成长过程中多多少少都受了些不同程度的性骚扰,最严重的就是这种先奸后杀,真不知道是怎么样的丧心病狂,心理变态,才会做出这种恶心的事情。可是这就是发生了,发生了很多次!!中国外国都有,这样心痛的次数还少吗?这种痛苦的时候还少吗?可是骂归骂,到底应该怎么办,无解,说白了,还是提高警惕,宁可忍受屈辱也要保命。这个社会本来就是男性向的,呵呵,所以我从来不倡导什么女性平等,因为狗屁,永远不可能平等,再发达的地方也不可能。

不知道能说什么,该说什么,已然香消玉殒的生命,再多的哀叹也无济于事。因为还有更多正在奋斗的女孩需要警醒需要措施。女孩需要的不是玛丽苏,不是小鲜肉小奶狗,是清醒的头脑,是明白这个社会油腻粗糙的冷静,是拒绝一切恶心的勇气。我知道,无论自己多么少女心,无论我的房间被布置的多么可爱温馨,外面的世界从来没有改变过它的残忍丑恶,深夜的大街从来不会是宁静美好,人的丑恶和他们的美好都会在特定情况下发挥到极致,永远永远不要让自己至于那种境地,这个世界的丑态永远比我们想象的多的多的多,就像这个世界的美好也永远比你想象的多的多。

宠着女孩,不如让她早早认清现实,在提供物质前,让她有一颗强大冷静的心,不要让好女孩沉浸在一片歌舞升平,客客气气的氛围之中,早点告诉她,有人开车会冲你竖中指,有的人会对你性骚扰,有人会在职场给你下套。。置身于这个社会会遇到形形色色的人,我知道保持礼貌和善良的本心最重要,但是很多人不配你用善良对待,很多人自己低劣无比无药可救,所以这种情况有能力的就让他滚蛋,没能力的就先保住自己。千万别以为自己可以感化他们,千万不要相信你的小学课本和心灵鸡汤。有些人,就是渣滓,就是无药可救。

心痛,说了这么多,我又特么说了什么?

怂或不怂,我有点困惑

今天开车又被黑人竖中指了,这是第三次,他在我前面,我的性格当然不能忍,我一直不停的按喇叭把他按走了。我其实不生气,没几分钟就恢复了,但是我还是很困惑。

我觉得自己很怂,因为我只敢滴他,但是内心深处的我其实想摇下窗户,边滴他边指着他,让他回头看着我,然后冲着他竖中指,再对他说:fuck off!可是又有人告诉我要注意安全,说这种人都是把枪放在自己车上身边,他们有路怒症,随时可能冲我开枪然后我得不偿失。。我当然不敢怼有枪的人,可是我又不甘心就这么莫名其妙的就被竖中指了,我真的忍不了。难道就因为他有枪,就可以为所欲为,我就必须忽视他对我赤裸裸的侮辱?难道就因为我在异国他乡,这种人就骑在我头上?难道我就这么窝窝囊囊的什么也不敢说不敢做,连被人都欺负成这样了还得但没事一样?我做不到,我也可以拥有持枪证,我也可以自己随身带着,如果他冲我开火我也可以对着他开,为什么我要忍?为什么他们是神经病脑残我就要忍着他们?他们凭什么不受法律的控制?而我又为什么这么弱,弱到怕这种垃圾?我要是什么事情都不敢,什么鸟人都害怕,我这一生也特么太怂了。可是如果他万一是那种疯子,不要命的,毒贩子什么的,我就真的性命难保了。真是困惑。这地方真让我恶心,虽然我也经常会喜欢这儿。

看不上这种垃圾,永远生活在社会底层的愚昧无知的未开化的脑子,河马都比这种强。

Java sort algorithms

insertion sort:

algorithm: given an int array arr with length=n, define key=arr[i: i= 1~n-1], and need to put each key in the correct position, how? first save the key=arr[i], and compare the key with arr[j: j=0~i-1], if arr[j] is larger then move arr[j] to arr[j+1].

how it works: each time we define a key, we’ll need to compare the key with all its preceding elements, and the key’s preceding subarray is always in a increasing order.

code:


 

JAVA Web light development

database

reference: http://blog.csdn.net/qingking520/article/details/52937728

第一范式(1NF):强调的是列的原子性,即列不能够再分成其他几列。
考虑这样一个表:【联系人】(姓名,性别,电话)
如果在实际场景中,一个联系人有家庭电话和公司电话,那么这种表结构设计就没有达到 1NF。要符合 1NF 我们只需把列(电话)拆分,即:【联系人】(姓名,性别,家庭电话,公司电话)。1NF 很好辨别,但是 2NF 和 3NF 就容易搞混淆。
第二范式(2NF):首先是 1NF,另外包含两部分内容,一是表必须有一个主键;二是没有包含在主键中的列必须完全依赖于主键,而不能只依赖于主键的一部分。
考虑一个订单明细表:【OrderDetail】(OrderID,ProductID,UnitPrice,Discount,Quantity,ProductName)。
因为我们知道在一个订单中可以订购多种产品,所以单单一个 OrderID 是不足以成为主键的,主键应该是(OrderID,ProductID)。显而易见 Discount(折扣),Quantity(数量)完全依赖(取决)于主键(OderID,ProductID),而 UnitPrice,ProductName 只依赖于 ProductID。所以 OrderDetail 表不符合 2NF。不符合 2NF 的设计容易产生冗余数据。
可以把【OrderDetail】表拆分为【OrderDetail】(OrderID,ProductID,Discount,Quantity)和【Product】(ProductID,UnitPrice,ProductName)来消除原订单表中UnitPrice,ProductName多次重复的情况。
第三范式(3NF):首先是 2NF,另外非主键列必须直接依赖于主键,不能存在传递依赖。即不能存在:非主键列 A 依赖于非主键列 B,非主键列 B 依赖于主键的情况。
考虑一个订单表【Order】(OrderID,OrderDate,CustomerID,CustomerName,CustomerAddr,CustomerCity)主键是(OrderID)。
其中 OrderDate,CustomerID,CustomerName,CustomerAddr,CustomerCity 等非主键列都完全依赖于主键(OrderID),所以符合 2NF。不过问题是 CustomerName,CustomerAddr,CustomerCity 直接依赖的是 CustomerID(非主键列),而不是直接依赖于主键,它是通过传递才依赖于主键,所以不符合 3NF。
通过拆分【Order】为【Order】(OrderID,OrderDate,CustomerID)和【Customer】(CustomerID,CustomerName,CustomerAddr,CustomerCity)从而达到 3NF。

 

web container, servlet container

reference:

http://blog.csdn.net/yalishadaa/article/details/70544492

 

container: 所谓容器(服务器、中间件等,就是提供一些底层的、业务无关的基本功能,为真正的Servlet提供服务。简单来说:容器负责根据请求的信息找到对应的Servlet,传递Request和Response参数,调用Servlet的service方法,完成请求的响应。

web container: web容器更准确的说应该叫web服务器,它是来管理和部署web应用的. web容器最典型的就是tomcat了,Tomcat是web容 器也是servlet容器.

servlet container

servlet: Servlet是服务器端运行的一个程序,是一个被编译好的java类。它不是框架等; Web容器的启动,需要依赖Servlet。当web服务器开始执行时,servlet类就被初始化; 当用户通过浏览器访问的时候,会输入URI,这个时候,web服务器就通过Servlet来分发请求执行不同的内容.

in spring

我的心又一次火热燃烧

俄罗斯老牌乐队lube,一曲白桦林,让我几度哽咽,眼眶湿润,我的心在寂静的深夜里火热燃烧,在喧闹的车流中却又平静如水。它就是这样,像在浅吟低唱,在静静诉说,在娓娓道来,可是,那种悲伤,惆怅,一次又一次的,看似平和的揉入我的身体,却激荡起情绪的波浪,让我不能平静。

像白桦树叶掉落一样,我离开了家乡,心爱的姑娘你不必悲伤,我会回来,年迈的母亲请停下你颤抖的步伐,我会归来,在你身旁,就像儿时一样。为何树叶会忧郁的飘落?我知道,我和它一样,叶子在你奋不顾身投入到大地的怀抱时,是不是也和我一样,那颗火热的心燃烧着,期盼着,融入泥土中,以另外一种方式回到了你生长的地方。可是我呢?我走在着宽广的道路上,这也许是我唯一的欢乐吧,因为我不知道前路是否宽广,我不知道是否有胜利的曙光,我不知道安宁平和会不会真的到来,我不知道,我心爱的姑娘,亲爱的母亲,能否等到我的归来?我身处什么地方,我又该去何处,我的身体四处漂泊,心却又一直停留在那个地方,可是我的愿望,久久得不到回响。。

诗一般的白桦林,没有一句提到战争,却句句让人想到战争,这样细腻的情感,这样的铁汉柔情,让我动容。它没有告诉你善与恶,对与错,只是一个普通的战士的静静诉说。那些爱的死去活来的,都比不上“与心爱的你坐在小路旁,要知道我会回来,这不值得悲伤”;那些感伤与父母离别的,也许不及这一句“老妈妈向我挥手道别,篱笆门在我身后缓缓关上”。

我知道,很多时候我会不相信一些人,一些情感,觉得这个世界浮躁。可是在音乐里,当我遇到了那个它,我的心会火热燃烧,不为任何目的,就是这不能释怀的感动,这一刻的眼眶湿润。我如死灰的心重新点燃,原本我的灵魂如一潭死水,但是终究会被挚爱的东西激起浪花,让我的内心重新变得柔软温润,哪怕只是这短短的一刻,这一刻,与它的心意相通,让我不再孤单,哪怕我明白,也许不能寻得那个陪你走过一生的人,也许深爱你的人终会离去,可是,我拥有着,这短暂的永恒。

白桦林

为何白桦在俄罗斯大地沙沙作响?

为何雪白的树身无所不晓?

沿着风的方向立在小路旁

叶子掉落满地忧伤,

沿着道路走向宽广,我心情欢畅,

这欢乐许是生活赋予我的唯一也是所有。

为何树叶忧郁地飘落

抚慰灵魂深处的心伤。

我的心一次又一次火热燃烧,

却总是得不到回响。

白桦树叶掉落肩头,

像我一样,离开了生长的地方。

与心爱的你坐在小路旁,

要知道我会回来,这不值得悲伤。

老妈妈向我挥手道别,

篱笆门在我身后缓缓关上。

为何白桦在俄罗斯大地沙沙作响?

为何手风琴演奏的如此美妙?

风儿的指尖掠过之处,树叶儿漫天飞舞,

而最后的一片啊,也飘落在地上。。。

我的心一次又一次火热燃烧,

却总是得不到回响。

白桦树叶掉落肩头,

像我一样,离开了生长的地方。。。

spring on the web

a client’s request passing spring mvc: a request (user request a web page) first sent to front controller/DispatcherServlet (a servlet which is responsible to designates tasks to different mvc components), front controller consult handler mappings to pass the request to the correct controller; then controller send the request to its service objects & wait for response; the response is some database raw data and view name (model&view name) and is not user-readable; then controller pass the model&view name back to front controller & front controller consult view resolver where to render the view, finally implement the view and send response back to the client.

set up java-based spring mvc: define a webAppInitializer app (create/register/map the DispatcherServlet); define a spring mvc config file (enables component scan for controllers, configure a JSP view resolver..); define a model class & a jsp page. see: https://samerabdelkafi.wordpress.com/2014/08/03/spring-mvc-full-java-based-config/ for a detailed full java based spring mvc example.

write a simple controller: annotate the controller class with @Controller (such that the component scanner in the spring mvc config file can automatically declare it as a bean, @Component also works);  create a method inside the controller class & annotate the method with @RequestMapping, the method will then return the view name to DispatcherServlet (@RequestMapping handles HTTP requests for a certain request path)

Spring core

spring is a alternative of EJB (enterprise java beans), with lighter & leaner programming models

a spring bean refers to a spring application component; refer to any POJO (plain old java obj)

*Spring simplifies java development*

non-invasive: Spring never force you to use Spring-specific classes/interfaces, thus you can transplant spring projects to non-spring platform.

dependency injection: class A depends on B, B depends on C, if we define C inside B & B inside A, then when we want to change C, we’ll have to change corresponding B, A; To avoid this, we create separate spring bean for A, B, C, and use spring application context to create A/B/C objects and wire them together, thus, A is created without knowing detailed implementation of B & C.

AOP (Aspect Oriented Programming): suppose in a system, class A object first call A’s method, and will trigger class B call B’s method. If we write & call B’s method inside A bean, then B has to be injected into A, and A must maintain B all the time. To avoid this, we turn B into a aspect in Spring application context, and define B’s method to be called after A’s method is called, but not inject B into A.

AOP – declare B as an aspect: use Spring’s <aop:config> to declare B as an aspect, and first declare B as a bean, then refer to that bean in <aop:aspect> . Since B’s method will be triggered after A’s method is called, so inside B’s aspect, define A’s method as pointcut, and refer B’s method to that pointcut. Thus, B’s method applied to A without A needing to call on it inside A.

Spring template: eliminate boilerplate code. If you want to query an employee by his id in a Java JDBC API, besides the core query algorithm, JDBC requires that you must write create a connection, create a statement, and catch SQL exceptions…etc. But Spring provides template for such JDBC requirements, and let you focus only on the core query algorithm.

*spring container*

spring container: it creates objects, inject DI into them, and wire them together, configures them, and mange their lifecycle from begin to end.

two-kind container implementation: bean factory & application context.

an application context: create it by loading application context from XML/Java config file, from filesystem/classpath. (what is classpath? see: https://stackoverflow.com/questions/2396493/what-is-a-classpath)

application context retrieve beans: use the context’s getBean() method.

bean’s lifecycle: see 1.2.2

*Spring Landscape*

JAR file: a JAR (java archive) file is a package file format used to aggregate many java class files and associate metadata and resources (image, sound, etc.) into one file for distribution.

spring lib folder: 20 distinct modules, with 3 JAR files for each one (the binary class library, the source JAR file, and a javadoc JAR file). 20 JAR files can be arranged into 6 categories of functionality – Figure 1.7.

*3 spring wiring mechanisms*

**automatic wiring**

component scanning: originally, in spring configuration file, we should define class A, B as beans and wire them together inside a configuration class; now we annotate the config class with @ComponentScan, and annotate A, B with @Component, then @ComponentScan will  by default scan the config class’s package and any subpackages underneath, look for classes that are annotated with @Component (A, B), and create beans for them in spring.

name a component-scanned bean: all beans in spring config file are given an ID. If class A is annotated with only @Component, then it is implicitly given an ID by lowercasing the first letter of the class name; you can also self-define the ID and annotate class A with @Component(“xxx”) or @Named(“xxx”) since @Named is alternative to @Component in most cases.

set base packages for component scanning: if the config class is annotated with @ComponentScan, spring will by default take the config class’s package as the base package and scan for components; if @ComponentScan(basePackageClasses={K.class, M.class}), spring will take whatever packages that class K&M are in as base packages.

autowiring: if class A is annotated with @Autowired/@Inject, then when spring creates A bean, it will automatically satisfy bean A’s dependencies by finding other beans in the application context that are a match to bean A’s needs.

@Autowired: it can be used in any methods in class A. if no matching beans found for a DI, spring will throw an exception as the application context is created; if multiple matching beans found for a DI, spring will throw an exception indicating ambiguity with auto-wiring.

*spring层次图*

structs: early time web mvc framework.

dao: is an object that provides an abstract interface to some type of database or other persistence mechanism. By mapping application calls to the persistence layer (hibernate), the DAO provides some specific data operations without exposing details of the database.

hibernate: It provides a framework for mapping an object-oriented domain model to a relational database, it can replace SQL to retrieving data from database.