Leetcode-Question-191: Number of 1 Bits

题目:
Number of 1 Bits
Difficulty: Easy

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 00000000000000000000000000001011, so the function should return 3.

解答:

或者

 

Leetcode-Question-100: Same Tree

题目:

Same Tree
Difficulty: Easy

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.

解答:

 

Leetcode-Question-26: Remove Duplicates from Sorted Array

题目:
Remove Duplicates from Sorted Array
Difficulty: Easy

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 constant memory.

For example,
Given input array nums = [1,1,2],

Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.

解答:快快快!注意题目中有提到说它对超出新长度之外剩下些什么不管不问。

一个直观但效率低的办法如下:

 

Leetcode-Question-7: Reversed Integer

题目:
Reverse Integer
Difficulty: Easy

Reverse digits of an integer.

Example1: x = 123, return 321
Example2: x = -123, return -321

#####click to show spoilers.#####

Have you thought about this?Here are some good questions to ask before coding. Bonus points for you if you have already thought through this!

If the integer’s last digit is 0, what should the output be? ie, cases such as 10, 100.

Did you notice that the reversed integer might overflow? Assume the input is a 32-bit integer, then the reverse of 1000000003 overflows. How should you handle such cases?

For the purpose of this problem, assume that your function returns 0 when the reversed integer overflows.

解答:

 

Leetcode-Question-6: ZigZag Conversion

题目:
ZigZag Conversion
Difficulty: Easy

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)


And then read line by line: "PAHNAPLSIIGYIR"Write the code that will take a string and make this conversion given a number of rows:

convert("PAYPALISHIRING", 3) should return "PAHNAPLSIIGYIR".

上面的方法简单直观,不过需要动态申请空间,浪费。
另一种方法就是找规律。

Updated 2017.10.02  两年前的今天,那也是一个国庆节假期。

 

Leetcode-Question-4: Median of Two Sorted Arrays

题目:
Median of Two Sorted Arrays
Difficulty: Hard

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)).

解答:

这里,我们的思路借鉴了http://blog.csdn.net/kenby/article/details/6833407这位仁兄的。

0_13172682232xrn但此题的中位数的概念为一般理解的中位数的概念,例如,给定数组[1,2,3,5],其中位数为2.5。

按照此逻辑,实现的代码如下。逻辑很简单,但处理起来很麻烦,要处理太多边界情况。

另一种很简洁的方法来自于http://blog.csdn.net/zxzxy1988/article/details/8587244,稍有改进。

 

Leetcode-Question-3: Longest Substring Without Repeating Characters

原文链接
https://leetcode.com/problems/longest-substring-without-repeating-characters/

题目描述
Longest Substring Without Repeating Characters
Difficulty: Medium

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. For “bbbbb” the longest substring is “b”, with the length of 1.

解答:
十一个月后

Updated 2017.10.09 Happy Birthday to ShoweryHe~

 

Leetcode-Question-2: Add Two Numbers

题目:

Add Two Numbers
Difficulty: Medium
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 linked list.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

解答:

缺陷: 没对内存分配不足情况做检查。

 

Leetcode-Question-1: Two Sum

题目:

Two Sum
Difficulty: Medium

Given an array of integers, find two numbers such that they add up to a specific target number.
The function twoSum should return indices of the two numbers such that they add up to the target, where index1 must be less than index2. Please note that your returned answers (both index1 and index2) are not zero-based.

You may assume that each input would have exactly one solution.

Input: numbers={2, 7, 11, 15}, target=9
Output: index1=1, index2=2

解答:

storage映射表中存放数组中元素到其索引的对应关系,索引为-1表示的是其“配对”元素的索引。

逻辑是这样的,如果映射表中不存在数组中的某元素,则将该元素及其“配对”元素放入。如果表中存在数组中某元素,但其配对元素的索引为-1,表明其配对元素未现身,这发生在同一个数字出现不止一次的情况下。如果表中存在数组中某元素,且其配对元素的索引不为-1,则表明其配对元素在之前已经现身,这时候返回这两个元素的索引即可。

代码中考虑到两种特殊情况:

1) target等于数组中某元素的2倍: 比如target等于8, 而数组中包含4的情况。

2)数组中有数字不止出现一次的情况,比如nums = [2, 3, 3, 5, 10], target = 8.

 

Leetcode: One Exercise A Day, American Dream Not Far Away

这个博客的副标题是S和S的世界。一个S说,三年后要去Stanford读博;另一个S就说,ta陪ta飞美国。前一个就说,你是说真的,还是只是随口说说?后一个说,我可以去美国找份工作啊,Google/Facebook什么的。

不过这后一个S心里可没底。ta面试编程题目训练得太少。未雨绸缪起见,ta想从今天开始刷题。坚持下来,ta想。

OK, 从现在开始。