java
YatKam
成长在于积累,分享是种美德!
展开
-
JDBC中Class.forName()的作用
使用JDBC时Class.forName()的作用Class.forName(xxx.xx.xx) 返回的是一个类 首先你要明白在java里面任何class都要装载在虚拟机上才能运行。这句话就是装载类用的(和new 不一样,要分清楚)。 至于什么时候用,你可以考虑一下这个问题,给你一个字符串变量,它代表一个类的包名和类名,你怎么实例化它?只有你提到的这个方法了,不过要再加一点。转载 2015-05-27 15:48:22 · 5502 阅读 · 0 评论 -
LeetCode解题报告--Longest Common Prefix
题目:最长公共前缀 Write a function to find the longest common prefix string amongst an array of strings.分析:题意找出所有字符串的最长公共前缀。 思路简单:如下步骤 1. 找出字符串数组中字符串长度最小的字符串minStr 2. minStr与其余的字符串逐个字符比较,确定最长公共前缀java 代码:(a原创 2015-08-14 15:43:40 · 1544 阅读 · 0 评论 -
LeetCode解题报告--3Sum Closest
题目:与3数和最接近的和 Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would原创 2015-08-16 16:46:24 · 1010 阅读 · 0 评论 -
LeetCode解题报告--3 Sum
题目: 3 个数和问题 Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note: Elements in a triplet (a,b原创 2015-08-16 16:37:27 · 1499 阅读 · 1 评论 -
LeetCode解题报告--Letter Combinations of a Phone Number
题目:字母组合的电话号码 Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below. Input:D原创 2015-08-17 17:43:42 · 2463 阅读 · 0 评论 -
LeetCode解题报告--2Sum, 3Sum, 4Sum, K Sum求和问题总结
前言: 这几天在做LeetCode 里面有2sum, 3sum(closest), 4sum等问题, 这类问题是典型的递归思路解题,该这类问题的关键在于,在进行求和求解前,要先排序Arrays.sort()可实现,而本文则着重探讨关于KSum问题。 leetcode求和问题描述(K sum problem): K sum的求和问题一般是这样子描述的:给你一组N个数字(比如 vector原创 2015-08-17 18:22:02 · 3681 阅读 · 0 评论 -
LeetCode解题报告--4Sum
题目: 4Sum Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.Note: Elements原创 2015-08-17 17:54:37 · 889 阅读 · 0 评论 -
LeetCode解题报告---Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321原题出处:https://leetcode.com/problems/reverse-integer/解题思路:如果输入”100”,”10000”,反转之后输出时应考虑前导‘0’;输入溢出时时以及反转输出时溢出直接原创 2015-06-03 03:55:18 · 640 阅读 · 0 评论 -
LeetCode解题报告--String to Integer (atoi)
Implement atoi to convert a string to an integer.Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yourself what are the possible input cases.N原创 2015-06-05 05:28:49 · 766 阅读 · 0 评论 -
LeetCode解题报告--Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. 题目来源:https://leetcode.com原创 2015-05-17 16:15:36 · 785 阅读 · 0 评论 -
LeetCode解题报告--ZigZag Conversion
题目来源:https://leetcode.com/problems/zigzag-conversion/P AH N AP LS I I G YI R 解法一: 间距为: icount = 2 * (nRows - 1) 每列的字符个数为:i = nRows[from 0 to (nRows - 1)] 中间未加粗字符其下标为: mid = j + icount - 2 * i原创 2015-06-02 00:04:47 · 706 阅读 · 0 评论 -
LeetCode解题报告-- Count and Say
题目: The count-and-say sequence is the sequence of integers beginning as follows: 1, 11, 21, 1211, 111221, … 1 is read off as “one 1” or 11. 11 is read off as “two 1s” or 21. 21 is read原创 2015-12-03 15:05:12 · 2613 阅读 · 1 评论 -
LeetCode解题报告--Next Permutation
题目: Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest pos原创 2015-12-03 20:54:30 · 1838 阅读 · 1 评论 -
LeetCode解题报告--Search for a Range
题目: Given a sorted array of integers, find the starting and ending position of a given target value. Your algorithm’s runtime complexity must be in the order of O(log n). If the target is not f原创 2015-12-04 13:32:03 · 2410 阅读 · 0 评论 -
LeetCode解题报告--Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. You may assume no duplicates in the a原创 2015-12-04 16:58:11 · 1806 阅读 · 0 评论 -
LeetCode解题报告--Combination Sum II
题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. Each number in C may only be used once in the comb原创 2015-12-07 21:10:15 · 1173 阅读 · 0 评论 -
LeetCode解题报告--Integer to Roman
题目:阿拉伯数字转罗马数字Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.原题链接地址:https://leetcode.com/problems/integer-to-roman/ 分析:题意将阿拉伯数字num转罗马数字 拼写原创 2015-08-13 19:39:20 · 899 阅读 · 0 评论 -
Reverse Integer 解题报告
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321原题出处:https://leetcode.com/problems/reverse-integer/解题思路:如果输入”100”,”10000”,反转之后输出时应考虑前导‘0’;输入溢出时时以及反转输出时溢出直接原创 2015-06-03 01:31:45 · 692 阅读 · 0 评论 -
LeetCode解题报告--Roman to Integer
题目:罗马数字转为阿拉伯数字 Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999. 分析:题意:将给定的罗马数字转为阿拉伯数字 从前往后遍历罗马数字,如果某个数比前一个数小,则把该数加入到结果中; 反之,则在结果中两次减去前一个数并加原创 2015-08-13 19:46:54 · 965 阅读 · 0 评论 -
Java连接mysql七步!
Java如何连接mysql数据库,本归纳总结7步,即可顺利达成! 前提你的java项目已经导入下载好的jdbc驱动包!! 没有可以此处下载 1> 声明驱动变量 driver://Database Driverprivate static final String driver = "com.mysql.jdbc.Driver";2> 声明mysql连接地址url://URL address原创 2015-05-27 16:11:25 · 3188 阅读 · 0 评论 -
java 小项目:简单扑克牌游戏
项目要求 1>新建两个玩家 2>创建扑克牌,洗牌 3>发牌,每个玩家没人拿两张,比较大小 4>比较规则:两个玩家选出各种最大的牌,进行游戏,先比较点数大小,点数大的获胜,点数相同则比较花色:黑桃>红心>梅花>方块 5>输出要求:结束时分别输出玩家的手牌情况。运行结果截图: Java源码: Card类:package com.card;import java.util.ArrayLis原创 2015-05-27 05:44:38 · 7712 阅读 · 1 评论 -
Java中equals()与hashCode()方法详解
Java中equals()与hashCode()方法详解一.equals()方法详解 equals()方法在object类中定义如下: Java代码 收藏代码 public boolean equals(Object obj) { return (this == obj); } 很明显是对两个对象的地址值进行的比较(即比较引用是否相同)。但是我们知转载 2015-05-30 17:42:25 · 748 阅读 · 0 评论 -
Java 基于数组自定义实现容量可变向量Vector
数组容量N固定。一方面,在向量规模很小时,预留这么多的空间实属浪费;反过来,当向量规模超过N时,即使系统有足够的空间资源,也会因 意外错而崩溃。幸好,有一个简易的方法可以克服这一缺陷。我们希望向量能够根据实际需要,动态地扩充数组的容量。当然, Java 语言本身并不支持这一功能,与多数程序语言一样, Java 中数组的容量都是固定的。我们的策略是,一旦数组空间溢出(即size≥CAPACITY),原创 2015-06-21 06:20:07 · 1418 阅读 · 0 评论 -
Java 基于数组自定义实现容量不可变向量Vector
背景:假定集合 S 由 n 个元素组成,它们按照线性次序存放,于是我们就可以直接访问其中的第一个元素、第二个元素、第三个元素……。也就是说,通过[0, n-1]之间的每一个整数,都可以直接访问到唯一的元素 e,而这个整数就等于 S 中位于 e 之前的元素个数⎯⎯在此,我们称之为该元素的秩( Rank)。不难看出,若元素 e 的秩为 r,则只要 e 的直接前驱(或直接后继)存在,其秩就是 r-1(或原创 2015-06-19 06:48:55 · 1149 阅读 · 0 评论 -
Java Semaphore 信号量
题目: 你的现有的银行有200美元 交易1 从银行取出100美元 交易2 往银行存20美元 两个操作同时进行不用信号量semaphore的java代码:package com.os;public class Withdraw implements Runnable{ private static int totalMoney = 200; public static int g原创 2015-06-07 23:49:46 · 627 阅读 · 0 评论 -
Java基础笔记 – Java中的Exception异常 自定义异常
public class Exception extends ThrowableException 类及其子类是 Throwable 的一种形式,它指出了合理的应用程序想要捕获的条件。public class RuntimeException extends ExceptionRuntimeException 是那些可能在 Java 虚拟机正常运行期间抛出的异常的超类。 可能在执行转载 2015-06-08 23:31:26 · 948 阅读 · 1 评论 -
基于数组实现Java 自定义Stack栈类及应用
栈是存放对象的一种特殊容器,在插入与删除对象时,这种结构遵循后进先出( Last-in-first-out,LIFO)的原则。java本身是有自带Stack类包,为了达到学习目的已经更好深入了解stack栈,自己动手自建java stack类是个很好的学习开始:自建Java Stack 类Stack 类:package com.stack;import java.util.ArrayList;im原创 2015-06-09 00:02:32 · 3532 阅读 · 1 评论 -
基于数组实现Java 自定义Queue队列及应用
Java 自定义队列Queue:队列的抽象数据类型就是一个容器,其中的对象排成一个序列,我们只能访问和取出排在最前端( Front)的对象,只能在队列的尾部( Rear)插入新对象。正是按照这一规则,才能保证最先被插入的对象首先被删除( FIFO)。java本身是有自带Queue类包,为了达到学习目的已经更好深入了解Queue队列,自己动手自建java Queue类是个很好的学习开始: 基于数组的原创 2015-06-10 00:08:25 · 5513 阅读 · 0 评论 -
Java 实现链表
链表相比数组操作更灵活,在空间效率方面比数组更有优势,虽然java中没有指针,但可以通过自定义类建立链表模型,进而实现链表。 分享下自己用java实现链表的过程: java代码: Node 类:package com.list.stack;/** * Setup Node class * @author gannyee * */public class Node { //D原创 2015-06-12 23:47:44 · 1686 阅读 · 1 评论 -
基于链表实现Java 自定义Queue队列
与栈一样,我们也可以借助单链表来实现队列ADT。同样地,出于效率方面的考虑,我们将以单链表的首(末)节点作为队列的首(末)节点⎯⎯这样,可以回避单链表在尾部进行删除操作时效率低下的缺陷。此外,还需要两个实例变量分别指示表的首、末节点。java代码如下:QueueList:package com.list.queue;import java.util.Arrays;import com.list.s原创 2015-06-14 00:32:00 · 2902 阅读 · 2 评论 -
基于链表实现Java 自定义Stack栈
接下来让我们看看,如何利用单链表结构来实现栈与队列。由于栈的操作只限于栈顶元素,而单链表只有对首元素才能在O(1)时间内完成插入和删除,故这里把单链表的首节点作为栈顶,其余元素依次排列。此外,为了保证getSize()方法也能够在O(1)时间内完成,还需借助一个实例变量来动态记录栈中元素的数目。具体的实现如 代码二.12 所示。Node类 Java代码见( Java 实现链表)StackLink 类原创 2015-06-12 23:57:14 · 1938 阅读 · 0 评论 -
LeetCode解题报告--Container With Most Water
题目:最大的盛水容器Given n non-negative integers a1, a2, …, an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Fi原创 2015-08-13 17:37:37 · 795 阅读 · 0 评论 -
LeetCode解题报告--Palindrome Number
题目:回文数字的判断 Determine whether an integer is a palindrome. Do this without extra space.Some hints: Could negative integers be palindromes? (ie, -1)If you are thinking of converting the integer to strin原创 2015-08-13 17:28:51 · 1492 阅读 · 0 评论 -
LeetCode解题报告--Combination Sum
题目: Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited n原创 2015-12-07 20:24:31 · 1069 阅读 · 1 评论