11. Rotate the smallest number in the array

Moving the beginning elements of an array to the end of the array is called array rotation. Take a rotation of an incrementally sorted array and print the smallest element of the rotation array. For example, the array [3,4,5,1,2] is a rotation of [1,2,3,4,5], whose minimum value is 1.

Example 1:

Input: [3,4,5, 2] Output: 1 Example 2:

Input: [2,2,2,0,1] output: 0

Java sort returns the first one, python uses min directly

java

class Solution {
    public int minArray(int[] numbers) {
        
        Arrays.sort(numbers);
        return numbers[0]; }}Copy the code

python

class Solution(object):
    def minArray(self, numbers):
        """
        :type numbers: List[int]
        :rtype: int"" "return min(numbers)
Copy the code

Offer 15. Number of 1s in binary

Implement a function that takes an integer and outputs the number of 1s in the binary representation of that number. For example, 9 in binary is 1001 with two bits of 1. Therefore, if the input is 9, the function outputs 2.

Example 1:

Input: 00000000000000000000000000001011 output: 3: input binary string in 00000000000000000000000000001011, a total of three for the ‘1’. Example 2:

Input: 00000000000000000000000010000000 output: 1: input binary string in 00000000000000000000000010000000, a total of a ‘1’. Example 3:

Input: 11111111111111111111111111111101 output: 31 explanation: input binary string in 11111111111111111111111111111101, a total of 31 to ‘1’.

Answer:

java

public class Solution {
    // you need to treat n as an unsigned value
    int count = 0;
    public int hammingWeight(int n) {
        for(int i = 0; i < 32; i++){
            if(((n >>> i) & 1) = =1){ count++; }}returncount; }}Copy the code

python

class Solution(object):
    def hammingWeight(self, n):
        """
        :type n: int
        :rtype: int
        """
        res = 0
        while n:
            res += n & 1
            n >>= 1
        return res
Copy the code

17. Print n digits from 1 to maximum

Enter the number n to print out the largest n decimal digits in sequence. For example, if you type 3, you print 1, 2, 3 up to 999.

Example 1:

Input: n = 1 Output: [1,2,3,4,5,6,7,8,9]

Description:

Return a list of integers instead of printing n as a positive integer 48,475 times of passes 61,574 times of submissions

Input n to represent 10 to the n minus a number

Math.pow(a)// Returns a double
Copy the code

Python is

math.pow# (), toodoubletypeCopy the code

java class Solution { public int[] printNumbers(int n) { int number = (int)Math.pow(10,n)-1; int[] res = new int[number]; int math = 1; for(int i=0; i

class Solution(object):
    def printNumbers(self, n):
        """
        :type n: int
        :rtype: List[int]
        """
        number = math.pow(10,n)- 1
        res = []
        for i in range(1.int(number)+1):
            res.append(i)
        return res
Copy the code

18. Remove nodes from the list

Given a head pointer to a one-way linked list and the value of a node to delete, define a function to delete that node.

Returns the head node of the deleted list.

Note: this question has been changed from the original question

Example 1:

Input: head = [4,5,1,9], val = 5 Output: [4,1,9] Example 2:

Input: head = [4,5,1,9], val = 1 Output: [4,5,9]

If so, change the next point of the current node to the next point to the node. If the first point is to be deleted, return p.ext

java

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }} * * /
class Solution {
    public ListNode deleteNode(ListNode head, int val) {
        ListNode p = head;
        if(p.val == val) return p.next;
        while(p.next! =null){if(p.next.val == val){
                p.next =p.next.next;
                return head;
            }
            else{ p = p.next; }}returnhead; }}Copy the code

python

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution(object):
    def deleteNode(self, head, val):
        """
        :type head: ListNode
        :type val: int
        :rtype: ListNode
        """
        p = head
        if p.val == val:
            return p.next
        while p.next :
            if p.next.val == val:
                
                p.next = p.next.next
                return head
            else:
                p = p.next
Copy the code

21. Reorder the array so that the odd number precedes the even number

Take an array of integers and implement a function to adjust the order of the numbers in the array so that all odd numbers are in the first half of the array and all even numbers are in the second half.

Example:

Input: nums = [1,2,3,4] output: [1,3,2,4] note: [3,1,2,4] is also one of the correct answers.

Tip:

1 <= nums.length <= 50000

1 <= nums[i] <= 10000

If the first pointer is odd and the second one is even, then the odd one will be swapped. If the first pointer is odd and the second one is odd, then the first one will be minus one. If the second one is even, then the second one will be minus one

java

class Solution {
    public int[] exchange(int[] nums) {
        int number = nums.length;
        int[] res = new int[number];
        int l = 0;
        int r = number- 1;
        for(int i=0; l<=r; i++){if(nums[i]%2= =0){
                res[r] = nums[i];
                r--;
            }else{ res[l] = nums[i]; l++; }}returnres; }}Copy the code

python

class Solution(object):
    def exchange(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        l = 0
        r = len(nums)- 1
        while l<=r:
            if nums[l]%2= =0 and nums[r]%2! =0:
                tmp = nums[l]
                nums[l] = nums[r]
                nums[r] = tmp
                l = l + 1
                r = r - 1 
            if nums[l]%2! =0 and nums[r]%2= =0:
                l = l + 1
                r = r - 1 
            if nums[l]%2= =0 and nums[r]%2= =0:
                r = r - 1 
            if nums[l]%2! =0 and nums[r]%2! =0:
                l = l + 1
        return nums
Copy the code

22. The k last node in the linked list

Input a linked list, output the last KTH node of the list. In order to conform to the convention of most people, this case counts from 1, i.e. the last node of the list is the last node from the last. For example, a linked list has six nodes, starting with the head node, whose values are 1, 2, 3, 4, 5, and 6. The third from last node of the list is the node with the value 4.

Example:

Given a linked list: 1->2->3->4->5, and k = 2.

Return to list 4->5.

When the pointer is quickly full, one goes k first, the other does not, and then goes together, knowing that the one that goes first is empty.

java

/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; }} * * /
class Solution {
    public ListNode getKthFromEnd(ListNode head, int k) {
        ListNode m = head;
        ListNode n = head;

        for(int i=0; i<k; i++){ m = m.next; }while(m! =null){ m = m.next; n = n.next; }returnn; }}Copy the code

python

# Definition for singly-linked list.
# class ListNode(object):
# def __init__(self, x):
# self.val = x
# self.next = None

class Solution(object):
    def getKthFromEnd(self, head, k):
        """
        :type head: ListNode
        :type k: int
        :rtype: ListNode
        """
        m = head
        n = head

        for i in range(k):
            m = m.next
        while m :
            m = m.next
            n = n.next
        return n
Copy the code