数据结构与算法
搬砖小松鼠
贮藏坚果,以备过冬!
展开
-
leetcode刷题日记——Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].问题分析:题目目标就是找出给定排序数组中的连续段,分段输出。解决这个问题我们只需要找出分段点就可以了,原创 2015-12-24 13:02:22 · 228 阅读 · 0 评论 -
leetcode刷题日记——Remove Duplicates from Sorted Array II
Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array nums = [1,1,1,2,2,3],Your function should return length = 5, with the first fi原创 2015-12-25 14:50:30 · 348 阅读 · 0 评论 -
leetcode刷题日记——Remove Duplicates from Sorted Array
Given a sorted array, remove the duplicates in place such that each element appear only once and return the new length.Do not allocate extra space for another array, you must do this in place with原创 2015-12-25 10:47:12 · 209 阅读 · 0 评论 -
leetcode刷题日记——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?问题分析:判断一个单链表是否存在环,要求不使用额外的空间。刚开始对着题目发了半天呆,想着如果用循环,如果存在环就是死循环,后来想想是不是有什么策略来在循环过程中判断是否原创 2015-12-24 20:34:02 · 301 阅读 · 0 评论 -
leetcode刷题日记——Missing Number
iven an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.For example,Given nums = [0, 1, 3] return 2.Note:Your algorithm should r原创 2015-12-24 19:42:41 · 157 阅读 · 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 array.原创 2015-12-24 19:09:03 · 253 阅读 · 0 评论 -
leetcode刷题日记——Rotate Array
Rotate an array of n elements to the right by k steps.For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].Note:Try to come up as many solutions as you原创 2015-12-24 16:47:10 · 179 阅读 · 0 评论 -
leetcode刷题日记——Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 问题分析:判断是个数是否是回文数(正反看都一样的数字),显然负数不是回文数。解决办法就是一次判断首尾是否一样就可以了,当然 可以考虑递归,但是有迭代的方法可以解决,题目要求不能要求额外的空间。这里采用把数字转化为字符串,然后判断。实现代码如下:原创 2015-12-24 15:15:32 · 205 阅读 · 0 评论 -
leetcode刷题日记—— Length of Last Word
Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string.If the last word does not exist, return 0.Note: A word is d原创 2015-12-24 13:44:09 · 206 阅读 · 0 评论 -
leetcode刷题日记——Same Tree
Given two binary trees, write a function to check if they are equal or not.Two binary trees are considered equal if they are structurally identical and the nodes have the same value.问题分析:判断二个树是否原创 2015-12-25 15:05:54 · 276 阅读 · 0 评论 -
leetcode刷题日记——Power of Two
Given an integer, write a function to determine if it is a power of two.问题分析:题目目标明显,判断一个数是否为2的指数幂。首先如果一个数不能被2整除,肯定排除。另外能整除的数中又分为,最终一直除以2之后,中途不终止的倒最变成一的肯定是,具体实现代码如下:class Solution {public: boo原创 2015-12-25 15:14:24 · 238 阅读 · 0 评论 -
leetcode刷题日记——Add Two Numbers
You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a link原创 2015-12-25 18:10:27 · 444 阅读 · 0 评论 -
leetcode刷题日记—— Binary Tree Level Order Traversal II
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2015-12-31 15:42:16 · 233 阅读 · 0 评论 -
冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序java实现
1.冒泡排序、选择排序、插入排序的时间复杂度均为O(n2);2.选择排序:对于选择排序时间代价主要花费在比较上,即使是起始序列已经相对有序,仍然需要进行那么多次的比较,需要注意的就是要用一个变量来保存最小的那个元素的下标,实现代码如下//选择排序 public static void selection(int[] arr){ int index=0;原创 2016-03-19 16:41:29 · 321 阅读 · 0 评论 -
二叉搜索树的基本java实现
package zcj;/** * Created by admin on 2016/4/15. */public class BinarySearchTree { //定义一个二叉树的节点 private class TreeNode { public int val; public TreeNode left; publ原创 2016-04-15 15:59:46 · 292 阅读 · 0 评论 -
AVL树的java实现
package alogrithm;public class AVLTree { //使用一个嵌套类来表示平很二叉树的节点 private static class TreeNode{ public int val; public TreeNode left; public TreeNode right; public int height; //构造函数 p原创 2016-04-17 11:11:35 · 290 阅读 · 0 评论 -
KMP算法代码实现
package com.zcj.alogrithm;import org.omg.DynamicAny.DynAnyFactoryPackage.InconsistentTypeCode;//kmp算法//kmp算法的思想就是尽量减少重复匹配,尽可能的保存中间匹配结果//过程:对需要查找的子串进行预处理//前缀和后缀 ababd//最长匹配前缀和后缀:这里以上面d位置来讲,它的最长匹原创 2016-09-02 11:27:50 · 406 阅读 · 0 评论 -
Manacher算法实现求最长回文子串的长度
package com.zcj.alogrithm;public class Manacher { public static void main(String[] args){ String s="adsfddsfdddddff"; int maxLength=computeMaxPString(s); System.out.println(maxLen原创 2016-09-02 14:52:31 · 481 阅读 · 0 评论 -
leetcode刷题日记——Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.The brackets must close in the correct order, "()" and "()[]{}" are all valid原创 2015-12-26 12:06:46 · 256 阅读 · 0 评论 -
leetcode刷题日记——Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321问题分析:将一个数翻转过来,符号不变。题目看似简单,实际还要考虑越界的问题。所以用了long long int (不调试和参考别人例子想不到),其他部分就是一个是取得各位数字,另外一个技巧就是翻转的数字和原原创 2015-12-25 21:42:20 · 269 阅读 · 0 评论 -
leetcode刷题日记——Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For原创 2015-12-25 20:26:00 · 263 阅读 · 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 off as "one原创 2015-12-24 12:27:27 · 232 阅读 · 0 评论 -
leetcode刷题日记——Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins.For example, the following two linked lists:A: a1 → a2↘c1 → c2 → c3↗ B: b1 → b2 → b3beg原创 2015-12-23 22:27:45 · 235 阅读 · 0 评论 -
leetcode刷题日记——Merge Sorted Array
Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array.Note:You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold addit原创 2015-12-23 21:21:01 · 348 阅读 · 0 评论 -
leetcode刷题日记——Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling your原创 2015-12-17 11:04:24 · 373 阅读 · 0 评论 -
leetcode刷题日记——Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may ass原创 2015-12-17 12:54:55 · 308 阅读 · 0 评论 -
leetcode刷题日记——Invert Binary Tree
Invert a binary tree.4/ \2 7/ \ / \1 3 6 9to4/ \7 2/ \ / \9 6 3 1问题分析:依照题目所给的例子,可以得知题目所想达到的目标就是让二叉树的每个节点的左右孩子交换,这里可以采用递归的调用方法,先交换根节点的左右孩子,然后再交换其左右孩子的子节点,解决办法很简单,主要就是一个交换和自身的递原创 2015-12-17 13:44:51 · 266 阅读 · 0 评论 -
leetcode刷题日记——Excel Sheet Column Number
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example:A -> 1B -> 2C -> 3...Z -> 26AA ->原创 2015-12-17 19:21:48 · 542 阅读 · 0 评论 -
leetcode刷题日记——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.问题分析:题目要求就是对已经排序的链表进行去掉重复原创 2015-12-17 20:58:40 · 188 阅读 · 0 评论 -
leetcode刷题日记——Majority Element
Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times.You may assume that the array is non-empty and the majority element al原创 2015-12-17 19:57:04 · 254 阅读 · 0 评论 -
leetcode刷题日记——Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is原创 2015-12-17 19:34:51 · 272 阅读 · 0 评论 -
leetcode刷题日记——Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.Subscribe to see which companies a原创 2015-12-17 19:42:57 · 244 阅读 · 0 评论 -
leetcode刷题日记—— Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.Note: Do not modify the linked list.Follow up:Can you solve it without using extra space?问题分析原创 2015-12-31 12:21:12 · 240 阅读 · 0 评论 -
leetcode刷题日记——Find Minimum in Rotated Sorted Array II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Suppose a sorted array is rotated at some pivot unkno原创 2015-12-30 20:19:54 · 208 阅读 · 0 评论 -
leetcode刷题日记——Find Minimum in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).Find the minimum element.You may assume no duplicate exists in the原创 2015-12-30 20:05:51 · 221 阅读 · 0 评论 -
leetcode刷题日记—— Count Primes
Description:Count the number of prime numbers less than a non-negative number, n.问题分析:求出比n小的素数的个数。解决代码实现就是判断是否为是素数。实现代码如下:class Solution {public: int countPrimes(int n) { if(n==0) r原创 2015-12-30 19:39:51 · 258 阅读 · 0 评论 -
leetcode刷题日记—— Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed?Would this affect the run-time complexity? How and why?Write a function to determine if a given target is in the a原创 2015-12-30 18:55:28 · 231 阅读 · 0 评论 -
leetcode刷题日记——Search a 2D Matrix II
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted in ascending from left to right.Integers in ea原创 2015-12-30 18:44:28 · 219 阅读 · 0 评论 -
leetcode刷题日记——Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:Integers in each row are sorted from left to right.The first integer of each r原创 2015-12-30 17:08:13 · 239 阅读 · 0 评论 -
leetcode刷题日记——Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).You are given a target value to search. If found in the array return原创 2015-12-30 16:51:16 · 246 阅读 · 0 评论