文章列表
Insert Interval
- 博客分类:
- OJ_leetcode
各种条件真复杂,不仅是边界条件,而且还要分很多种情况讨论 而且 检查数组越界真心是个伤不起的活。。。
/**
* Definition for an interval.
* public class Interval {
* int start;
* int end;
* Interval() { start = 0; end = 0; }
* Interval(int s, int e) { start = s; end = e; }
* }
*/
public class Solution {
public Ar ...
感觉算法比数据结构要难啊,KMP折腾了我好久。。。而递归和动规神马的,只要想到了就比较简单(当然要注意边界条件之类),而算法复杂起来要想很久很细,唉。。。
唉 终于到了要记算法的时候了 KMP。。。还没写完 回去再写。。。
。。。
首先可以看这篇文章
http://www.programfan.com/blog/article.asp?id=15762
然后之前代码完全写错了,我觉得KMP的理解和记忆的核心在于两点:1、KMP中next数组的目的在于知道某个字母失配之后应该怎么根据模式串已匹配的字符找最近的最大匹配 2、next数组的生成过程和最后的模式匹配过程基本类似
public class Solution {
public String strStr(String haystack, String needle) {
...
Flatten太简单了 递归 一遍过 oh yeah = = 也没什么好骄傲的
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public void flatten(TreeNode root) {
// Start ...
唉 想了很久都没想出来 后来还是看了网上的答案 >_<
public class Solution {
public int firstMissingPositive(int[] A) {
// Start typing your Java solution below
// DO NOT write main() function
//put i to A[i-1], so the array looks like: 1, 2, 3, ...
for (int i = 0; i < A.lengt ...
Edit Distance
- 博客分类:
- OJ_leetcode
动规 就是递推。。。比较难想 然后数组长度设置比字符串长度多一,并做一定的初始化,都是为了方便边界条件的设置。。。。
public class Solution {
public int minDistance(String word1, String word2) {
// Start typing your Java solution below
// DO NOT write main() function
int length1 = word1.length();
int length2 = word2 ...
关于递归和动规= =
- 博客分类:
- 技术随笔
关于递归的一点笔记
递归的问题:1、重复计算,有必要的时候使用动规 2、边界条件 3、待新加入
自己实现除法 太太太恶心了。。。。
就是用位移代替了乘法,然后不断减去位移得到的乘数*除数
主要是测试数据还有一个Java.MinValue的问题 = = 取了abs之后它居然还是负数。。。我日
public class Solution {
public int divide(int dividend, int divisor) {
// Start typing your Java solution below
// DO NOT write main() function
int positive = 1;
...
动规,从前到后用T的每一个字符i,扫描S的每一个字符j。维护一个数组的更新,表示到j位置的子串有多少子序列和到i之前字串匹配的。然后在递推的时候,只需要看i是否和j匹配,然后匹配就说明有新的j可以和之前的子串组成符合条件的串。
开始边界条件
public class Solution {
public int numDistinct(String S, String T) {
// Start typing your Java solution below
// DO NOT write main() function
int ...
public class Solution {
public int numDecodings(String s) {
// Start typing your Java solution below
// DO NOT write main() function
if (s.length() == 0)
return s.length();
else if (s.startsWith("0"))
return 0;
el ...
Count and Say
- 博客分类:
- OJ_leetcode
public class Solution {
public String countAndSay(int n) {
// Start typing your Java solution below
// DO NOT write main() function
ArrayList<Character> current = new ArrayList<Character>();
current.add('1');
for (int i = 1; i != n; ++i) ...
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode sortedArrayToBST(int[] num) {
// Start typing your Java solution belo ...
本来以为是个简单的题目,直接二重循环,结果小测试过了,大测试超时了= = 1万个数据啊,必须优化了
public class Solution {
public int maxArea(int[] height) {
// Start typing your Java solution below
// DO NOT write main() function
int max = 0;
int area = 0;
for (int i = 0; i < height.length - 1 ...
不知道为什么错了。。。eclipse上明明是正确的啊 leetcode上就显示runtime error
/**
* Definition for binary tree
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode buildTree(int[] inorder, ...