
C++算法
大工zay
这个作者很懒,什么都没留下…
展开
-
大数乘法
用字符串实现两个大数乘法#include <iostream>#include <string.h> using namespace std; void multiply(const char *a,const char *b); int main(){ //cout<<"hicjiajia"<<endl; string num1,num2; // 初始状态用string转载 2016-03-26 10:23:29 · 313 阅读 · 0 评论 -
3Sum, 3Sum Closest, 4 Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.Note:Elements in a triplet (a,b,c) must be in原创 2015-09-20 16:35:28 · 317 阅读 · 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 vertically neig原创 2015-11-11 21:22:29 · 386 阅读 · 0 评论 -
Spiral Matrix
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, 9 ]]You should retu原创 2015-10-25 11:11:56 · 285 阅读 · 0 评论 -
Maximum Subarray
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] has the原创 2015-10-25 10:27:38 · 280 阅读 · 0 评论 -
N-Queens and N-Queens II
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.Eac原创 2015-10-23 20:42:50 · 320 阅读 · 0 评论 -
二叉树遍历(前序、中序、后序)递归与非递归
二叉树的非递归遍历 二叉树的非递归遍历 二叉树是一种非常重要的数据结构,很多其它数据结构都是基于二叉树的基础演变而来的。对于二叉树,有前序、中序以及后序三种遍历方法。因为树的定义本身就是递归定义,因此采用递归的方法去实现树的三种遍历不仅容易理转载 2015-09-19 20:49:26 · 339 阅读 · 0 评论 -
Letter Combinations of a Phone Number
Given a digit string, return all possible letter combinations that the number could represent.A mapping of digit to letters (just like on the telephone buttons) is given below.Input:Digit string "23"原创 2015-09-22 09:37:10 · 236 阅读 · 0 评论 -
Pow(x, n)
Implement pow(x, n).实现一个浮点数幂运算的问题。这里不需要考虑越界的问题,指数是int,也不需要处理分数指数的问题。为了减小运行时间,我们采取折半相乘的方案,用递归来实现。用枚举的方法也可以实现,但是代码会比较长。class Solution {public: double myPow(double x, int n) { if(x==0) //底数为原创 2015-10-22 10:55:48 · 279 阅读 · 0 评论 -
Combinations
Given two integers n and k, return all possible combinations of k 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,4],]直接能想到的还是通过递归来原创 2015-11-06 13:03:21 · 313 阅读 · 0 评论 -
从尾到头打印链表
输入一个链表,从尾到头打印链表每个节点的值。可以用库函数解题。每次在vector的头部插入即可,没什么技术含量。代码如下:class Solution {public: vector<int> printListFromTailToHead(struct ListNode* head) { vector<int> result; while(head!=N原创 2015-09-18 10:07:41 · 249 阅读 · 0 评论 -
几个数组问题,持续更新
1.数组分段求最长给定一个长度为N(N>1)的整型数组A,可以将A划分成左右两个部分,左部分A[0..K],右部分A[K+1..N-1],K可以取值的范围是[0,N-2]。求这么多划分方案中,左部分中的最大值减去右部分最大值的绝对值,最大是多少?给定整数数组A和数组的大小n,请返回题目所求的答案。测试样例:[2,7,3,1,1],5返回:6总的思路还是依次变换k的值进行验证。我们知道,数组原创 2015-09-17 21:13:13 · 384 阅读 · 0 评论 -
Divide Two Integers
Divide two integers without using multiplication, division and mod operator.If it is overflow, return INT_MAX.乘法实际上也是加法的重复操作,而除法则是乘法的逆操作。因此,在不用乘法,除法和模运算的情况下,我们用加法来代替除法操作。class Solution {public: i原创 2015-10-12 10:35:48 · 334 阅读 · 0 评论 -
Unique Paths and Unique Paths II
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 trying to reach the botto原创 2015-10-28 09:47:52 · 244 阅读 · 0 评论 -
Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.If such arrangement is not possible, it must rearrange it as the lowest possible orde原创 2015-10-13 15:27:43 · 387 阅读 · 0 评论 -
Recover Binary Search Tree
Two elements of a binary search tree (BST) are swapped by mistake.Recover the tree without changing its structure.Note:A solution using O(n) space is pretty straight forward. Could you devise a consta原创 2015-12-03 09:49:51 · 395 阅读 · 0 评论 -
Unique Binary Search Trees
Given n, how many structurally unique BST's (binary search trees) that store values 1...n?For example,Given n = 3, there are a total of 5 unique BST's. 1 3 3 2 1 \ /原创 2015-11-30 10:00:47 · 391 阅读 · 0 评论 -
Restore IP Addresses
Given a string containing only digits, restore it by returning all possible valid IP address combinations.For example:Given "25525511135",return ["255.255.11.135", "255.255.111.35"]. (Order does not m原创 2015-11-28 16:16:42 · 330 阅读 · 0 评论 -
Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping:'A' -> 1'B' -> 2...'Z' -> 26Given an encoded message containing digits, determine the total number of w原创 2015-11-24 10:58:14 · 301 阅读 · 0 评论 -
Subsets and Subsets II
Given a set of distinct integers, nums, return all possible subsets.Note:Elements in a subset must be in non-descending order.The solution set must not contain duplicate subsets.For example,If nums原创 2015-11-11 20:37:48 · 333 阅读 · 0 评论 -
Find the Duplicate Number
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, fin转载 2015-11-23 10:39:18 · 390 阅读 · 0 评论 -
Valid Sudoku and Sudoku Solver
Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.The Sudoku board could be partially filled, where empty cells are filled with the character '.'.A partially filled sudoku which原创 2015-10-15 10:04:27 · 246 阅读 · 0 评论 -
Remove Duplicates from Sorted List II
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list.For example,Given 1->2->3->3->4->4->5, return 1->2->5.Given 1->1->1->2->原创 2015-11-12 11:12:53 · 299 阅读 · 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 sequence of gray转载 2015-11-16 10:26:01 · 340 阅读 · 0 评论 -
Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes greater than or equal to x.You should preserve the original relative order of the nodes in each of the原创 2015-11-15 21:53:20 · 298 阅读 · 0 评论 -
Permutation Sequence
The set [1,2,3,…,n] contains a total of n! unique permutations.By listing and labeling all of the permutations in order,We get the following sequence (ie, for n = 3):"123""132""213""231""312""321转载 2015-10-27 10:42:32 · 260 阅读 · 0 评论 -
Permutations and Permutations II
Given a collection of numbers, return all possible permutations.For example,[1,2,3] have the following permutations:[1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], and [3,2,1].这道题目的要求是找出数组的全排列,并把所有的全排列原创 2015-10-20 10:59:05 · 415 阅读 · 0 评论 -
Merge Two Sorted Lists and Merge k Sorted 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.这个题的思路还是比较清晰的。两个链表都已经排好序,首先,选择两个链表中头结点较小的作为结果的头结点。然后,依次比较当前原创 2015-10-05 21:18:44 · 299 阅读 · 0 评论 -
Longest Palindromic Substring
Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.这个题的关键还是在运行时间,回文字符串的判断需要逐个原创 2015-09-05 09:40:21 · 197 阅读 · 0 评论 -
Median of Two Sorted Arrays
There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).本来以为是道挺简单的题,只是求两个数组的中值,大不了合成一原创 2015-08-31 21:20:47 · 292 阅读 · 0 评论 -
Longest Substring Without Repeating Characters
leetcode解题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 i原创 2015-08-30 20:40:58 · 349 阅读 · 0 评论 -
排列组合算法
1。最近一直在考虑从m个数里面取n个数的算法。最容易理解的就是递归,但是其效率,实在不能使用。一直找寻中,今日得果2。算法来源与互联网组合算法 本程序的思路是开一个数组,其下标表示1到m个数,数组元素的值为1表示其下标 代表的数被选中,为0则没选中。 首先初始化,将数组前n个元素置1,表示第一个组合为前n个数。 然后从左到右扫描数组元素值的“10”组合,找到第一个“10”组合后将其变为 “转载 2015-05-19 11:13:58 · 502 阅读 · 0 评论 -
B-和B+树
B树的定义是这样的,一棵m阶的B树满足下列条件:(1)每个结点至多有m棵子树;(2)除根结点外,其他每个非叶子结点至少有m/2棵子树;(3)若根结点不是叶子结点,则至少有两棵子树;(4)所有叶结点在同一层上。B树的叶结点可以看成一种外部结点,不包含任何信息;(5)所有的非叶子结点中包含的信息数据为:(n,p0,k1,p1,k2,P2,…,kj-1,Pj-1)其中,ki为关键字,且满足kiki+1;p原创 2015-05-19 10:09:21 · 413 阅读 · 0 评论 -
Single Number
Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note: Your algorithm should have a linear runtime complexity. Could you implement it witho转载 2015-06-01 20:59:36 · 302 阅读 · 0 评论 -
sum-root-to-leaf-numbers
Given a binary tree containing digits from0-9only, each root-to-leaf path could represent a number. An example is the root-to-leaf path1->2->3which represents the number123. Find the total sum of all转载 2015-05-11 09:30:51 · 564 阅读 · 0 评论 -
按照左右半区的方式重新组合单链表
给定一个单链表的头部节点head,链表长度为N。 如果N为偶数,那么前N/2个节点算作左半区,后N/2个节点算作右半区; 如果N为奇数,那么前N/2个节点算作左半区,后N/2+1个节点算作右半区; 左半区从左到右依次记为L1->L2->…,右半区从左到右依次记为R1->R2->…。请将单链表调整成L1->R1->L2->R2->…的样子。 例如: 1->2->3->4 调整后:1->3->2->4翻译 2015-04-24 09:08:40 · 426 阅读 · 0 评论 -
将路径数组变为统计数组
给定一个路径数组paths,表示一张图。 paths[i]==j代表城市i连向城市j,如果paths[i]==i表示i城市是首都,一张图里只会有一个首都,不会有分图且图中除了首都指向自己之外不会有环; 例如:paths={9,1,4,9,0,4,8,9,0,1} 由这个数组表示的图如下图所示。 城市1是首都所以距离为0;离首都距离为1的城市只有城市9;离首都距离为2的城市有城市0,3,7;离翻译 2015-04-24 09:13:08 · 502 阅读 · 0 评论 -
[编程题] 奇数位上都是奇数或者偶数位上都是偶数
给定一个长度不小于2的数组arr。 写一个函数调整arr,使arr中要么所有的偶数位上都是偶数,要么所有的奇数位上都是奇数上。 要求:如果数组长度为N,时间复杂度请达到O(N),额外空间复杂度请达到O(1),下标0,2,4,6…算作偶数位,下标1,3,5,7…算作奇数位,例如[1,2,3,4]调整为[2,1,4,3]即可class Solution {public: /** *原创 2015-04-13 09:45:22 · 649 阅读 · 0 评论 -
[编程题] 求正数数组的最小不可组成和
给定一个全是正数的数组arr,定义一下arr的最小不可组成和的概念: 1,arr的所有非空子集中,把每个子集内的所有元素加起来会出现很多的值,其中最小的记为min,最大的记为max; 2,在区间[min,max]上,如果有一些正数不可以被arr某一个子集相加得到,那么这些正数中最小的那个,就是arr的最小不可组成和; 3,在区间[min,max]上,如果所有的数都可以被arr的某一个子集相加得到,那转载 2015-04-13 10:40:23 · 668 阅读 · 0 评论 -
ZigZag Conversion
The string “PAYPALISHIRING” is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)P A H NA P L S I I GY原创 2015-09-05 10:47:19 · 215 阅读 · 0 评论