- 博客(11)
- 收藏
- 关注
原创 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.(合并两个有序链表,并返回该新链表。新链表应该由最初的俩链表拼接而成)1.个人分析 与合并两个有序数组类似,只不过在这
2016-10-20 12:09:28 681
原创 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 ances
2016-10-17 12:10:06 310
原创 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 ugl
2016-10-15 13:16:45 655
原创 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个台阶的楼梯,每次能够上1或2个台阶,有多少种不同到达顶部的走法?
2016-10-15 13:13:40 719
原创 Best Time to Buy and Sell Stock(股票交易)
Say you have an array for which the ith element is the price of a given stock on day i.If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), desi
2016-10-15 13:10:05 483
原创 Number of 1 Bits(二进制中1的个数)
Write a function that takes an unsigned integer and returns the number of ’1’ bits it has (also known as the Hamming weight).(给定一个无符号整型数,实现一个函数返回该整数的二进制中1的个数)For example, the 32-bit integer ’11’ has bi
2016-10-12 11:25:00 527
原创 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-10-12 11:17:58 618
原创 Happy Number
Write an algorithm to determine if a number is “happy”.(判断一个数字是否是”happy”)A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum o
2016-10-12 11:12:44 265
原创 Power of Four
Given an integer (signed 32 bits), write a function to check whether it is a power of 4.(给定一个32位整型数,判断其是否是4的幂)1.个人分析 判断一个整型数是否是4的幂,必定是从2的幂中寻找符合条件的。1,2,4,8,16,32,64都是2的幂,其中1,4,16,64为4的幂,观察这些数的二进制位发现,它们
2016-10-07 12:35:15 361
原创 Power of Two
Given an integer, write a function to determine if it is a power of two.(给定一个整数,判断其是否是2的幂)1.个人分析 与判断是否是3的幂类似,普通解法就是不断地对输入的整数进行整除2,如果是2的幂则结果一定为1。2.个人解法bool isPowerOfTwo(int n) { if(n<1) re
2016-10-07 12:31:20 342
原创 Power of Three
Given an integer, write a function to determine if it is a power of three.(给定一个整数,判断其是否是3的幂)Follow up: Could you do it without using any loop / recursion?(能否不使用循环或递归实现?)1.个人分析 思路:最简单直观的做法是不断对3进行整除,如果
2016-10-07 12:26:27 353
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人