- 博客(43)
- 收藏
- 关注
转载 mysql增删改查的封装
package cn.zcw.util;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.List
2017-12-07 19:33:59 371
转载 JdbcUtil的封装
package cn.zcw.util;import java.io.IOException;import java.io.InputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql
2017-12-07 19:32:13 217
转载 Leetcode 201. Bitwise AND of Numbers Range
第二种方法速度更快1.public int rangeBitwiseAnd(int m, int n) {int i = 0;while (m != n) {m = m >> 1;n = n >> 1;i++;}return m }2.public int rangeBitwiseAnd(int m, int n)
2017-10-25 11:10:18 122
原创 Leetcode 199. Binary Tree Right Side View
1. public List rightSideView(TreeNode root) { List res=new ArrayList(); rightView(root,res,0); return res; } public void rightView(TreeNode cur, List res, int i) {
2017-10-25 09:25:06 158
转载 Leetcode 198. House Robber
这道题应用动态规划的思想求解,动态规划需要注意的一个地方是数组有时没必要,可以用常量代替。public int rob1(int[] nums){int[][] dp=new int[nums.length+1][2];for(int i=1;idp[i][0]=Math.max(dp[i-1][0], dp[i-1][1]);dp[i][1]=dp[i-1][0]+n
2017-10-24 10:41:04 160
转载 Leetcode 190. Reverse Bits
public int reverseBits(int n) {int result = 0;for (int i = 0; i result = result result+=n&1;n = n >> 1;}return result;}
2017-10-23 10:37:02 121
转载 Leetcode 189. Rotate Array
1.public void rotate(int[] nums, int k) {if(nums.lengthreturn;}int n=nums.length;int step=k%nums.length;int[] tmp=new int[step];for(int i=0;itmp[i]=nums[n-step+i];}for(int i=
2017-10-22 19:59:36 96
转载 Leetcode 188. Best Time to Buy and Sell Stock IV
public int maxProfit(int k, int[] prices) { int n = prices.length; if (n 1) return 0; //if k >= n/2, then you can make maximum number of transactions. if (k >= n/2) { int maxPro = 0; for
2017-10-21 21:33:02 143
转载 leetcode 187. Repeated DNA Sequences
class Solution { public List findRepeatedDnaSequences(String s) {List res = new ArrayList();Map map = new HashMap();if (s.length() return res;}int key = 0;for (int i = 0; i
2017-10-20 20:12:02 115
原创 179. Largest Number
public String largestNumber(int[] nums) {if(nums==null || nums.length==0){return "";}String[] strs=new String[nums.length];int count=0;for(int i=0;iif(nums[i]==0){count++;}strs[i
2017-10-12 20:10:54 117
转载 166. Fraction to Recurring Decimal
public String fractionToDecimal(int numerator, int denominator) {if(denominator==0){return "";}if(numerator==0){return "0";}String res="";if((numeratorres+="-";}long num=nume
2017-10-06 10:20:14 111
转载 leetcode 154.Find Minimum in Rotated Sorted Array II
public int findMin(int[] num) { 3 if (num == null || num.length == 0) { 4 return 0; 5 } 6 7 int len = num.length; 8 if (len == 1) { 9
2017-09-27 11:38:55 100
原创 python爬取图片
import urllib.requestimport redef getHtml(url): page = urllib.request.urlopen(url) html = page.read() return htmldef getImg(html): html=html.decode('utf-8')
2017-09-23 10:56:52 313
原创 leetcode 151. Reverse Words in a String
本题难度不大,核心在于对于小细节的处理,字符串的预处理很重要public class Solution { public void reverseArray(String[] words) {int i = 0, n = words.length - 1;while (n > 2 * i) {String x = words[i];words[i]
2017-09-23 10:23:28 112
原创 python生成随机图片验证码
from PIL import Image,ImageDraw,ImageFont,ImageFilterimport randomimport stringchars=string.digits+string.ascii_letterschars=random.sample(chars,4)print(chars)def rndColor(): #产生随机颜色
2017-09-19 11:19:26 1950
原创 链表的归并排序算法
public static ListNode mergeTwoList(ListNode headA,ListNode headB){ListNode rehead=new ListNode(0);ListNode pre=rehead;while(headA!=null && headB!=null){if(headA.valpre.next=headA;headA=he
2017-09-19 10:14:43 330
转载 python统计代码空行与注释
#_*_coding:utf-8_*_# 统计代码行数, 空行, 注释.import osdef code_lines_count(path): code_lines = 0 comm_lines = 0 space_lines = 0 for root,dirs,files in os.walk(path):
2017-09-18 10:27:39 580
转载 147. Insertion Sort List
public ListNode insertionSortList(ListNode head) {if(head==null){return null;}if(head.next==null){return head;}ListNode rehead=new ListNode(-1);rehead.next=head;ListNode pre=head;L
2017-09-17 21:56:48 221
原创 LRU Cache leetcode
1.自己想的 public class LRUCache {public LinkedList used=new LinkedList();public Map map=new HashMap();public int len;public int capacity;public LRUCache(int capacity) {this.capacity = capa
2017-09-16 10:52:41 142
原创 143. Reorder List
本题比较难想,关键是要找到思路。思路是按照三步来, 1.找到中间节点,断开。 2.把后半截单链表 反转一下一下。 3.再合并两个单链表。public void reorderList(ListNode head) {if(head==null){return;}ListNode slow=head;ListNode fast=head;while(
2017-09-14 10:15:21 145
转载 142. Linked List Cycle II
设:链表头是X,环的第一个节点是Y,slow和fast第一次的交点是Z。各段的长度分别是a,b,c,如图所示。环的长度是L。slow和fast的速度分别是qs,qf。第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!)
2017-09-13 11:22:06 120
转载 leetcode 140. Word Break II
140. Word Break II public boolean wordBreakcheck(String s, List dict) {if (s == null || s.length() == 0)return true;boolean[] res = new boolean[s.length() + 1];res[0] = true;
2017-09-12 10:35:14 126
转载 139. Word Break
1.对于这个问题首先想到的是递归方法,关于遍历的问题,不一定非要通过字符串下标来遍历字符串,通过遍历字典中的字符串更简洁明了,但是一般来说递归总是会有超时问题,遇到这种问题尽量使用动态规划方法: public boolean wordBreak(String s, List wordDict) {if(wordDict.size()==0){return false;}if(
2017-09-11 09:54:35 130
转载 Leetcode 138. Copy List with Random Pointer
1.本道题的目标是对链表进行深度复制,首先第一种解法是利用map方便操作。旧结点为key,新节点为value。这么做的目的是为了第二遍扫描的时候我们按照这个哈希表把结点的随机指针接上。时间复杂度为O(n),空间复杂度为O(n)。 public RandomListNode copyRandomList(RandomListNode head) { if(head == null)
2017-09-10 10:11:31 111
转载 Leetcode 132. Palindrome Partitioning II
public int minCut(String s) { int min = 0;int len = s.length() ;boolean[][] matrix = new boolean[len][len];int[] cuts = new int[len + 1];if (s.length() == 0 || s == null) {r
2017-09-03 11:08:32 92
转载 Leetcode 120. Triangle
解法一:给出一个三角形状的整数list,从第一行向下移动,每次只能向下一行相邻的数移动,要求求出从第一行到最后一行所经过的数字的和最小的方法。这是一道动态规划的题目,第一种解法可以用一个二维数组存储从第一行到某行某个数的经过数的最小值,这是空间为o(n*n),对其中第i行(i>1)的第j个数(0 利用动态规划常规思想去解从上到下,但是有个问题就是由于是三角形形数据,二维数组不少空间都
2017-09-02 11:21:02 102
转载 Leetcode 131. Palindrome Partitioning
public void solve(char[][] board) {if(board==null || board.lengthreturn;}for(int i=0;ifill(board, 0, i);fill(board, board.length-1, i);}for(int i=0;ifill(board, i, 0);fill(board, i
2017-08-30 10:33:55 128
转载 128. Longest Consecutive Sequence
public int longestConsecutive(int[] nums) { if(nums==null || nums.length==0){ return 0; } HashSet set=new HashSet();int len=1;for(int e:nums){set.add(e)
2017-08-29 11:12:47 147
转载 leetcode 130. Surrounded Regions
public void solve(char[][] board) {if(board==null || board.lengthreturn;}for(int i=0;ifill(board, 0, i);fill(board, board.length-1, i);}for(int i=0;ifill(board, i, 0);fill(board, i
2017-08-29 11:10:37 139
原创 Leetcode Best Time to Buy and Sell Stock III
题目要求限定两次交易求最大收益,解决方法自然应该想到了二分法和动态规划public int maxProfit(int[] prices) {if(prices==null || prices.lengthreturn 0;}int n=prices.length;int[] preProfit=new int[n];int[] postProfit=new int[n
2017-08-21 11:36:12 115
原创 Leetcode 97Interleaving String
本题核心思想是动态规划,涉及此类问题尽量不要用递归。public boolean isInterleave(String s1, String s2, String s3) {// Note: The Solution object is instantiated only once and is reused by each test case. if (s
2017-08-08 21:24:56 127
原创 93. Restore IP Addresses
这道题核心思想是DFS求解,一个IP地址有四段,每段有1到3个字符,public List restoreIpAddresses(String s) {List res = new ArrayList();if (s == null || s.length() 12) {return res;}StringBuffer tmp = new String
2017-08-07 10:53:58 86
转载 Leetcode 91. Decode Ways
if(s==null || s.length()==0) { return 0; } if(s.charAt(0)=='0') { return 0; } int [] number = new int[s.length() + 1];
2017-08-05 10:26:07 131
原创 Scramble String 动态规划
动态规划问题,涉及到动态规划问题基本思想是尽量使用数组 public boolean isScramble(String s1, String s2) { if (s1.length() != s2.length()) {return false;}int n = s1.length();if (s1.equal
2017-07-30 11:06:37 230
原创 82. Remove Duplicates from Sorted List II
82. Remove Duplicates f rom Sorted List II三刷
2017-07-25 10:18:45 155
原创 关于Spring MVC的一个经验
今天在写一个JSON数据传输案例时,按照书上写的代码是死活跑不通,后来网上查了,是xml文件配置问题,只配置了org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter,却没有配置org.springframework.web.servlet.mvc.method.annotation.Req
2017-07-24 14:36:28 148
原创 leetcode经典组合类问题,采用dfs思想
public class Solution { public List> combine(int n, int k) { List> combSet=new ArrayList>(); List comb=new ArrayList(); if(n return combSet;
2017-07-22 09:51:11 182
原创 leetcode 72 edit distance
求两个字符串的编辑距离。核心思想依然是动态规划,但是递归的时间复杂度太大,尽量用数组解决这种问题。public int minDistance(String word1, String word2) { int len1=word1.length()+1; int len2=word2.length()+1; int
2017-07-17 10:41:59 148
原创 leetcode 70. Climbing Stairs
这个问题是比较典型的动态规划问题,到达第n个台阶时的走法是第n-1个台阶和n-2个台阶步数之和。 public int climbStairs(int n) { int[] res=new int[n+1]; res[0]=1; res[1]=1; for(int i=2;i res
2017-07-15 19:51:47 105
原创 67. Add Binary ershua二刷
public class Solution { public String addBinary(String a, String b) { if(a == null || a.isEmpty()) { return b; } if(b == null || b.isEmpty()) { retu
2017-07-11 11:09:33 216
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人