
Leetcode/剑指offer
xingyanxiao
硕士在读,关注图像处理,数据挖掘、机器学习算法、希望有其长的同时开阔眼界。
展开
-
Leetcode Problem.172—Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!.Note: Your solution should be in logarithmic time complexity. 分析:有人可能第一反应就是现求出N!,然后再根据求出的结果,最后得出N!的末尾有多少个0。但是转念一想,会不会溢出原创 2015-06-01 13:07:53 · 425 阅读 · 0 评论 -
Leetcode Problem.28—Implement strStr()
Implement strStr().Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.My C++ solution! int strStr(string haystack, string needle)原创 2015-06-03 15:18:20 · 272 阅读 · 0 评论 -
Leetcode Problem.191—Number of 1 Bits
Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight).For example, the 32-bit integer ’11' has binary representation 000000原创 2015-06-03 15:33:17 · 342 阅读 · 0 评论 -
Leetcode Problem.58—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原创 2015-06-03 15:18:55 · 284 阅读 · 0 评论 -
Leetcode Probem.169—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原创 2015-06-03 15:29:10 · 403 阅读 · 0 评论 -
Leetcode Problem.189—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 yo原创 2015-06-03 15:31:13 · 424 阅读 · 0 评论 -
Leetcode Problem.83—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.My C++ solution!/**原创 2015-06-03 15:23:03 · 300 阅读 · 0 评论 -
Leetcode Problem.202—Happy Number
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原创 2015-06-03 15:35:01 · 339 阅读 · 0 评论 -
Leetcode Problem.47—Permutations II C++实现
Given a collection of numbers that might contain duplicates, return all possible unique permutations.For example,[1,1,2] have the following unique permutations:[1,1,2], [1,2,1], and [2,1原创 2015-06-05 13:09:53 · 542 阅读 · 0 评论 -
Leetcode Problem.190—Reverse Bits C++实现
Reverse bits of a given 32 bits unsigned integer.For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as0011100101原创 2015-06-03 15:32:39 · 480 阅读 · 0 评论 -
Leetcode Problem.35—Search Insert Position C++实现
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-06-05 13:25:20 · 392 阅读 · 0 评论 -
Leetcode Problem.46—Permutations C++实现
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-06-05 12:59:32 · 1154 阅读 · 0 评论 -
Leetcode Problem.—Rotate List C++实现
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.我的C++代码!struct ListNode {原创 2015-06-11 11:06:07 · 435 阅读 · 0 评论 -
剑指offer——斐波那契数列相关问题总结
题目描述题目一:写一个函数,输入n,求斐波那契数列的第n项。斐波那契数列定义如下:原创 2015-07-25 15:20:15 · 1876 阅读 · 1 评论 -
剑指offer——链表相关问题总结
首先统一链表的数据结构为:struct ListNode { int val; struct ListNode *next; ListNode(int x) :val(x), next(NULL) {}};题目一:从尾到头打印链表:输入一个链表,从尾到头打印链表每个节点的值。分析:难点在于链表只有指向后继的指针,没有指向前驱的指针。转换思路,结合栈后原创 2015-07-26 19:07:21 · 1004 阅读 · 0 评论 -
笔试题:第一个只出现一次的字符
题目:在字符串中找出第一个只出现一次的字符,如输入”abaccdeff“,则输出‘b’分析:题目中要求第一个只出现一次的字符,那么就跟字符出现的次数有关。我们考虑如何统计字符出现的次数,然后找出第一个次数为1的那个字符。这里我们需要一个数据容器来保存字符出现次数,并且能够通过字符找出其相对应的次数。哈希表就是一种常用用的容器。我们可以定义哈希表的键值(Key)是字符的A原创 2015-08-11 20:56:09 · 1037 阅读 · 1 评论 -
删除字符串中多余的空格
面试题:给定字符串,删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个void RemoveExtraSpace(char* str){ bool keep_space = false; int new_str_end = 0; for (int i = 0; str[i]; ++i) { //如果遍历得到的此字符不是空格,则将标志原创 2015-09-10 21:10:24 · 1978 阅读 · 0 评论 -
Leetcode Problem.206—Reverse Linked List
Reverse a singly linked list.Hint:A linked list can be reversed either iteratively or recursively. Could you implement both?My C++ solution!/** * Definition for singly-linked list. * struc原创 2015-06-03 15:39:05 · 577 阅读 · 0 评论 -
Leetcode Problem.119—Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle.For example, given k = 3,Return [1,3,3,1].Note:Could you optimize your algorithm to use only O(k) extra space?My C++ solut原创 2015-06-03 15:26:16 · 476 阅读 · 0 评论 -
Leetcode Problem.8—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 ca原创 2015-06-01 23:42:47 · 267 阅读 · 0 评论 -
Leetcode Problem.9—Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.My C++ solution!int reverse(int integer){ if(integer==0x80000000) return 0; long long temp=0; int len=0; bool flag=原创 2015-06-01 23:46:27 · 328 阅读 · 0 评论 -
Leetcode Problem.20—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 va原创 2015-06-01 23:50:10 · 305 阅读 · 0 评论 -
Leetcode Problem.27—Remove Element
Given an array and a value, remove all instances of that value in place and return the new length.The order of elements can be changed. It doesn't matter what you leave beyond the new length.M原创 2015-06-01 23:56:18 · 306 阅读 · 0 评论 -
Leetcode Probelm.3 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. Fo原创 2015-06-01 23:38:45 · 311 阅读 · 0 评论 -
Leetcode Problem.19—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, the原创 2015-06-01 23:48:30 · 265 阅读 · 0 评论 -
Leetcode Problem.26—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-06-01 23:52:49 · 268 阅读 · 0 评论 -
Leetcode Problem.21—Merge Two 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.My C++ solution!ListNode* mergeTwoLists(ListNode* l1,原创 2015-06-01 23:50:35 · 309 阅读 · 0 评论 -
Leetcode Problem.2—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-06-01 23:35:31 · 307 阅读 · 0 评论 -
Leetcode Problem.7—Reverse Integer
Reverse digits of an integer.Example1: x = 123, return 321Example2: x = -123, return -321Have you thought about this?Here are some good questions to ask before coding. Bonus points for you原创 2015-06-01 23:41:30 · 232 阅读 · 0 评论 -
Leetcode Problem.125—Valid Palindrome
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a原创 2015-06-03 15:29:34 · 344 阅读 · 0 评论 -
Leetcode Problem.203 —Remove Linked List Elements
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 --> 5My C++ solution!/*原创 2015-06-03 15:37:09 · 371 阅读 · 0 评论 -
Leetcode Problem.88 —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 add原创 2015-06-03 15:24:18 · 308 阅读 · 0 评论 -
Leetcode Problem.66—Plus One
Given a non-negative number represented as an array of digits, plus one to the number.The digits are stored such that the most significant digit is at the head of the list.My C++ solution!原创 2015-06-03 15:21:09 · 322 阅读 · 0 评论 -
Leetcode Problem.217 —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原创 2015-06-03 15:41:30 · 347 阅读 · 0 评论 -
Leetcode Problem.67—Add Binary
Given two binary strings, return their sum (also a binary string).For example,a = "11"b = "1"Return "100".My C++ solution!string addBinary(string a, string b) { int len_a原创 2015-06-03 15:21:55 · 443 阅读 · 0 评论 -
Leetcode Problem.118—Pascal's Triangle
Given numRows, generate the first numRows of Pascal's triangle.For example, given numRows = 5,Return[ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1]]My C++ solution!vect原创 2015-06-03 15:25:52 · 312 阅读 · 0 评论 -
面试题二叉树相关问题总结
二叉树中的面试题比较常见的题型大概有下面几个:创建一颗二叉树(先序,中序,后序)、遍历一颗二叉树(先序,中序,后序和层次遍历)、求二叉树中叶子节点的个数、求二叉树的高度、求二叉树中两个节点的最近公共祖先、打印和为某一值的全部路径、求某一节点是否在一个树中等等。再详细的说这些面试题之前,不妨先看一下几种常见的二叉树:完全二叉树:若二叉树的高度是h,除第h层之外,其他(1~h-1)层的原创 2015-09-10 19:34:01 · 881 阅读 · 0 评论