66. Plus One
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.
The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.
You may assume the integer does not contain any leading zero, except the number 0 itself.
Example 1:
1 | Input: [1,2,3] |
Example 2:
1 | Input: [4,3,2,1] |
1 | //case1 1011 1012 |
136. Single Number
Given a non-empty array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
Example 1:
1 | Input: [2,2,1] |
Example 2:
1 | Input: [4,1,2,1,2] |
1 | class Solution { |
🐘异或:相同为0,不同为1
389. Find The Difference
Given two strings s\ and t\ which consist of only lowercase letters.
String t\ is generated by random shuffling string s\ and then add one more letter at a random position.
Find the letter that was added in t\.
Example:
1 | Input: |
1 | //性质:4与6进行异或的结果,再与4进行异或,得到了6. |
性质(1):4与6进行异或的结果,再与4进行异或,得到了6.
191. Number of 1 Bits
Write a function that takes an unsigned integer and return the number of ‘1’ bits it has (also known as the Hamming weight).
Example 1:
1 | Input: 00000000000000000000000000001011 |
Example 2:
1 | Input: 00000000000000000000000010000000 |
Example 3:
1 | Input: 11111111111111111111111111111101 |
Note:
- Note that in some languages such as Java, there is no unsigned integer type. In this case, the input will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
- In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 3 above the input represents the signed integer
-3
.
Follow up:
If this function is called many times, how would you optimize it?
1 | class Solution { |
性质(2)n&(n-1):将n的二进制表示中的最低位为1的改成0(在这里注意重复操作)
231. Power of Two
Given an integer, write a function to determine if it is a power of two.
Example 1:
1 | Input: 1 |
Example 2:
1 | Input: 16 |
Example 3:
1 | Input: 218 |
1 | // 2 : 10 |
190. Reverse Bits
Reverse bits of a given 32 bits unsigned integer.
Example 1:
1 | Input: 00000010100101000001111010011100 |
Example 2:
1 | Input: 11111111111111111111111111111101 |
Note:
- Note that in some languages such as Java, there is no unsigned integer type. In this case, both input and output will be given as signed integer type and should not affect your implementation, as the internal binary representation of the integer is the same whether it is signed or unsigned.
- In Java, the compiler represents the signed integers using 2’s complement notation. Therefore, in Example 2 above the input represents the signed integer
-3
and the output represents the signed integer-1073741825
.
Follow up:
If this function is called many times, how would you optimize it?
1 | // 1011 右移变成0101 |
27. Remove Element
Given an array nums and a value val, 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 by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
Example 1:
1 | Given nums = [3,2,2,3], val = 3, |
Example 2:
1 | Given nums = [0,1,2,2,3,0,4,2], val = 2, |
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
1 | // nums is passed in by reference. (i.e., without making a copy) |
1 | //在List中是相等,在Array中是不等 |
26. Remove Duplicates from Sorted Array
Given a sorted array nums, 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 by modifying the input array in-place with O(1) extra memory.
Example 1:
1 | Given nums = [1,1,2], |
Example 2:
1 | Given nums = [0,0,1,1,1,2,2,3,3,4], |
Clarification:
Confused why the returned value is an integer but your answer is an array?
Note that the input array is passed in by reference, which means modification to the input array will be known to the caller as well.
Internally you can think of this:
1 | // nums is passed in by reference. (i.e., without making a copy) |
1 | class Solution { |
1 | * [80] 删除排序数组中的重复项 II |
1 | * [83] 删除排序链表中的重复元素 |
53. Maximum Subarray
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 | Input: [-2,1,-3,4,-1,2,1,-5,4], |
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.
1 | class Solution { |