c++ code
文章平均质量分 59
chunxia75qin
这个作者很懒,什么都没留下…
展开
-
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原创 2013-08-08 16:51:03 · 514 阅读 · 0 评论 -
Sort Colors
题目:Given an array with n objects colored red, white or blue, sort them so that objects of the same color are adjacent, with the colors in the order red, white and blue.Here, we will use the inte原创 2013-05-23 16:21:04 · 393 阅读 · 0 评论 -
Minimum Window Substring
题目:Given a string S and a string T, find the minimum window in S which will contain all the characters in T in complexity O(n).For example,S = "ADOBECODEBANC"T = "ABC"Minimum window is "原创 2013-05-23 19:42:08 · 751 阅读 · 0 评论 -
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原创 2013-05-23 15:41:34 · 443 阅读 · 0 评论 -
Set Matrix Zeroes
题目:Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place.Follow up:Did you use extra space?A straight forward solution using O(mn) space is probably a原创 2013-05-23 11:13:56 · 913 阅读 · 0 评论 -
Edit Distance
题目:Given two words word1 and word2, find the minimum number of steps required to convertword1 to word2. (each operation is counted as 1 step.)You have the following 3 operations permitted on a原创 2013-05-22 18:37:09 · 513 阅读 · 0 评论 -
Simplify Path
题目:Given an absolute path for a file (Unix-style), simplify it.For example,path = "/home/", => "/home"path = "/a/./b/../../c/", => "/c"分析:此题主要需注意以下两点1、结果起码有一个"/"2、当有一个这样的形式/../,就代表少一个路原创 2013-05-22 17:07:44 · 703 阅读 · 0 评论 -
Climbing Stairs
题目:You are climbing a stair case. It takes n steps to reach to the top.Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top?代码如下:int climbStairs(in原创 2013-05-22 16:13:51 · 378 阅读 · 0 评论 -
Text Justification
题目:Given an array of words and a length L, format the text such that each line has exactlyL characters and is fully (left and right) justified.You should pack your words in a greedy approach; that原创 2013-05-21 18:25:24 · 777 阅读 · 0 评论 -
Sqrt(x)
题目:Implement int sqrt(int x).Compute and return the square root of x.代码如下: int sqrt(int x) { if(x double a=x,b=0,ErrorMargin=0.0000001; while(abs(a-b)>Error原创 2013-05-21 15:33:16 · 367 阅读 · 0 评论 -
利用归并排序求逆序对
归并排序原理:将一个规模为n的问题分解为n个已经排序的子问题(此时每个子问题只有一个数),然后两两进行合并就可以得到原问题的解。求逆序对事实上就是统计归并排序中位置交换的次数,简单起见,我们可以用一个全局变量m_Count来统计。代码如下:int m_Count=0;void merge(int* a,int begin,int temp,int end){原创 2013-05-07 12:47:53 · 838 阅读 · 0 评论 -
Valid Number
题目:Validate if a given string is numeric.Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => trueNote: It is intended for the problem statement to be ambi原创 2013-05-20 16:16:20 · 673 阅读 · 0 评论 -
Plus One
题目:Given a number represented as an array of digits, plus one to the number.eg: input [9,9] output [1,0,0]分析:此题比较简单,只要处理当末尾数是9的时进位以及当第一位为9时有可能要增加数组长度。代码如下: vector plusOne(ve原创 2013-05-20 18:22:33 · 455 阅读 · 0 评论 -
Add Binary
题目:Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".分析:可以根据二进制的加法原理解决此问题,要注意string与int之间的转换。代码如下: string addBinary(str原创 2013-05-19 16:50:37 · 715 阅读 · 0 评论 -
求 pow(x, n)
题目非常简单,但需要考虑的细节1) x = 0 或 n = 02) n 为正或负数利用递归,算法复杂度为O(log(n))代码如下:double pow1(double x, int n){ double result = 1; if(n==0)return 1; if(n==1)return x; if(原创 2013-05-11 15:12:45 · 641 阅读 · 0 评论 -
Combinations
题目:Given two integers n and k, return all possible combinations ofk numbers out of 1 ... n.For example,If n = 4 and k = 2, a solution is:[ [2,4], [3,4], [2,3], [1,2], [1,3], [1原创 2013-05-25 20:54:34 · 421 阅读 · 0 评论 -
Merge Intervals
题目:Given a collection of intervals, merge all overlapping intervals.For example,Given [1,3],[2,6],[8,10],[15,18],return [1,6],[8,10],[15,18].分析:先排序,这里我简单的选择插入排序,再合并,可以用贪心算法的思想。代码如下:原创 2013-05-15 18:58:51 · 413 阅读 · 0 评论 -
gray code
题目:The gray code is a binary numeral system where two successive values differ in only one bit.Given a non-negative integer n representing the total number of bits in the code, print the sequenc原创 2013-05-28 13:37:44 · 596 阅读 · 0 评论 -
螺旋矩阵
题目: Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.For example,Given the following matrix:[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8,原创 2013-05-14 15:54:10 · 437 阅读 · 0 评论 -
生成螺旋矩阵
题目:Given an integer n, generate a square matrix filled with elements from 1 ton2 in spiral order.For example,Given n = 3,You should return the following matrix:[ [ 1, 2, 3 ], [ 8, 9, 4原创 2013-05-14 16:22:21 · 779 阅读 · 0 评论 -
连续子向量的最大和
题目:Find the contiguous subarray within an array (containing at least one number) which has the largest sum.For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1]原创 2013-05-13 19:52:16 · 652 阅读 · 0 评论 -
Remove Duplicates from Sorted List
题目:代码如下:struct ListNode { int val; ListNode *next; ListNode(int x) : val(x), next(NULL) {} }; ListNode *deleteDuplicates(ListNode *head) { if(head==NULL)retu原创 2013-05-26 14:02:50 · 441 阅读 · 0 评论 -
Word Search
题目:Given a 2D board and a word, find if the word exists in the grid.The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or verti原创 2013-05-26 13:31:29 · 725 阅读 · 0 评论 -
Subsets
题目:Given a set of distinct integers, S, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For exam原创 2013-05-25 20:19:52 · 484 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
题目:Given a sorted linked list, delete all nodes that have duplicate numbers, leaving onlydistinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given原创 2013-05-26 14:56:31 · 866 阅读 · 0 评论 -
Remove Duplicates from Sorted Array II
题目:Follow up for "Remove Duplicates":What if duplicates are allowed at most twice?For example,Given sorted array A = [1,1,1,2,2,3],Your function should return length = 5, and A is now [1,1原创 2013-05-26 13:43:51 · 869 阅读 · 1 评论 -
n-皇后问题
题目如下:The n-queens puzzle is the problem of placing n queens on ann�n chessboard such that no two queens attack each other.Given an integer n, return all distinct solutions to the n-queens puzz原创 2013-05-12 16:38:40 · 895 阅读 · 0 评论 -
Maximal Rectangle
题目:Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area.代码如下:int maximalRectangle(vector > &matrix) { int l = matrix.si原创 2013-05-26 19:40:25 · 405 阅读 · 0 评论 -
Largest Rectangle in Histogram
题目:Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.Above is a histogram where widt原创 2013-05-26 16:19:07 · 688 阅读 · 0 评论 -
Merge two sorted linked lists
题目:Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.代码如下: struct ListNode { int v原创 2013-05-19 16:04:35 · 685 阅读 · 0 评论 -
Minimum Path Sum
题目:Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right whichminimizes the sum of all numbers along its path.Note: You can only move either down or ri原创 2013-05-19 15:36:37 · 403 阅读 · 0 评论 -
查找已排序的旋转后的数组中的数(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 re原创 2013-04-24 17:44:02 · 885 阅读 · 0 评论 -
找出最长的有效的Parentheses
题目如下:Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.For "(()", the longest valid parentheses substring is "()原创 2013-04-24 16:33:53 · 438 阅读 · 0 评论 -
交换链表中的相邻节点
LeetCode中的题: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 const原创 2013-04-24 11:33:22 · 931 阅读 · 0 评论 -
给两个字符串A,B,判断B是否出现在A中
题目:给两个字符串A,B,判断B是否出现在A中,如果出现,返回第一次出现的位置指针,否则,返回NULL分析:此题我暂时想到用O(M*N)的方法去解决,每一次均拿B去A中的对应的位置比较。代码如下:char *strStr(char *haystack, char *needle) { int length1=strlen(haystack);原创 2013-04-24 15:07:04 · 4604 阅读 · 0 评论 -
去掉数组中与给定元素相同的元素
题目如下:Given an array and a value, remove all instances of that value in place and return the new length.要求时间复杂度为O(N),空间复杂度为O(1).分析:此题跟我上一篇文章做的题非常类似,均非常简单。代码如下:int removeElement(int A[], int原创 2013-04-24 14:36:00 · 444 阅读 · 0 评论 -
合并K个已排序的链表
LeetCode上的题:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.分析:合并K个已排序的链表,实际上就是重复合并两个已排序的链表。代码如下: //Definition for singly-linked list. str原创 2013-04-24 10:42:29 · 561 阅读 · 0 评论 -
两个整数相除
题目要求:两个整数相除,不能使用乘法,除法,取模,取余运算。代码如下:int divide(int dividend, int divisor) { if(dividend==0||divisor==0)return 0; bool symbol=false; if((divisor>0&÷nd0))原创 2013-04-24 16:14:19 · 726 阅读 · 0 评论 -
去掉排序数组中重复的数字
题目:Given a sorted array, remove the duplicates in place such that each element appear onlyonce and return the new length.Do not allocate extra space for another array, you must do this in plac原创 2013-04-24 14:26:18 · 504 阅读 · 0 评论 -
字符串的全排列和组合算法
转自:http://blog.csdn.net/hackbuteer1/article/details/7462447全排列在笔试面试中很热门,因为它难度适中,既可以考察递归实现,又能进一步考察非递归的实现,便于区分出考生的水平。所以在百度和迅雷的校园招聘以及程序员和软件设计师的考试中都考到了,因此本文对全排列作下总结帮助大家更好的学习和理解。对本文有任何补充之处,欢迎大家指出。首先来转载 2013-04-23 16:45:16 · 549 阅读 · 0 评论