1. Given an integer array nums and an integer scalar target, find the two integers in the array that are the target and return their array subscripts.
You can assume that each input corresponds to only one answer. However, the same element in the array cannot be repeated in the answer.
You can return the answers in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Interpretation: Because nums[0] + nums[1] == 9, returns [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
class Solution { public int[] twoSum(int[] nums, int target) { int [] result=new int[2]; for(int i=0;i<=nums.length;i++){ for(int j=i+1;j<=nums.length-1;j++){ int sum=nums[j]+nums[i]; if(sum==target){ result[0]=i; result[1]=j; } } } return result; } }
2. Give you a 32-bit signed integer x and return the result of inverting the numeric part of X.
Returns 0 if the inverted integer exceeds the range of 32-bit signed integers [231, 231].
Suppose the environment does not allow 64-bit integers (signed or unsigned).
Example 1:
Input: x = 123
Output: 321
Example 2:
Input: x = -123
Output: -321
Example 3:
Input: x = 120
Output: 21
Example 4:
Input: x = 0
Output: 0
Tips:
-231 <= x <= 231 - 1
public static int reverse(int x) { try { boolean b = true; if (x < 0) { x = x * (-1); b = false; } String num = String.valueOf(x); String num2 = ""; for (int i = num.length() - 1; i >= 0; i--) { if (num.charAt(i) != 'a') { num2 += num.charAt(i); } } if (b) { return Integer.parseInt(num2); } else { return -Integer.parseInt(num2); } } catch (Exception e) { return 0; } }
3. You and your friends, two people play Nim games together:
There is a heap of stones on the table.
You take turns making your own rounds, you as the first.
Every turn, the person whose turn it is takes takes 1 - 3 stones.
The winner is the one who takes the last stone.
Assume that each of your steps is the best solution. Write a function to determine if you can win a game given the number of stones as n. If you can win, return true; otherwise, return false.
Example 1:
Input: n = 4
Output: false
Explanation: If there are 4 stones in the heap, you will never win the game.
Because no matter you take one, two or three stones, your friend always takes the last one.
Example 2:
Input: n = 1
Output:true
Example 3:
Input: n = 2
Output:true
Tips:
1 <= n <= 231 - 1
public boolean canWinNim(int n) { try { if(1<=n&&n<=3){ return true; } else { if(n%4==0){ return false; } return true; } }catch (Exception e){ return false; } }
4. Palindromes
Give you an integer x, if x is a palindrome integer, return true; otherwise, return false.
Palindrome number refers to an integer whose reading order is positive (left-to-right) and reverse (right-to-left). For example, 121 is palindrome and 123 is not.
Example 1:
Input: x = 121
Output:true
Example 2:
Input: x = -121
Output: false
Interpretation: Left-to-right reading, -121. Right-to-left reading, 121-. Therefore, it is not a palindrome number.
Example 3:
Input: x = 10
Output: false
Interpretation: read right to left, 01. So it is not a palindrome number.
Example 4:
Input: x = -101
Output: false
Tips:
-231 <= x <= 231 - 1
class Solution { public boolean isPalindrome(int x) { try { if (x < 0) { return false; } else { String num = String.valueOf(x); String num2 = ""; for (int i = num.length() - 1; i >= 0; i--) { if (num.charAt(i) != 'a') { num2 += num.charAt(i); } } if (Integer.parseInt(num2) == x) { return true; } return false; } }catch (Exception e){ return false; } } }
5. Addition of Two Numbers
Give you two non-empty linked lists representing two non-negative integers. Each number is stored in reverse order, and each node can only store one digit.
Add the two numbers together and return a list of sums in the same form.
You can assume that neither number starts with 0 except the number 0.
Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Interpretation: 342 + 465 = 807.
Example 2:
Input: l1 = [0], l2 = [0]
Output: [0]
Example 3:
Input: L1 = [9,9,9,9,9,9], L2 = [9,9,9]
Output: [8,9,9,9,0,0,1]
Tips:
Number of nodes in each list is within range [1, 100]
0 <= Node.val <= 9
Topic Data Guarantee List Number without Leading Zeros
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { List<Integer> listNodes1=new ArrayList<>(); while (true){ listNodes1.add(l1.val); if (l1.next==null){ break; } l1=l1.next; } List<Integer> listNodes2=new ArrayList<>(); while (true){ listNodes2.add(l2.val); if (l2.next==null){ break; } l2=l2.next; } String num1=""; String num2=""; int num=0; int redis=0; int redis2=0; SingleLinkedList singleLinkedList=new SingleLinkedList(); ListNode listNode=null; ListNode listNode2=null; List<Integer> maxList=new ArrayList<>(); int max=0; int min=0; // System.out.println("listNodes1.size()="+listNodes1.size()); //System.out.println("listNodes2.size()="+listNodes2.size()); if(listNodes1.size()>listNodes2.size()){ max=listNodes1.size(); min=listNodes2.size(); maxList= listNodes1; } else { max=listNodes2.size(); min=listNodes1.size(); maxList= listNodes2; } System.out.println("max="+max); System.out.println("min="+min); for (int i=0;i<max;i++){ if(i<min){ num=listNodes2.get(i)+listNodes1.get(i)+redis2; if (num>10){ redis=num%10; redis2=1; listNode2=new ListNode(redis); } else if (num==10){ redis=0; redis2=1; listNode2=new ListNode(redis); } else { listNode2=new ListNode(num); redis2=0; } listNode=singleLinkedList.add(listNode2); } if(i>=min) { num=maxList.get(i)+redis2; if (num>10){ redis=num%10; redis2=1; listNode2=new ListNode(redis); } else if (num==10){ redis=0; redis2=1; // System.out.println("000"); listNode2=new ListNode(redis); } else { listNode2=new ListNode(num); redis2=0; } listNode=singleLinkedList.add(listNode2); } } if(redis2>0){ listNode2=new ListNode(redis2); listNode=singleLinkedList.add(listNode2); } return listNode; } } class SingleLinkedList { //Initialize a header node first, the header node does not move ListNode head = new ListNode(0); public ListNode add(ListNode listNode) { ListNode temp=head; //Walk through the list to find the last while (true){ //Find the last link list if(temp.next==null){ break; } //Move temp back if no final one is found temp=temp.next; } //When exiting the while loop, temp points to the end of the list //Point the last node next to the new node temp.next=listNode; return head.next; } }
6.Roman Number to Integer
Roman numerals contain the following seven characters: I, V, X, L, C, D, and M.
Character = Number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, the Roman number 2 is written as II, that is, 1. 12 is written as XII, that is, X + II. 27 is written as XXVII, that is, XX + V + II.
Normally, small numbers in Roman numerals are on the right side of large numbers. There are also exceptions, such as 4 not written as IIII but IV. On the left side of number 5, number 1 represents the number equal to the value 4 obtained by the decrease of number 5. Similarly, number 9 is represented as IX. This special rule only applies to the following six situations:
I can be placed to the left of V(5) and X(10) to represent 4 and 9.
X can be placed to the left of L(50) and C(100) to represent 40 and 90.
C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
Given a Roman number, convert it to an integer. Enter in the range 1 to 3999
Example 1:
Input:'III'
Output: 3
Example 2:
Input:'IV'
Output: 4
Example 3:
Input:'IX'
Output: 9
Example 4:
Input:'LVIII'
Output: 58
Interpretation: L = 50, V= 5, III = 3.
Example 5:
Input:'MCMXCIV'
Output: 1994
Interpretation: M = 1000, CM = 900, XC = 90, IV = 4.
Tips:
1 <= s.length <= 15
s contains only characters ('I','V','X','L','C','D','M')
Title data guarantees that s is a valid Roman numeral and represents an integer in the range [1,3999]
The test cases given by the title all conform to the Roman numeral writing rules, and there will be no crossovers.
Examples such as IL and IM do not meet the requirements of the title. 49 should write XLIX and 999 should write CMXCIX.
For detailed rules on the writing of Roman numerals, you can refer to Roman numerals - Mathematics.
463,234 submissions 732,562
class Solution { public int romanToInt(String s) { int values[] ={1,4,5,9,10,40,50,90,100,400,500,900,1000}; String roman[]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; int Roman=0; for(int i=0;i<s.length();i++){ for(int j=12;j>=0;j--) { if (i + 1 < s.length() && (String.valueOf(s.charAt(i)) + String.valueOf(s.charAt(i+1))).equals(roman[j])){ Roman+=values[j]; i++; break; } else { if (String.valueOf(s.charAt(i)).equals(roman[j])) { Roman += values[j]; } } } } return Roman; } }
7.Integer to Roman Number
Roman numerals contain the following seven characters: I, V, X, L, C, D, and M.
Character = Number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, the Roman number 2 is written as II, that is, 1. 12 is written as XII, that is, X + II. 27 is written as XXVII, that is, XX + V + II.
Normally, small numbers in Roman numerals are on the right side of large numbers. There are also exceptions, such as 4 not written as IIII but IV. On the left side of number 5, number 1 represents the number equal to the value 4 obtained by the decrease of number 5. Similarly, number 9 is represented as IX. This special rule only applies to the following six situations:
I can be placed to the left of V(5) and X(10) to represent 4 and 9.
X can be placed to the left of L(50) and C(100) to represent 40 and 90.
C can be placed to the left of D (500) and M (1000) to represent 400 and 900.
Give you an integer and turn it into a Roman number.
Example 1:
Input: num = 3
Output:'III'
Example 2:
Input: num = 4
Output:'IV'
Example 3:
Input: num = 9
Output:'IX'
Example 4:
Input: num = 58
Output:'LVIII'
Interpretation: L = 50, V = 5, III = 3.
Example 5:
Input: num = 1994
Output:'MCMXCIV'
Interpretation: M = 1000, CM = 900, XC = 90, IV = 4.
Tips:
1 <= num <= 3999
class Solution { public String intToRoman(int num) { int values[] ={1,4,5,9,10,40,50,90,100,400,500,900,1000}; String roman[]={"I","IV","V","IX","X","XL","L","XC","C","CD","D","CM","M"}; int i=12; String Roman=""; while (num>0){ if(num>=values[i]){ num=num-values[i]; Roman+=roman[i]; } else { i--; } } return Roman; } }
8.Valid parentheses
Given a string s containing only'(',')','{','}','[',']'to determine whether the string is valid.
Valid strings need to be:
The left parenthesis must be closed with the same type of right parenthesis.
The left parenthesis must be closed in the correct order.
Example 1:
Input: s ='()'
Output:true
Example 2:
Input: s ='()[]{}'
Output:true
Example 3:
Input: s ='(]'
Output: false
Example 4:
Input: s ='([)]'
Output: false
Example 5:
Input: s ='{[]}'
Output:true
Tips:
1 <= s.length <= 104
s consists of only brackets'()[]{}'
class Solution { public boolean isValid(String s) { int length = s.length() / 2; for (int i = 0; i < length; i++) { s = s.replace("()", "").replace("{}", "").replace("[]", ""); } System.out.println(s); return s.length() == 0; } }
9. Longest common prefix
Write a function to find the longest common prefix in a string array.
Returns the empty string''if no public prefix exists.
Example 1:
Input: STRs = ['flower','flow','flight']
Output:'fl'
Example 2:
Input: STRs = ['dog','racecar','car']
Output: ""
Interpretation: There is no public prefix for the input.
Tips:
1 <= strs.length <= 200
0 <= strs[i].length <= 200
strs[i] consists of only lowercase English letters
class Solution { public String longestCommonPrefix(String[] strs) { String s=""; String s2=""; int num[]=new int[strs.length]; for(int i=0;i<strs.length;i++){ num[i]=strs[i].length(); } for (int gap = num.length / 2; gap > 0; gap /= 2) { // Insert and sort groups directly from the top gap element one by one for (int i = gap; i < num.length ; i++) { int j = i; int temp = num[j]; if (num[j] < num[j - gap]) { while (j - gap >= 0 && temp < num[j - gap]) { //move num[j] = num[j-gap]; j -= gap; } //When you exit while, find the insertion location for temp num[j] = temp; } } } boolean b=false; int sum=0; String snum=""; if(strs.length>1) { for (int k = 0; k < num[0]; k++) { for (int p = 1; p < strs.length; p++) { if (String.valueOf(strs[0].charAt(k)).equals(String.valueOf(strs[p].charAt(k)))) { b = true; sum++; } else { b = false; sum--; break; } } if (k == 0) { if (!b) { return ""; } } if(k>0){ if (!b) { break; } } if (b) { snum += String.valueOf(strs[0].charAt(k)); } } } else { snum=strs[0]; } return snum; } }
21. Merge two ordered chains
Merge the two ascending chaining lists into a new ascending chaining list and return it. The new chaining list is made up of all the nodes of a given two chaining lists.
Example 2:
Input: l1 = [], l2 = []
Output: []
Example 3:
Input: l1 = [], l2 = [0]
Output: [0]
Tips:
The range of the number of nodes in the two linked lists is [0, 50]
-100 <= Node.val <= 100
l1 and l2 are in non-decreasing order
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode() {} * ListNode(int val) { this.val = val; } * ListNode(int val, ListNode next) { this.val = val; this.next = next; } * } */ class Solution { public ListNode mergeTwoLists(ListNode l1, ListNode l2) { if(l1!=null||l2!=null) { List<Integer> list = new ArrayList<>(); while (true) { if (l1 != null) { list.add(l1.val); if (l1.next == null) { break; } l1 = l1.next; } else { break; } } while (true) { if (l2 != null) { list.add(l2.val); if (l2.next == null) { break; } l2 = l2.next; } else { break; } } int temp = 0; for (int i = 0; i < list.size() - 1; i++) { for (int j = 0; j < list.size() - 1 - i; j++) { // If the number in front is larger than the number in the back, swap if (list.get(j) < list.get(j + 1)) { temp = list.get(j); list.set(j, list.get(j + 1)); list.set(j + 1, temp); } } } // list.forEach(System.out::println); ListNode listNode[] = new ListNode[list.size()]; if (list != null) { for (int i = 0; i < list.size(); i++) { if (i == 0) { listNode[i] = new ListNode(list.get(i)); } else { listNode[i] = new ListNode(list.get(i), listNode[i - 1]); } } } return listNode[list.size() - 1]; } else { return null; } } }