初级算法49题 — LeetCode(20181122 - )
Array:
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;
}
}
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;
}
}
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;
}
}
这题注意处理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;
}
}
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;
}
}
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;
}
}
注意当结果为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;
}
}
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;
}
}
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
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);
}
}
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();
}
}
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;
}
}
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 - )的更多相关文章
- LeetCode初级算法的Python实现--链表
LeetCode初级算法的Python实现--链表 之前没有接触过Python编写的链表,所以这里记录一下思路.这里前面的代码是和leetcode中的一样,因为做题需要调用,所以下面会给出. 首先定义 ...
- 【LeetCode算法】LeetCode初级算法——字符串
在LeetCode初级算法的字符串专题中,共给出了九道题目,分别为:反转字符串,整数反转,字符串中的第一个唯一字符,有效的字母异位词,验证回文字符串,字符串转换整数,实现strStr(),报数,最 ...
- LeetCode探索初级算法 - 动态规划
LeetCode探索初级算法 - 动态规划 今天在LeetCode上做了几个简单的动态规划的题目,也算是对动态规划有个基本的了解了.现在对动态规划这个算法做一个简单的总结. 什么是动态规划 动态规划英 ...
- LeetCode初级算法--数组01:只出现一次的数字
LeetCode初级算法--数组01:只出现一次的数字 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
- LeetCode初级算法--数组02:旋转数组
LeetCode初级算法--数组02:旋转数组 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- LeetCode初级算法--字符串01:反转字符串
LeetCode初级算法--字符串01:反转字符串 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.ne ...
- LeetCode初级算法--链表01:反转链表
LeetCode初级算法--链表01:反转链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.net/ ...
- LeetCode初级算法--链表02:合并两个有序链表
LeetCode初级算法--链表02:合并两个有序链表 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn. ...
- LeetCode初级算法--树01:二叉树的最大深度
LeetCode初级算法--树01:二叉树的最大深度 搜索微信公众号:'AI-ming3526'或者'计算机视觉这件小事' 获取更多算法.机器学习干货 csdn:https://blog.csdn.n ...
随机推荐
- mvc注解验证
前端: @{ Layout = null;}@using System.Activities.Expressions@model MvcApplication1.Models.News<!DOC ...
- Part3_lesson2---ARM指令分类学习
1.算术和逻辑指令 mov.mvn.cmp.tst.sub.add.and.bic 2.比较指令 cmp和tst 3.跳转指令 b和bl 4.移位指令 lsl和ror 5.程序状态字访问指令 msr与 ...
- position与offset的区别
.offset()是相对于文档(document)的当前位置,.position()是相对于父级元素的位移,一个元素可以嵌套多个position
- mysql链接错误:2003 can't connect to mysql server on 10038
出现这个错误原因是端口号不是3306. 打开D:\Program Files\MySQL\MySQL Server 5.5 \my.ini文件,当然还有其他的.ini的文件: [client] ...
- Logback configuration
官方指导 http://logback.qos.ch/manual/configuration.html 规则 ch.qos.logback.core.joran.JoranConfiguratorB ...
- React Native开发环境的搭建
我只能说搭建开发环境还是不能相信网上纷乱的博客,还是中文网靠谱. http://reactnative.cn/docs/0.47/getting-started.html 纯粹只是为了记录一下.
- Smart3d 近景摄影测量与航空摄影测量
实例:http://blog.sina.com.cn/s/blog_8f68d2350102wef4.html ContextCapture(Smart3d):https://www.bentley. ...
- hibernate通过SQL更新数据
@Resource(name = "hibernateTemplate") public HibernateTemplate hibernateTemplate; /** * @T ...
- unigui的UnimDatePicker控件使用经验
最近使用unigui的UnimDatePicker控件遇到只能选择当年之前的年份和日期及日期选择界面不能显示中文的问题,经以下设置就能正常使用.1.UnimDatePicker月份显示中文 unim ...
- [LeetCode 题解]: Search a 2D Matrix
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...