Algorithms & DataStructure
lviiii
这个作者很懒,什么都没留下…
展开
-
LeetCode Solutions : 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原创 2014-08-18 15:06:00 · 717 阅读 · 0 评论 -
LeetCode Solutions : Path Sum I & II
Path SumGiven a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum.For example:Given the below binary原创 2014-09-15 16:23:30 · 967 阅读 · 0 评论 -
LeetCode Solutions : 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原创 2014-11-14 11:41:19 · 989 阅读 · 0 评论 -
LeetCode Solutions : 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./** * Definition for singly-link原创 2014-11-13 19:17:17 · 841 阅读 · 0 评论 -
LeetCode Solutions : Remove Duplicates from Sorted Array I & II
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 anoth原创 2014-08-27 18:25:01 · 650 阅读 · 0 评论 -
LeetCode Solutions : Sort List
【算法思路】时间复杂度限制在O(n log n),我们可以第一时间想到常用的二路归并排序,快速排序和堆排序,其中快排和堆排只适用于线性表,即数组,故这道编程题毫无疑问用二路归并排序;【编程步骤】 * 1. 利用一个小技巧,可以设置慢行指针low和快行指针fast,把链表分成两部分来操作,即first和second链表 * 2. 递归排序first和second链表,即 first=sortList(head); second=sortList(second); * 3. 合并这两个链表,即:原创 2014-11-18 09:18:20 · 1492 阅读 · 1 评论 -
LeetCode Solutions : Remove Nth Node From End of List
【算法思路】这道题的关键在于找倒数第n个结点的前驱结点:可以设置慢行指针slow和快行指针fast,让快行指针fast先走n步,然后同步地移动慢行指针slow和快行指针fast,这样它们之间的距离永远相差n个结点,当fast指针移动最后一个结点的时候,slow指向倒数第n个结点;【编程步骤】 * 1. 处理特殊情况:如果被移除的结点为头结点,则移除,并返回第二个结点作为头结点; * 2. 如果为其他情况,则找到倒数第n个结点的前驱结点 pre; * 3. 移除倒数第n个结点,即 pre.next=s原创 2014-11-18 18:29:55 · 604 阅读 · 0 评论 -
LeetCode Solutions : Remove Duplicates from Sorted List II
【编程步骤】 * 1. 处理特殊情况:如果该链表为空或只有一个结点,说明此链表没有重复的元素,直接返回头指针head;即:if(head==null||head.next==null) return head; * 2. 设置伪头结点;即:ListNode pre=new ListNode(0); pre.next=head; head=pre; * 3. 令cur=pre.next,若不存在复本,cur依然为pre原创 2014-11-21 09:03:39 · 713 阅读 · 0 评论 -
LeetCode Solutions : Reorder List
Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→…You must do this in-place without altering the nodes' values.For example,Given {1,2,3,4}, reorder itto {1,4,2,3}.Considering the following steps: * 1. split such list原创 2014-11-17 10:01:06 · 951 阅读 · 0 评论 -
LeetCode Solutions : 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.【编程步骤】 * 1. 处理特殊情况:如果该链表为空或只有一个结点,说明此链表没有重复的元素,直接返回头指针head;即:if(head==null||原创 2014-11-19 19:10:08 · 579 阅读 · 0 评论 -
LeetCode Solutions : Find Minimum in Rotated Sorted Array
【算法思路】利用折半查找的思路去查找这个最小元素【编程步骤】 * 1. 如果数组num只有一个元素,则所求的最小的元素就是它了; * 2. 若left到right位置的元素严格递增,则最小的元素为num[left],如左图否则,如右图,利用折半查找,若left到mid递增有序,则最小元素必出现在右边部分:mid+1到right;若mid到right递增有序,则最小元素出现在左边部分:left到mid;while(left<right){ if(num[left]<num原创 2015-01-07 15:35:18 · 862 阅读 · 0 评论 -
数据结构:可以用求最短路径的方法思想求最长路径么?给出详细解答。。
数据结构:可以用求最短路径的方法思想求最长路径么?为什么呢?这里求解最短路径的通用方法有Dijkstra算法和Floyd-Warshall算法,Dijkstra算法不允许边的权值为负,也不允许有回路,而Floyd-Warshall算法可以允许边的权值为负,但不允许负值边构成回路,即可以求解有回路的图它们都有局限,这两种算法的思想可以用来求最长路径么??为什么 不可以?以下给出详细解答:1) 不可以,核心在于最短路问题是有最优子结构的,就是『最短路的子路径还是最短路』,而最长路径不存在这原创 2015-01-03 17:43:42 · 3131 阅读 · 1 评论 -
LeetCode Solutions : 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./** * Definition for singly-link原创 2014-09-03 18:54:30 · 758 阅读 · 0 评论 -
LeetCode Solutions : Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path.Note: You can only move either down or right原创 2014-08-11 13:55:04 · 609 阅读 · 0 评论 -
LeetCode Solutions:Binary Tree Inorder Traversal
Given a binary tree, return the inorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,3,2].Note: Recursive solution i原创 2014-08-08 09:20:43 · 633 阅读 · 0 评论 -
LeetCode Solutions : 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原创 2014-08-13 15:41:30 · 520 阅读 · 0 评论 -
LeetCode Solutions : N-Queens II
Follow up for N-Queens problem.Now, instead outputting board configurations, return the total number of distinct solutions.Java Solutions( Refer to blog http://blog.csdn.net/mbh_199原创 2014-08-25 17:43:51 · 858 阅读 · 0 评论 -
LeetCode Solutions : N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzzle.原创 2014-08-25 17:19:11 · 815 阅读 · 0 评论 -
LeetCode Solutions : 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原创 2014-08-26 17:11:28 · 1072 阅读 · 0 评论 -
LeetCode Solutions : 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). Fin原创 2014-08-12 16:23:48 · 507 阅读 · 0 评论 -
LeetCode Solutions : Sqrt(x)
Implement int sqrt(int x).Compute and return the square root of x.Java Solutions:public class Solution { public int sqrt(int x) { if(x<0) return -1; long high=x/2+1; l原创 2014-08-14 16:48:19 · 621 阅读 · 0 评论 -
LeetCode Solutions : Unique Paths I & II
Unique Paths A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram below).The robot can only move either down or right at any point in time. The robot is t原创 2014-09-11 17:30:19 · 860 阅读 · 0 评论 -
LeetCode Solutions : 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 fo原创 2014-08-15 14:05:15 · 508 阅读 · 0 评论 -
LeetCode Solutions : Remove Duplicates from Sorted List I & II
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, r原创 2014-09-01 17:18:42 · 732 阅读 · 0 评论 -
LeetCode Solutions : Combination Sum I & II
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 fro原创 2014-09-02 17:09:15 · 845 阅读 · 0 评论 -
VisuAlgo 学习笔记:Bitmask
原资源链接为:Bitmark其中各个模块代表的含义:- Set S: 初始值s- Set bit(j): s or 第j位- Check bit(j):s and 第j位- Toggle bit(j):s xor 第j位(相同的bit为0,不同则为1)- Clear bit(j位不为1): and 运算- Least原创 2014-12-04 17:26:32 · 4283 阅读 · 2 评论