Array:

Single Number
 class Solution {
public int singleNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int res = nums[0];
for (int i = 1; i < nums.length; i++) {
res = res ^ nums[i];
} return res;
}
}
Remove Duplicates from Sorted Array
 class Solution {
public int removeDuplicates(int[] nums) {
if(nums==null || nums.length==0){
return 0;
}
int res =1;
for(int i=1;i<nums.length;i++){
if(nums[i]!=nums[i-1]){
nums[res] = nums[i];
res++;
}
}
return res;
}
}
Best Time to Buy and Sell Stock II
 class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int res = 0;
for (int i = 0; i < prices.length - 1; i++) {
if (prices[i + 1] - prices[i] > 0) {
res += prices[i + 1] - prices[i];
}
}
return res;
}
}
Rotate Array

这题注意处理k>nums.length的情况,直接取余

 class Solution {
public void rotate(int[] nums, int k) {
if (nums == null || nums.length == 0) {
return;
}
k = k%nums.length;
rotateSubArray(nums, 0, nums.length - k - 1);
rotateSubArray(nums, nums.length - k, nums.length - 1);
rotateSubArray(nums, 0, nums.length - 1);
} public void rotateSubArray(int[] nums, int start, int end) {
int left = start;
int right = end;
while (left < right) {
swap(nums, left, right);
left++;
right--;
}
} public void swap(int[] nums, int aIndex, int bIndex) {
int temp = nums[aIndex];
nums[aIndex] = nums[bIndex];
nums[bIndex] = temp;
}
}
Contains Duplicate
 class Solution {
public boolean containsDuplicate(int[] nums) {
if (nums == null || nums.length == 0) {
return false;
} Arrays.sort(nums);
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1])
return true;
} return false;
}
}
 Intersection of Two Arrays II
 class Solution {
public int[] intersect(int[] nums1, int[] nums2) {
if (nums1 == null || nums1.length == 0) {
return nums1;
} if (nums2 == null || nums2.length == 0) {
return nums2;
}
List<Integer> list = new ArrayList<>();
HashMap<Integer, Integer> map = new HashMap<>();
for (int num : nums1) {
if (map.get(num) == null) {
map.put(num, 1);
} else {
map.put(num, map.get(num) + 1);
}
} for (int num : nums2) {
Integer exist = map.get(num);
if (exist != null && exist > 0) {
map.put(num, exist - 1);
list.add(num);
}
} int[] res = new int[list.size()];
for (int i = 0; i < res.length; i++) {
res[i] = list.get(i);
}
return res;
}
}
Plus One

注意当结果为1000...时需重新生成长数组返回,否则WA(eg:99999 -> 00000)

 class Solution {
public int[] plusOne(int[] digits) {
if (digits == null || digits.length == 0) {
return digits;
} int carry = digits[digits.length - 1] + 1 >= 10 ? 1 : 0;
digits[digits.length - 1] = carry == 1 ? 0 : digits[digits.length - 1] + 1;
if (carry == 0) {
return digits;
}
for (int i = digits.length - 2; i >= 0; i--) {
if (digits[i] + carry >= 10) {
digits[i] = 0;
} else {
digits[i] = digits[i] + carry;
return digits;
}
} int[] newNumber = new int[digits.length+1];
newNumber[0] = 1;
return newNumber;
}
}
Move Zeroes
 class Solution {
public void moveZeroes(int[] nums) {
if (nums == null || nums.length == 0) {
return;
} int noZeroIndex = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] != 0) {
nums[noZeroIndex] = nums[i];
noZeroIndex++;
}
} for (int j = noZeroIndex; j < nums.length; j++) {
nums[j] = 0;
} return;
}
}
Two Sum
 class Solution {
public int[] twoSum(int[] nums, int target) {
if (nums == null || nums.length < 2) {
return null;
} Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < nums.length; i++) {
Integer exist = map.get(target - nums[i]);
if (exist != null) {
int[] res = new int[2];
res[0] = i;
res[1] = exist;
return res;
}
map.put(nums[i], i);
} return null;
}
}

Strings

Reverse String
 class Solution {
public String reverseString(String s) {
if (s == null || s.length() == 0) {
return s;
}
char[] arrays = s.toCharArray();
int start = 0;
int end = arrays.length - 1;
while (start < end) {
char temp = arrays[start];
arrays[start] = arrays[end];
arrays[end] = temp;
start++;
end--;
} return new String(arrays);
}
}
Reverse Integer
 class Solution {
public int reverse(int x) {
String s = x + "";
if (s.length() <= 1) {
return x;
}
char[] arrays = s.toCharArray();
int len = arrays.length;
int start = 0;
int end = s.length() - 1;
while (start < arrays.length && arrays[start] > '9' || arrays[start] < '0') {
start++;
}
while (end >= 0 && arrays[end] == '0') {
arrays[end] = ' ';
end--;
}
while (start < end) {
char temp = arrays[start];
arrays[start] = arrays[end];
arrays[end] = temp;
start++;
end--;
} s = new String(arrays);
Long res = Long.valueOf(s.trim());
if (res > Integer.MAX_VALUE || res < Integer.MIN_VALUE) {
return 0;
}
return res.intValue();
}
}
 First Unique Character in a String
 class Solution {
public int firstUniqChar(String s) {
if (s == null || s.length() == 0) {
return -1;
} char[] array = s.toCharArray();
Map<Character, Integer> map = new HashMap<>();
for (int i = 0; i < array.length; i++) {
Integer exist = map.get(array[i]);
if (exist == null) {
map.put(array[i], 1);
} else {
map.put(array[i], exist + 1);
}
} for (int i = 0; i < array.length; i++) {
if (map.get(array[i]) == 1) {
return i;
}
}
return -1;
}
}
Valid Palindrome
 class Solution {
public boolean isPalindrome(String s) {
if (s == null || s.length() == 0) {
return true;
}
s = s.trim();
int start = 0;
int end = s.length() - 1;
while (start < end) {
while (start < s.length() && !Character.isLetter(s.charAt(start)) && !Character.isDigit(s.charAt(start))) {
start++;
} while (end >= 0 && !Character.isLetter(s.charAt(end)) && !Character.isDigit(s.charAt(end))) {
end--;
}
if (start >= end) {
break;
}
if (Character.toLowerCase(s.charAt(start)) != Character.toLowerCase(s.charAt(end))) {
return false;
}
start++;
end--;
} return true;
}
}

初级算法49题 — LeetCode(20181122 - )的更多相关文章

  1. LeetCode初级算法的Python实现--链表

    LeetCode初级算法的Python实现--链表 之前没有接触过Python编写的链表,所以这里记录一下思路.这里前面的代码是和leetcode中的一样,因为做题需要调用,所以下面会给出. 首先定义 ...

  2. 【LeetCode算法】LeetCode初级算法——字符串

      在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...

  3. LeetCode探索初级算法 - 动态规划

    LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...

  4. LeetCode初级算法--数组01:只出现一次的数字

    LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...

  5. LeetCode初级算法--数组02:旋转数组

    LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...

  6. LeetCode初级算法--字符串01:反转字符串

    LeetCode初级算法--字符串01:反转字符串 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...

  7. LeetCode初级算法--链表01:反转链表

    LeetCode初级算法--链表01:反转链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...

  8. LeetCode初级算法--链表02:合并两个有序链表

    LeetCode初级算法--链表02:合并两个有序链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...

  9. LeetCode初级算法--树01:二叉树的最大深度

    LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...

随机推荐

  1. Redhat9.0+Apache1.3.29+Mysql3.23.58+PHP4.3.4

    Redhat9.0+Apache1.3.29+Mysql3.23.58+PHP4.3.4 TAG标签: 摘要:红帽创建于1993年,是目前世界上最资深的Linux和开放源代码提供商,同时也是最获认可的 ...

  2. Python Socket实现简单的聊天室

    通过参考其他牛人的文章和代码,  再根据自己的理解总结得出,  说明已经加在注释中, FYI 主要参考文章: http://blog.csdn.net/dk_zhe/article/details/3 ...

  3. 项目中遇到的死锁问题: Lock wait timeout exceeded; try restarting transaction

    最近项目中频繁出现  Lock wait timeout exceeded; try restarting transaction这个错误,把我们弄得痛苦不堪啊,为了解决问题,上网上找好多资料,终于把 ...

  4. C# 生成dll文件 并导入使用

    首先 在unity创建一个脚本 并编写内容,其中需要调用的方法.变量要公有化(也可以直接新建cs文件用编译器打开编译,但要先导入UnityEngine.dll). 然后,复制脚本关闭unity,在外界 ...

  5. 设计模式15:Interpreter 解释器模式(行为型模式)

    Interpreter 解释器模式(行为型模式) 动机(Motivation) 在软件构建过程中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变 ...

  6. uva639 回溯!

    #include<iostream> using namespace std; int n,Max,C[4][4]; char board[5][5]; bool vis[16]; boo ...

  7. 12、Semantic-UI之输入框

    12.1 基础输入框   在Semantic-UI中可以定义多个样式的输入框,可以将图片与输入框结合,输入提示信息文字,设置输入框的状态. 示例:定义基础输入框 用户名: <div class= ...

  8. C# 同步锁 lock Monitor

    Lock关键字 C#提供lock关键字实现临界区,MSDN里给出的用法: Object thisLock = new Object();lock (thisLock){   // Critical c ...

  9. solr入门教程-较详细

    Solr调研总结 开发类型 全文检索相关开发 Solr版本 4.2 文件内容 本文介绍solr的功能使用及相关注意事项;主要包括以下内容:环境搭建及调试;两个核心配置文件介绍;维护索引;查询索引,和在 ...

  10. Windowsform datagridview选中向父窗口传值

    Datagridview mouseclick事件 private void dataGridView1_MouseClick(object sender, MouseEventArgs e) { i ...