Anne

Anne.github.io


  • Home

  • About

  • Tags

  • Categories

  • Archives

237.Delete Node in a Linked List

Posted on 2018-05-27 | In LeetCode

LinkedList, Easy

Question

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 value 3, the linked list should become 1 -> 2 -> 4 after calling your function.

Read more »

191.Number of 1 Bits

Posted on 2018-05-23 | In LeetCode

Bit Manipulation, Easy

Question

Write a function that takes an unsigned integer and returns the number of ‘1’ bits it has (also known as the Hamming weight).

Example 1:

1
2
3
Input: 11
Output: 3
Explanation: Integer 11 has binary representation 00000000000000000000000000001011

Example 2:

1
2
3
Input: 128
Output: 1
Explanation: Integer 128 has binary representation 00000000000000000000000010000000
Read more »

136.Single Number

Posted on 2018-05-23
Read more »

693.Binary Number with Alternating Bits

Posted on 2018-05-23 | In LeetCode

Bit Manipulation, Easy

Question

Given a positive integer, check whether it has alternating bits: namely, if two adjacent bits will always have different values.

Example 1:

1
2
3
4
Input: 5
Output: True
Explanation:
The binary representation of 5 is: 101

Example 2:

1
2
3
4
Input: 7
Output: False
Explanation:
The binary representation of 7 is: 111.

Example 3:

1
2
3
4
Input: 11
Output: False
Explanation:
The binary representation of 11 is: 1011.

Example 4:

1
2
3
4
Input: 10
Output: True
Explanation:
The binary representation of 10 is: 1010.
Read more »

PAT-B-1002--写出这个数

Posted on 2018-05-22 | In PAT-solutions

Question

读入一个自然数n,计算其各位数字之和,用汉语拼音写出和的每一位数字。

输入格式:每个测试输入包含1个测试用例,即给出自然数n的值。这里保证n小于10^100^。

输出格式:在一行内输出n的各位数字之和的每一位,拼音数字间有1 空格,但一行中最后一个拼音数字后没有空格。

输入样例:

1
1234567890987654321123456789

输出样例:

1
yi san wu
Read more »

714. Best Time to Buy and Sell Stock with Transaction Fee

Posted on 2018-05-21 | In LeetCode

Question:

Your are given an array of integers prices, for which the i-th element is the price of a given stock on day i; and a non-negative integer fee representing a transaction fee.

You may complete as many transactions as you like, but you need to pay the transaction fee for each transaction. You may not buy more than 1 share of a stock at a time (ie. you must sell the stock share before you buy again.)

Return the maximum profit you can make.

Example 1:

1
2
3
4
Input: prices = [1, 3, 2, 8, 4, 9], fee = 2
Output: 8
Explanation: The maximum profit can be achieved by:
Buying at prices[0] = 1Selling at prices[3] = 8Buying at prices[4] = 4Selling at prices[5] = 9The total profit is ((8 - 1) - 2) + ((9 - 4) - 2) = 8.

Note:

0 < prices.length <= 50000.

0 < prices[i] < 50000.

0 <= fee < 50000.

Read more »

169. Majority Element

Posted on 2018-05-19 | In LeetCode

Bit Manipulation

Question

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 always exist in the array.

Example 1:

1
2
Input: [3,2,3]
Output: 3

Example 2:

1
2
Input: [2,2,1,1,1,2,2]
Output: 2
Read more »

206.Reversed Linked List

Posted on 2018-05-18 | In LeetCode

LinkedList, Easy

Question

Reverse a singly linked list.

Example:

1
2
Input: 1->2->3->4->5->NULL
Output: 5->4->3->2->1->NULL

Follow up:

A linked list can be reversed either iteratively or recursively. Could you implement both?

Read more »

53.Maximum Subarray

Posted on 2018-05-16 | In LeetCode

Divide And Conquer, Easy

Question

Given an integer array nums, find the contiguous subarray (containing at least one number) which has the largest sum and return its sum.

Example:

1
2
3
Input: [-2,1,-3,4,-1,2,1,-5,4],
Output: 6
Explanation: [4,-1,2,1] has the largest sum = 6.

Follow up:

If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.

Read more »

461.Hamming Distance

Posted on 2018-05-16 | In LeetCode

Bit Manipulation

Question

The Hamming distance between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:
0 ≤ x, y < 231.

Example:

1
2
3
4
5
6
7
8
9
10
Input: x = 1, y = 4
Output: 2
Explanation:
1 (0 0 0 1)
4 (0 1 0 0)
↑ ↑
The above arrows point to positions where the corresponding bits are different.
Read more »
1…91011…14
Anne_ZAJ

Anne_ZAJ

boom pow

134 posts
14 categories
17 tags
0%
© 2019 Anne_ZAJ
Powered by Hexo
|
Theme — NexT.Pisces v5.1.3