Brush Inscription

Leetcode brush Inscription

1. The sum of two numbers

Violence Finger

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for (int i = 0; i < nums.length; i++) {
            for (int j = i + 1; j< nums.length; j++) {
                if (nums[i] == target - nums[j]) {
                    return new int[] { i , j };
                }
            }
        }
        throw new IllegalArgumentException("No two sum soltion");
    }   
}

IllegalArgumentException: Parameter error

2. The combination of the two tables

Here Insert Picture Description

Need to use left joinis available to meet the conditions (regardless of personwhether the address information, the following information will be output)

select FirstName,LastName,City,State 
from Person left join Address
on Person.PersonId = Address.PersonId

3. determining whether the unique string

Here Insert Picture Description

Violence Finger

class Solution {
    public boolean isUnique(String astr) {
        for (int i = 0; i < astr.length() - 1; i++) {
            if (astr.indexOf(astr.charAt(i) , i+1) != -1) {
                return false;
            }
        }
        return true;
    }
}

astr.length(): Returns the length of the string
astr.indexOf(a,b): a return position beginning position of the first occurrence of the string b
astr.charAt(i): Returns a string of characters at the i

4. The array duplicate numbers

Here Insert Picture Description
Seen from the question, if the array is not repeated, the array will be equal to the following table correspond to an array of values. So from the beginning to scan, encounters duplicate values ​​return.

class Solution {
    public int findRepeatNumber(int[] nums) {
        int temp;
        for (int i = 0; i < nums.length; i++) {
            while (nums[i] != i) {
                if (nums[i] == nums[nums[i]]) {
                    return nums[i];
                }
                temp = nums[i];
                nums[i] = nums[temp];
                nums[temp] = temp;
            }
        }
        return -1;
    }
}

nums.length: Returns the length of the array

The first question an IllegalArgumentException still do not understand why should this exception, want to help the next big brother said, thanks! ! !

Published an original article · won praise 1 · views 11

Guess you like

Origin blog.csdn.net/Wuyikkk/article/details/104562656