自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(67)
  • 收藏
  • 关注

原创 Generate Parentheses

原题链接:https://leetcode.com/problems/generate-parentheses/description/思路:这道题要求出n对括号能够正确排列的所有情况,重点是要找到符合条件排列的特点。我们可以将长度为2n的排列,看成是n个(与n个)一次次从左往右拼接而成。如果,这个排列是合理的,那么它有着如下的特点:1. 在拼接符号的过程中,(的个数一定是小于

2017-11-23 09:21:45 245

原创 Valid Parentheses

原题链接:https://leetcode.com/problems/valid-parentheses/description/思路:括号匹配问题,之前在严蔚敏的数据结构那本书里见到过,C语言版在49页。思路是,将输入的字符串中的每一个符号c1和栈顶符号c2比较,看是否匹配(比如"()"就是匹配,")("或者"({"就是不匹配),如果不匹配,将c1入栈,如果匹配了,将c2

2017-11-20 21:39:12 243

原创 Letter Combinations of a Phone Number

原题链接:https://leetcode.com/problems/letter-combinations-of-a-phone-number/description/思路:输入一串0-9的数字,每个数字对应着电话上的1个或多个字符,要求出所有可能的字符组合,以List形式返回。假设输入“23”,先取第一个字符2,对应的字符串是“abc”,那么此时的List内元素是“

2017-11-19 13:35:42 316

原创 基本类型与自动装箱与拆箱

先说几个要注意的地方,然后根据以下的结论来分析两个题目。1.所有数值类型都有正负号2.浮点数值有后缀f或F,表示float类型,没有的话默认为double类型,double可加可不加后缀d或D。long型有后缀l或L3.不同类型运算结果向右靠齐 char4.计算机进行运算是以补码形式进行5.正数的补码就是原码,负数的补码是除符号位以外其余各位取反+16.对象包装器类

2017-11-14 16:16:17 468

原创 3Sum

原题链接:https://leetcode.com/problems/3sum/description/思路:这道题要找出不重复的合为0的三元组,做过了2Sum那道题会很容易想到借助HashMap,但是笔者尝试过,这样的方式很难解决重复的问题。暴力破解的做法是每次挑出一个元素值叫做a,然后从剩余的数组中找出和为-a的两元素,两个步骤的合在一块的时间复杂度为o(n*n^2)=o(n^3)

2017-11-04 12:42:50 265

原创 Container With Most Water

原题链接:https://leetcode.com/problems/container-with-most-water/description/思路:这道题说白了就是求(j-i)*min(height[j],height[i])的最大值,其中i,j在[0,height.length-1]中。如果暴力破解一共要算n^2个值找出最大的,算法的目的就是减少需要计算的值,也就是说剔除掉肯定不

2017-11-03 15:08:28 318

原创 Longest Palindromic Substring

原题链接:https://leetcode.com/problems/longest-palindromic-substring/description/思路:这题要求一个字符串的最长回文串,注意到回文串有着中心对称的特点,而且有两种对称方式。一种是字符串所含字符串是单数个,比如aba,另外一种偶数情况,比如baab。那么我们要求字符串的最长回文串,如果字符串为空或者只含一个字符那么直接返

2017-11-02 19:07:09 301

原创 G1垃圾收集器

G1:Garbage First特点:1.并行与并发:G1能充分利用多CPU,多核环境下的硬件优势来缩短Stop the world停顿的时间。并行:Parallel,多个GC进程一起执行。并发:Concurrent,GC进程与用户进程一起执行。2.分代收集:G1不需要其他垃圾收集器就能独立管理整个GC堆,但它能采用不同的方式来回收新对象与旧对象来获得更好的收集效果。3.空

2017-10-23 22:17:54 367

原创 CMS垃圾收集器

CMS收集器特点:1.以获取最短回收停顿时间为目标2.CMS全称是 Concurrent Mark Sweep。从名字就可以看出来两个特点,一是并发,这里并发并不是指多条GC进程一起工作,而是指GC进程可以与用户进程一起工作。二是,该垃圾收集器使用标记清除算法的方式回收垃圾。收集过程:1.初始标记:这个过程需要Stop the world,初始化标记仅仅只是标记

2017-10-23 21:30:12 480

原创 java方法调用中的单分派与多分派

方法的接受者与方法的参数统称为方法的宗量。根据分派基于多少宗量,可以将分派分为单分派与多份派。《深入理解java虚拟机》一书中写到,java是静态多分派动态单分派的语言,我一直没有理解,我觉得静态分派是根据方法参数的类型和顺序来决定调用哪一个重载方法。而动态分派就是在运行期根据方法调用者的实际类型来决定调用哪一个重写的方法。这不都是单分派么?我不知道有没有读者有或者曾经有过与我相似的困惑,

2017-10-17 22:55:59 520

原创 java中的方法调用-解析与分派

在本文开始之前,先搞清楚一个概念。对于People p = new Man();这句代码,我们把People叫做静态类型,Man叫做动态类型。静态类型在编译期可知,实际类型在运行期才可以确定下来。解析所有方法调用中的目标方法在Class文件里面都是一个常量池中的符号引用,在类加载的解析阶段,会将其中一部分符号引用转化为直接引用,这种解析能成立的前提是:方法在程序真正运行之前就有一个可确定的

2017-10-17 14:52:44 1589

原创 java 中的向前引用

在学习《深入理解java虚拟机》一书中,关于类的初始化一章中提到了一句:静态语句块中只能访问到定义在静态语句块之前的变量,定义在它之后的变量,在前面的静态语句块中可以赋值,但是不能访问。并给出一个示例:public class MyClass { static { i = 0; //给变量赋值可以正常通过 System.out.println(i); //编译器提示非法向前引用

2017-10-15 13:06:39 1659 2

原创 java中关于final方法与static方法不具备多态性的理解

先说明一下这里的多态性特指方法的重写。方法的重载不会受到final,static修饰符的影响,一个类中当然可以有多个static或者final修饰的同名但不同参数类型的方法,重载根据传入参数的实际类型来确定调用哪一个方法。那为什么final(private方法是一种隐式的final方法)和static方法不能够被重写呢?1.final方法很好理解,因为不可以被继承,自然不可以被子类

2017-10-13 10:02:02 424

原创 构造器的多态行为

我们知道,在调用父类构造器时,得先要调用子类构造器。如果父类有方法f(),子类对其重写了,恰好父类构造器中用到了这个f()方法,那么这个f()调用的是父类中的f()还是子类中已经重写的f()呢?不妨用一个例子试试看,这个例子引用自Thinking in java。class Glyph { void draw() { System.out.println("Glyph.dra

2017-10-12 22:10:55 614

原创 java中涉及继承关系的初始化顺序

在http://blog.csdn.net/xdugucc/article/details/78217098这篇博文中对单个类成员变量初始化和构造器初始化顺序做了介绍。而这篇将讲解具有继承关系的类中的初始化顺序。一个类中具有静态成员和静态代码块,非静态成员和非静态代码块,构造器这些需要初始化的东西。现在他有一个父类。那么当生成一个子类对象时,必然需要对父类中需要初始化的东西进行初始

2017-10-12 21:09:15 482

原创 java中的访问权限控制

访问权限修饰词java中提供了四种访问权限修饰词,分别为private,default,protected,public。这四种修饰词用来修饰成员变量与方法(下面统称为成员),从前往后访问权限越来越宽。现在分别进行介绍1.privateprivate修饰的成员只能被该类本身所访问,其他类都不可访问到private修饰的成员。2.default默认的权限修饰符,默认的(不加修饰符

2017-10-12 19:29:18 390

原创 java中成员初始化与构造器初始化的顺序

1.初始化顺序先看一个例子class Counter { int i; Counter() { //i = 7; }}public class Test { public static void main(String[] args) { System.out.println(new Counter().i); }}我们执行这段程序,当i=7被注释掉,打印出0。这说

2017-10-12 17:04:20 2253

原创 一个关于字符串常量池的问题

看一个例子public class Test4 { public static void main(String[] args) { String s1 = "b"; String s2 = "a"; String s3 = s1 + "d"; String s4 = "e" + "f"; String s5 = s1 + s2; } }问题是,现在字符串常量池

2017-10-11 16:36:10 704

原创 String.intern()方法

本文介绍String.intern()方法这个方法在jdk1.6与idk1.7之后发生了变化。主要是因为jdk1.7之后,方法区中常量池的位置从方法区变成了堆上,intern()方法也做了相应的修改。直接看一个网上很流行的例子。 String s = new String("1"); s.intern(); String s2 = "1"; Syste

2017-10-11 13:31:13 586 1

转载 String之常量池小结

转载自http://blog.csdn.net/xsf507171、String 常量池为了减少在JVM中创建的字符串的数量,字符串类维护了一个字符串池,每当代码创建字符串常量时,JVM会首先检查字符串常量池。如果字符串已经存在池中,就返回池中的实例引用。如果字符串不在池中,就会实例化一个字符串并放到池中。Note:常量池在java用于保存在编译期已确定的,

2017-10-10 16:07:37 6309 6

原创 shell 中bad substitution错误

今天在学习linux写shell脚本的时候,碰到了一个bad substitution错误。脚本的内容是输入一个文件名,创建出三个文件名+日期(今天,昨天,前天)的文件。有错误的代码如下: 1 #!/bin/bash 2 PATH=/bin:/sbin

2017-09-30 13:47:21 82451

原创 Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.Examples:Given "abcabcbb", the answer is "abc", which the length is 3.Given "bbbbb", the answer is "

2017-09-29 10:10:23 200

原创 Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings思路:题目的要求是求字符串数组strs中所有字符串的最长公共前缀子串。第一步,先把前缀子串pre 设为strs[0].第二步,判断一个字符串中是否含有另一个字符串可以使用indexof()方法,它返回的是子

2017-09-28 22:08:28 171

原创 Reverse Linked List II

Reverse a linked list from position m to n. Do it in-place and in one-pass.For example:Given 1->2->3->4->5->NULL, m = 2 and n = 4,return 1->4->3->2->5->NULL.Note:Given m, n satisfy the

2017-09-28 21:35:53 193

原创 Partition List

Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of

2017-09-28 20:41:16 191

原创 Remove Duplicates from Sorted List II

Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1

2017-09-28 19:59:54 295

原创 Rotate List

Given a list, rotate the list to the right by k places, where k is non-negative.For example:Given 1->2->3->4->5->NULL and k = 2,return 4->5->1->2->3->NULL.思路:这道题的要求不用多说,看上述的例子就能看懂。说几

2017-09-28 19:12:44 182

原创 Swap Nodes in Pairs

Given a linked list, swap every two adjacent nodes and return its head.For example,Given 1->2->3->4, you should return the list as 2->1->4->3.Your algorithm should use only constant space. Y

2017-09-28 11:03:06 170

原创 Remove Nth Node From End of List

Given a linked list, remove the nth node from the end of list and return its head.For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the

2017-09-28 10:18:55 188

原创 Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i

2017-09-25 22:50:26 177

原创 Linked List Cycle

Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路:要判断一个链表是否成环,先想象一下成环的样子,一定是0字型或者9字形,而不会是8字型。想象一下,绕城的环就像是一个操场。有两个人在跑步速度不一样,如果

2017-09-25 21:38:08 196

原创 Delete Node In A LinkedList

Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value

2017-09-25 21:00:06 217

原创 Palindrome Linked List

Given a singly linked list, determine if it is a palindrome.Follow up:Could you do it in O(n) time and O(1) space?思路:题目要求判断一个链表是否是回文链表。并且规定时间复杂度为o(n),空间复杂度为o(1)。既然要判断一个链表是否回文,并且空间复

2017-09-25 15:47:18 202

原创 Reverse LinkedList

Reverse a singly linked list.思路:问题描述很简单,翻转一个链表。我们搜索链表,将每一个节点cur的前驱pre作为它的后继。然后继续下一个节点,但是我们在寻找下一个节点的时候(cur = cur.next),因为更改了cur的后继,cur.next反而会指向cur的原前驱节点pre。故我们需要用一个临时的指针temp来保存cur的原后继,在cur修改了后

2017-09-25 14:42:54 270

原创 Remove Linked List Elements

Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5Credits:思路:

2017-09-25 13:24:35 170

原创 Remove Duplicates from Sorted List

Given a sorted linked list, delete all duplicates such that each element appear only once.For example,Given 1->1->2, return 1->2.Given 1->1->2->3->3, return 1->2->3.思路:这是一个比较简单的操作链表的

2017-09-24 20:20:14 193

原创 Merge Two Sorted Lists

Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. * public class ListNode { *     int val; *     L

2017-09-24 15:45:24 164

原创 Longest Continuous Increasing Subsequence

Given an unsorted array of integers, find the length of longest continuous increasing subsequence.Example 1:Input: [1,3,5,4,7]Output: 3Explanation: The longest continuous increasing subseque

2017-09-24 14:56:46 188

原创 NonDecreasingArray

Given an array with n integers, your task is to check if it could become non-decreasing by modifying at most 1 element.We define an array is non-decreasing if array[i]  holds for every i (1

2017-09-24 14:08:40 193

原创 ImageSmoother

Given a 2D integer matrix M representing the gray scale of an image, you need to design a smoother to make the gray scale of each cell becomes the average gray scale (rounding down) of all the 8 surro

2017-09-24 11:07:05 192

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除