点击跳转到题目位置
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] + nums[j] == target){
return new int[]{i, j};
}
}
}
return new int[]{0, 0};
}
}
点击跳转到题目位置
class Solution {
public boolean isPalindrome(int x) {
if(x < 0 || (x % 10 == 0 && x != 0)){
return false;
}
int reverseNumber = 0;
while(x > reverseNumber){
reverseNumber = reverseNumber * 10 + x % 10;
x /= 10;
}
return reverseNumber / 10 == x || reverseNumber == x;
}
}
点击跳转到题目位置
class Solution {
Map<Character, Integer> symbplValues = new HashMap<Character, Integer>(){
{
put('I', 1);
put('V', 5);
put('X', 10);
put('L', 50);
put('C', 100);
put('D', 500);
put('M', 1000);
}
};
public int romanToInt(String s) {
int res = 0;
int n = s.length();
for(int i = 0; i < n; ++i){
int num = symbplValues.get(s.charAt(i));
if(i == n - 1){
res += num;
continue;
}
int num1 = symbplValues.get(s.charAt(i+1));
if(num1 > num){
res += (num1 - num);
++i;
} else{
res += num;
}
}
return res;
}
}
点击跳转到题目位置
class Solution {
public String longestCommonPrefix(String[] strs) {
if(strs == null || strs.length == 0){
return "";
}
String res = new String();
int max_length = 1000000;
for(int i = 0; i < strs.length; ++i){
max_length = Math.min(max_length, strs[i].length());
}
for(int i = 0; i < max_length; ++i){
char ch = strs[0].charAt(i);
for(int j = 1; j < strs.length; ++j){
if(strs[j].charAt(i) != ch){
return res;
}
}
res += ch;
}
return res;
}
}
点击跳转到题目位置
class Solution {
public boolean isValid(String s) {
int n = s.length();
if((n & 1) == 1){
return false;
}
Deque<Character> stack = new LinkedList<Character>();
for(int i = 0; i < n; ++i){
if(stack.isEmpty()){
stack.push(s.charAt(i));
} else{
if((stack.peek() == '(' && s.charAt(i) == ')')
|| (stack.peek() == '[' && s.charAt(i) == ']')
|| (stack.peek() == '{' && s.charAt(i) == '}')
){
stack.pop();
} else{
stack.push(s.charAt(i));
}
}
}
return stack.isEmpty();
}
}
点击跳转到题目位置
/**
* 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 list1, ListNode list2) {
if(list1 == null){
return list2;
} else if(list2 == null){
return list1;
} else if(list1.val < list2.val){
list1.next = mergeTwoLists(list1.next, list2);
return list1;
} else{
list2.next = mergeTwoLists(list2.next, list1);
return list2;
}
}
}
点击跳转到题目位置
class Solution {
public int removeDuplicates(int[] nums) {
int left = 0;
int right = 0;
int n = nums.length;
while(right < n){
while(right < n - 1 && nums[right] == nums[left]){
++right;
}
if(right == n - 1 && nums[left] == nums[right]){
break;
}
++left;
nums[left] = nums[right];
}
return left + 1;
}
}
点击跳转到题目位置
class Solution {
public int removeElement(int[] nums, int val) {
int left = 0;
int right = 0;
int n = nums.length;
while(right < n){
if(nums[right] == val){
++right;
continue;
} else{
nums[left] = nums[right];
++left;
++right;
}
}
return left;
}
}
点击跳转到题目位置
class Solution {
public int strStr(String haystack, String needle) {
for(int i = 0; i < haystack.length(); ++i){
int flag = 0;
if(i + needle.length() > haystack.length()){
break;
}
for(int j = 0; j < needle.length(); ++j){
if(haystack.charAt(i + j) != needle.charAt(j)){
flag = -1;
break;
}
}
if(flag == 0){
return i;
}
}
return -1;
}
}
点击跳转到题目位置
class Solution {
public int searchInsert(int[] nums, int target) {
int left = 0;
int right = nums.length - 1;
int ans = -1;
while(left <= right){
int mid = ((right - left) >> 1) + left;
if(nums[mid] == target){
return mid;
} else if(nums[mid] < target){
left = mid + 1;
} else{
ans = mid;
right = mid - 1;
}
}
if(ans == -1){
return nums.length;
}
return ans;
}
}
点击跳转到题目位置
class Solution {
public int lengthOfLastWord(String s) {
int len = 0;
int flag = 0;
for(int i = 0; i < s.length(); ++i){
if(s.charAt(i) == ' '){
flag = 1;
} else{
if(flag == 1){
len = 1;
flag = 0;
} else{
++len;
}
}
}
return len;
}
}
点击跳转到题目位置
class Solution {
public int[] plusOne(int[] digits) {
int n = digits.length;
for(int i = n - 1; i >= 0; --i){
digits[i] = (digits[i] + 1) % 10;
if(digits[i] != 0){
return digits;
}
}
digits = new int[digits.length + 1];
digits[0] = 1;
return digits;
}
}
点击跳转到题目位置
class Solution {
public String addBinary(String a, String b) {
StringBuffer ans = new StringBuffer();
int i = a.length() - 1;
int j = b.length() - 1;
int carry = 0;
while(i >= 0 && j >= 0){
int num1 = a.charAt(i) - '0';
int num2 = b.charAt(j) - '0';
int num = (num1 + num2 + carry) % 2;
carry = (num1 + num2 + carry) / 2;
ans.append(num);
--i;
--j;
}
while(i >= 0){
int num1 = a.charAt(i) - '0';
int num = (num1 + carry) % 2;
carry = (num1 + carry) / 2;
ans.append(num);
--i;
}
while(j >= 0){
int num2 = b.charAt(j) - '0';
int num = (num2 + carry) % 2;
carry = (num2 + carry) / 2;
ans.append(num);
--j;
}
if(carry == 1){
ans.append(1);
}
ans.reverse();
return ans.toString();
}
}
点击跳转到题目位置
class Solution {
public int mySqrt(int x) {
int left = 0;
int right = x;
int ans = -1;
while(left <= right){
int mid = ((right - left) >> 1) + left;
long num = (long)mid * mid;
if(num == x){
return mid;
} else if(num < x){
ans = mid;
left = mid + 1;
} else{
right = mid - 1;
}
}
return ans;
}
}
点击跳转到题目位置
class Solution {
public int climbStairs(int n) {
int []dp = new int[n+1];
dp[0] = 1;
dp[1] = 1;
for(int i = 2; i <= n; ++i){
dp[i] = dp[i-1] + dp[i-2];
}
return dp[n];
}
}