初级算法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 ...
随机推荐
- html5标签---不常用新标签的整理
状态标签 meter 用来显示已知范围的标量值或者分数值. value:当前的数值. min:值域的最小边界值.如果设置了,它必须比最大值要小.如果没设置,默认为0 max:值域的上限边界值.如果设置 ...
- save is not valid without active transaction
org.hibernate.HibernateException: save is not valid without active transaction at org.hibernate.cont ...
- HDU 6153 A Secret (KMP)
题意:给定两个串,求其中一个串 s 的每个后缀在另一个串 t 中出现的次数. 析:首先先把两个串进行反转,这样后缀就成了前缀.然后求出 s 的失配函数,然后在 t 上跑一遍,如果发现不匹配了或者是已经 ...
- 编写高质量代码改善C#程序的157个建议——建议142:总是提供有意义的命名
建议142:总是提供有意义的命名 除非有特殊原型,否则永远不要为自己的代码提供无意义的命名. 害怕需要过长的命名才能提供足够的意义?不要怕,其实我们更介意的是在代码的时候出现一个iTemp. int ...
- UT源码162
(3)设计佣金问题的程序 commission方法是用来计算销售佣金的需求,手机配件的销售商,手机配件有耳机(headphone).手机壳(Mobile phone shell).手机贴膜(Cellp ...
- 了解entity framework其他query方式之Entity SQL,Raw Sql分析
一:linq 对ef来说不是唯一性的query... 二:Entity Sql 1. esql => entity sql... [类sql的语言] 和sql差不多,但是呢,不是sql... u ...
- touch和click优先性
jQuery的touch事件是当用户触摸事件(页面)时触发的. jQuery的click事件是当用户点击元素时触发的. 而事件执行流程是手指点击一个元素,会经过:touchstart --> t ...
- auc的本质
AUC的本质 定义 auc是roc曲线下的面积.其中,roc是横坐标为fpr,纵坐标是tpr的坐标系上的曲线. TPR(true positive rate):所有正样本中被预测为正的比例 FPR(f ...
- js和C# 时间日期格式转换
下午在搞MVC和EXTJS的日期格式互相转换遇到了问题,我们从.NET服务器端序列化一个DateTime对象的结果是一个字符串格式,如 '/Date(1335258540000)/' 这样的字串. 整 ...
- kali 下 apache 配置文件
默认的可执行文件 /usr/sbin/apache2 root@ty:/etc/init.d# netstat -anp |grep apache tcp6 ::: :::* LISTEN /apac ...