leetcode&剑指offer
文章平均质量分 74
海阔天空sky1992
静 静
展开
-
Leetcode 105. Construct Binary Tree from Preorder and Ignorer Traversal(C++版)
Given preorder and inorder traversal of a tree, construct the binary tree.思路分析:一个前序遍历序列和一个中序遍历序列可以唯一确定一颗二叉树。设前序遍历序列存储在preorder,中序遍历序列存储在inorder。前序遍历序列的第一个数字preorder[0]就是根结点的值。然后可以查找该值在中序遍历原创 2017-03-27 17:06:36 · 382 阅读 · 0 评论 -
LeetCode 83. Remove Duplicates from Sorted List(C++版)
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.思路分析:遍历链表,比较相邻结点pre原创 2017-03-25 12:05:23 · 726 阅读 · 0 评论 -
LeetCode 19. Remove Nth Node From End of List(C++版)
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, the原创 2017-03-25 10:47:25 · 341 阅读 · 0 评论 -
LeetCode 657. Judge Route Circle(C++版)
题目:Initially, there is a Robot at position (0, 0). Given a sequence of its moves, judge if this robot makes a circle, which means it moves back to the original place. The move sequence is repr原创 2017-08-18 12:24:18 · 1048 阅读 · 0 评论 -
LeetCode 606. Construct String from Binary Tree(C++版)
题目描述:You need to construct a string consists of parenthesis and integers from a binary tree with the preorder traversing way.The null node needs to be represented by empty parenthesis pair "()原创 2017-08-18 15:23:26 · 416 阅读 · 0 评论 -
LeetCode 557. Reverse Words in a String III(C++版)
题目描述:Given a string, you need to reverse the order of characters in each word within a sentence while still preserving whitespace and initial word order.Example 1:Input: "Let's take Leet原创 2017-08-18 17:57:39 · 527 阅读 · 0 评论 -
LeetCode 541. Reverse String II(C++版)
题目描述:Given a string and an integer k, you need to reverse the first k characters for every 2k characters counting from the start of the string. If there are less than k characters left, reverse al原创 2017-08-18 22:36:13 · 527 阅读 · 0 评论 -
剑指offer 面试题1:赋值运算符函数
题目描述:如下为类型CMyString的声明,请为该类型添加赋值运算符函数。题目分析:定义一个赋值运算符函数,应注意以下几点:1)返回值的类型应为该实力自身的引用(即*this)。返回引用,才可以连续赋值。2)传入的参数类型声明为const常量引用。理由有两点:一是在赋值运算符函数内不会改变传入的实例;二是如果传入的参数不是引用而是实例,那么从形参到实参会调用一次复制构造函数,原创 2017-08-18 14:55:08 · 701 阅读 · 0 评论 -
剑指offer 对公司上万名员工按照年龄排序
题目:公司有上万名员工,在O(n)时间内将员工按照年龄排序。可以使用常量大小辅助空间。思路分析:需要排序的数字为年龄,年龄在一个较小的范围内,我们可以设置为[0,99]。然后遍历所有的年龄,记录下每个年龄出现的次数。再依次将这些年龄赋值给要排序的年龄数组。比如25岁的有1000个,那么就需要给年龄数组赋值1000个25.代码:void sortAges(int ages[], in原创 2017-04-25 22:12:45 · 680 阅读 · 0 评论 -
剑指offer 面试题5:从尾到头打印链表
题目:输入一个单链表的头结点,从尾到头反过来打印出每个结点的值。链表结点定义如下:struct ListNode{ int m_nKey; ListNode * m_pNext;};思路分析:将链表从头到尾输出比较容易,遍历即可。但是从尾到头的话,我们可能会想到将链表结点的指针反转过来,改变链表的方向,然后再从头到尾打印。但是这会改变链表本来的结构,所以这时候我们需原创 2017-04-24 23:08:33 · 441 阅读 · 0 评论 -
剑指offer 面试题4:替换空格
题目:请实现一个函数,把字符串中的每个空格替换成“%20”。例如:输入“we are happy.”,则“we%20are%20happy.”。题目由来:在网络编程中,如果URL参数中含有特殊字符,如空格、“#”等,可能导致服务器端无法获得正常的参数值。我们需要将这些特殊符号转换成服务器可以识别的字符。转换的规则是在%后面跟上ASCII码的两位十六进制的表示。比如空格的ASCII码是32,即十原创 2017-04-24 21:53:50 · 439 阅读 · 3 评论 -
剑指offer 面试题3:二维数组中查找
题目:在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序。请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数。思路分析:首先选取数组右上角的数字。比较该数字和要查找的数字:如果该数字等于要查找的数字,查找过程结束;如果该数字大于要查找的数字,剔除该数字所在列;如果该数字小于要查找的数字,剔除该数字所在的行。下面是原创 2017-04-24 17:33:53 · 566 阅读 · 0 评论 -
剑指offer 面试题14:调整数组顺序使奇数位于偶数前面(C++版)
题目:输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有的奇数位于数组的前半部分,所有偶数位于数组的后半部分。思路分析:可以维护两个指针i和j,i指向数组的第一个数字,它只向后移动;j指向数组的最后一个数字,它只向前移动。在两个指针相遇之前,第一个指针总是位于第二个指针的前面。如果第一个指针指向的数字为偶数,并且第二个指针指向的数字是奇数,就交换这两个数字。当两个指针相遇时,原创 2017-04-12 10:55:19 · 1262 阅读 · 0 评论 -
剑指offer 面试题13:在O(1)时间删除链表结点(C++版)
题目:给订单想链表的头指针和一个结点指针,定义一个函数在O(1)时间删除该结点。链表结点与函数的定义如下:原创 2017-04-11 21:49:42 · 954 阅读 · 0 评论 -
剑指offer 面试题35 第一个只出现一次的字符(C++版)
题目描述:在字符串中找出第一个只出现一次的字符。如输入“abaccdeff”,则输出为‘b’.思路分析:统计每个字符出现的次数。字符(char)是一个长度为8的数据类型,因此总共有256中可能,所以我们可以使用一个长度为256的数组,每个字母根据起ascii码值作为数组的下表,对应数组的值存储每个字符出现的次数。需要两次扫描字符串。第一次统计每个字符出现的次数。第二次查找字符对应的原创 2017-08-19 10:52:18 · 670 阅读 · 0 评论 -
剑指offer 面试题35扩展:从第一个字符串中删除第二个字符串中的所有字符 (C++版)
题目描述:例如,输入”They are students.”和”aeiou”,则删除之后的第一个字符串变成”Thy r stdnts.”。思路分析:总体来说,就是在第一字符中拿到一个字符,判断其是否在第二个字符串中,在的话,就删除该字符。考虑如下几个问题:1、如何在字符串中删除一个字符:字符串的内存是连续分配的,当我们删除其中一个字符时,就需要把后面所有的字符向前移动一原创 2017-08-19 12:56:35 · 2260 阅读 · 1 评论 -
LeetCode 551. Student Attendance Record I (C++版)
题目描述:You are given a string representing an attendance record for a student. The record only contains the following three characters:'A' : Absent. 'L' : Late.'P' : Present. A student could原创 2017-08-19 15:54:40 · 567 阅读 · 0 评论 -
剑指offer面试题8:旋转数组的最小数字
题目描述:把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转。输入一个递增排序的数组的一个旋转。输出旋转数组的最小元素。例如数组{3,4,5,1,2}是{1,2,3,4,5}的一个旋转,该数组的最小元素为1。思路分析:1、旋转数组的特点:1)旋转数组可以划分为两个排序的递增子数组,且前面的子数组的元素都大于或者等于后面子数组的元素;2)最小的元素刚好是两个子数组的原创 2017-08-22 10:55:27 · 529 阅读 · 0 评论 -
LeetCode 206. Reverse Linked List(C++版)
Reverse a singly linked list.反转单链表。/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */clas原创 2017-03-23 14:09:51 · 449 阅读 · 0 评论 -
LeetCode 141. Linked List Cycle(C++版)
Given a linked list, determine if it has a cycle in it.Follow up:Can you solve it without using extra space?思路分析:题目要求是判断链表是否有环,leetcode上链表的题都是没有头结点的。最常见的想法是遍历linked list,同时用个set进行记录遍历过原创 2017-03-24 20:06:24 · 951 阅读 · 0 评论 -
LeetCode 21. Merge Two Sorted Lists(C++版)
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.思路分析:遍历两个链表,每次取值小的作为新的链表的结点。需要注意的点:1)比如取了链表l1的p结点原创 2017-03-25 11:20:11 · 365 阅读 · 0 评论 -
LeetCode 100. Same Tree(C++版)
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.思路分析:假设两棵树原创 2017-03-27 21:02:55 · 444 阅读 · 0 评论 -
Leetcode 8. String to Integer (atoi)( C++版)
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 inpu原创 2017-04-07 18:24:35 · 465 阅读 · 0 评论 -
Leetcode 65. Valid Number( C++版)
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 ambiguo原创 2017-04-08 16:30:27 · 438 阅读 · 0 评论 -
LeetCode 1. Two Sum(C++版)
Given an array of integers, return indices of the two numbers such that they add up to a specific target.You may assume that each input would have exactly one solution, and you may not use the sam原创 2017-03-26 16:59:34 · 1239 阅读 · 0 评论 -
LeetCode 203. Remove Linked List Elements(C++版)
Remove all elements from a linked list of integers that have value val.ExampleGiven: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6Return: 1 --> 2 --> 3 --> 4 --> 5思路分析:遍历链表,记当前遍历的链表结点为p,原创 2017-03-23 14:28:53 · 324 阅读 · 0 评论 -
LeetCode 160. Intersection of Two Linked Lists(C++版)
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 ↘原创 2017-03-23 16:13:58 · 382 阅读 · 0 评论 -
LeetCode 136. Single Number ( C++版)
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 without using ext原创 2017-03-30 13:28:43 · 762 阅读 · 1 评论 -
LeetCode 202. Happy Number ( C++版)
Write an algorithm to determine if a number is "happy".A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares原创 2017-03-30 15:05:29 · 1078 阅读 · 0 评论 -
LeetCode 204. Count Primes(C++版)
Description:Count the number of prime numbers less than a non-negative number, n.思路分析:方法一:我们知道大于2的偶数肯定不是素数。所以遍历小于n的奇数,判断是否为素数,是,count++。class Solution {public: bool isPrime(int n){原创 2017-03-30 17:04:12 · 1256 阅读 · 0 评论 -
Leetcode 101. Symmetric Tree( C++版)
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree [1,2,2,3,4,4,3] is symmetric: 1 / \ 2 2 / \ / \3 4 4 3原创 2017-04-09 16:42:54 · 592 阅读 · 0 评论 -
Leetcode 102. Binary Tree Level Order Traversal( C++版)
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 2原创 2017-04-09 17:32:10 · 524 阅读 · 0 评论 -
Leetcode 144. Binary Tree Preorder Traversal( C++版)
Given a binary tree, return the preorder traversal of its nodes' values.For example:Given binary tree {1,#,2,3}, 1 \ 2 / 3return [1,2,3].Note: Recursive soluti原创 2017-04-09 21:03:30 · 302 阅读 · 0 评论 -
LeetCode 217. Contains Duplicate(C++版)
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原创 2017-03-31 11:16:32 · 1484 阅读 · 0 评论 -
LeetCode 205. Isomorphic Strings(C++版)
Given two strings s and t, determine if they are isomorphic.Two strings are isomorphic if the characters in s can be replaced to get t.All occurrences of a character must be replaced with anot原创 2017-03-31 10:54:17 · 1083 阅读 · 0 评论 -
LeetCode 148. Sort List(C++版)
Sort a linked list in O(n log n) time using constant space complexity.思路分析:对数组而言,能够有O(n lgn)时间复杂度的算法为,快速排序,堆排序,归并排序,三者的空间复杂度分别为O(1), O(N),O(N)快速排序最坏时间复杂度为O(n^2)。通常而言,也就是针对数组而言,归并排序的空间复杂度为O(N原创 2017-03-24 10:29:34 · 820 阅读 · 0 评论 -
LeetCode 142. Linked List Cycle II(C++版)
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?思路:原创 2017-03-24 20:11:26 · 493 阅读 · 0 评论 -
LeetCode 2. Add Two Numbers(C++版)
You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return i原创 2017-03-26 11:00:27 · 508 阅读 · 0 评论 -
剑指offer 面试题35扩展:删除字符串中所有重复出现的字符 (C++版)
题目描述:定义一个函数,删除字符串中所有重复出现的字符。例如输入“google”,删除重复的字符之后的结果是“gole”。思路分析:创建一个bool型数组作为哈希表,数组下标对应字符的ascii码,数组的值表示其下标对应的字符在字符串中是否已经出现过。扫描字符串,未出现过,则保留并将哈希表中对应值设置为已出现过。已出现过,则删除。删除字符时采用重构字符串的思路(详细请看我的上一篇博原创 2017-08-20 09:46:34 · 4609 阅读 · 1 评论