![](https://img-blog.csdnimg.cn/20201014180756754.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
LeetCode
NEUWYT
家人、朋友、美食,这些才是最重要的东西
展开
-
LeetCode Algorithms #290 <Word Pattern>
Given a pattern and a string str, find if str follows the same pattern.Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.原创 2015-10-11 06:41:55 · 372 阅读 · 0 评论 -
LeetCode Algorithms #326 <Power of Three>
Given an integer, write a function to determine if it is a power of three.Follow up:Could you do it without using any loop / recursion?思路:递归的很好做,就是在每次调用的时候看能不能被3整除,不能直接返回false,能的话继续除以3,直到最原创 2016-02-23 18:39:13 · 322 阅读 · 0 评论 -
LeetCode Algorithms #27 <Remove Element>
Given an array and a value, remove all instances of that value in place and return the new length.Do not allocate extra space for another array, you must do this in place with constant memory.原创 2016-04-01 18:39:36 · 312 阅读 · 0 评论 -
LeetCode Algorithms #66 <Remove Element>
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.思路:从最后一位开始+1,发现大原创 2016-04-01 19:36:43 · 351 阅读 · 0 评论 -
LeetCode Algorithms #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原创 2016-04-01 21:45:51 · 261 阅读 · 0 评论 -
LeetCode Algorithms #66 <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]]思路:虽然我希望再简单的题我也能记录下自己的思路原创 2016-04-01 22:12:04 · 311 阅读 · 0 评论 -
LeetCode Algorithms #231 <Power of Two>
Given an integer, write a function to determine if it is a power of two.思路:2这个数字很特殊,因为计算机使用的数制是二进制。所以可以尝试通过位操作来完成判断。2的幂的数字特点就是有且只有一个1,比如二进制1000,10这些。我发现1000-1 = 111和1000直接按位取反是一样的,但是C++语言的取反会把前面原创 2016-02-24 18:01:50 · 1066 阅读 · 0 评论 -
LeetCode Algorithms #206 <Reverse Linked List>
Reverse a singly linked list.思路:。。。解:/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * };原创 2016-02-02 19:46:01 · 394 阅读 · 0 评论 -
LeetCode Algorithms #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.思路:就和玩积木一样,这些基本的链表操作是没有任何难度的,小心些,不犯低级错误就可以一次ac。解:原创 2016-02-24 18:38:56 · 409 阅读 · 0 评论 -
LeetCode Algorithms #232 <Implement Queue using Stacks>
Implement the following operations of a queue using stacks.push(x) -- Push element x to the back of queue.pop() -- Removes the element from in front of queue.peek() -- Get the front element.empty(原创 2016-02-24 19:06:33 · 335 阅读 · 0 评论 -
LeetCode Algorithms #24 <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原创 2016-03-24 10:39:04 · 242 阅读 · 0 评论 -
LeetCode Algorithms #198 <House Robber>
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent house原创 2016-03-24 15:28:20 · 236 阅读 · 0 评论 -
LeetCode Algorithms #24 <Swap Nodes in Pairs>
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never diffe原创 2016-03-24 16:25:18 · 325 阅读 · 0 评论 -
LeetCode Algorithms #101 <Symmetric Tree>
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).For example, this binary tree is symmetric: 1 / \ 2 2 / \ / \3 4 4 3But the f原创 2016-03-24 16:50:38 · 233 阅读 · 0 评论 -
LeetCode Algorithms #107 <Binary Tree Level Order Traversal II>
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root).For example:Given binary tree {3,9,20,#,#,15,7},原创 2016-03-27 18:30:07 · 325 阅读 · 0 评论 -
LeetCode Algorithms #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.思路:a * (b*c) = a * b * c;只有2*5会产生0;所以这道题可以转化成 1,2,3....n中原创 2016-04-13 16:06:11 · 359 阅读 · 0 评论 -
LeetCode Algorithms #102 <Binary Tree Level Order Traversal>
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,#,#,15,7}, 3 / \ 9 20原创 2016-04-13 16:18:58 · 301 阅读 · 0 评论 -
LeetCode Algorithms #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原创 2016-02-16 21:49:15 · 274 阅读 · 0 评论 -
LeetCode Algorithms #263 <Ugly Number>
Write a program to check whether a given number is an ugly number.Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly si原创 2016-02-16 21:29:42 · 228 阅读 · 0 评论 -
LeetCode Algorithms #328 <Odd Even Linked List>
Given a singly linked list, group all odd nodes together followed by the even nodes. Please note here we are talking about the node number and not the value in the nodes.You should try to do it in原创 2016-02-02 12:51:21 · 298 阅读 · 0 评论 -
LeetCode Algorithms #258 <Add Digits>
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.For example:Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has on原创 2015-10-07 08:02:29 · 363 阅读 · 0 评论 -
LeetCode Algorithms #104 <Maximum Depth of Binary Tree>
Given a binary tree, find its maximum depth.The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.思路:简单的递归应用。解:/** * De原创 2015-10-11 06:53:57 · 387 阅读 · 0 评论 -
LeetCode Algorithms #237 <Delete Node in a Linked List>
Write a function to delete a node (except the tail) in a singly linked list, given only access to that node.Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with val原创 2015-10-11 07:06:27 · 384 阅读 · 0 评论 -
LeetCode Algorithms #100 <Same Tree>
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.思路:略原创 2015-10-11 07:36:56 · 335 阅读 · 0 评论 -
LeetCode Algorithms #283 <Move Zeroes>
Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.For example, given nums = [0, 1, 0, 3, 12], after calling you原创 2015-10-11 08:12:50 · 446 阅读 · 0 评论 -
LeetCode Algorithms #226 <Invert Binary Tree>
invert a binary tree. 4 / \ 2 7 / \ / \1 3 6 9to 4 / \ 7 2 / \ / \9 6 3 1思路:简单的递归调用,不知道Homebrew的作者为什么没做出来。解:/** * Definition for a bina原创 2015-10-11 08:33:25 · 322 阅读 · 0 评论 -
LeetCode Algorithms #13 <Roman to Integer>
Given a roman numeral, convert it to an integer.Input is guaranteed to be within the range from 1 to 3999.思路:(摘自互动百科)表示数的基本方法:除I、X、C位于大数后作为加数,位于大数前作为减数外,一般把若干罗马基本数字写在一起,它表示的数字等原创 2016-02-12 22:23:43 · 219 阅读 · 0 评论 -
LeetCode Algorithms #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原创 2016-02-13 01:26:14 · 360 阅读 · 0 评论 -
LeetCode Algorithms #70 <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?思路:n个阶梯的方法相当于n-1个阶梯迈一步,和原创 2016-02-13 01:45:45 · 382 阅读 · 0 评论 -
LeetCode Algorithms #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.思路:一个指针向后遍历,发现自己的下一原创 2016-02-13 02:07:04 · 283 阅读 · 0 评论 -
LeetCode Algorithms #292 <Nim Game>
You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the原创 2016-01-31 16:09:41 · 270 阅读 · 0 评论 -
LeetCode Algorithms #171 <Excel Sheet Column Number>
Related to question Excel Sheet Column TitleGiven a column title as appear in an Excel sheet, return its corresponding column number.For example: A -> 1 B -> 2 C -> 3 ...原创 2016-01-31 17:27:00 · 218 阅读 · 0 评论 -
LeetCode Algorithms #242 <Valid Anagram>
Given two strings s and t, write a function to determine if t is an anagram of s.For example,s = "anagram", t = "nagaram", return true.s = "rat", t = "car", return false.Note:You may a原创 2016-01-31 17:10:31 · 233 阅读 · 0 评论 -
LeetCode Algorithms #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原创 2016-01-31 17:38:44 · 239 阅读 · 0 评论 -
LeetCode Algorithms #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原创 2016-01-31 17:59:49 · 278 阅读 · 0 评论 -
LeetCode Algorithms #235 <Lowest Common Ancestor of a Binary Search Tree>
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BST.According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined betw原创 2016-02-02 00:16:13 · 234 阅读 · 0 评论 -
LeetCode Algorithms #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?思路:因为只返回结果原创 2016-04-14 16:04:55 · 331 阅读 · 0 评论