16.11.10
来源:互联网 发布:c语言有一个函数 编辑:程序博客网 时间:2024/05/20 21:44
283. Move Zeroes
Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
分析:将整型数组中的0移动到数组的末端
public class Solution { public void moveZeroes(int[] nums) { //用于统计0的个数以便判断将非0的数值向前移动的位数 int totalZeros=0; for(int i=0;i<nums.length;i++){ if(nums[i]==0){ ++totalZeros; }else{ //如果这个数不是0,将当前数向前移动一定的距离(距离等于0的个数) nums[i-totalZeros]=nums[i]; } } //将后几位数都赋值为0 for(int i=nums.length-totalZeros;i<nums.length;i++){ nums[i]=0; } }}
290. Word Pattern
Given a pattern and a string str, find if str follows the same pattern.
Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in str.
Examples:
pattern = “abba”, str = “dog cat cat dog” should return true.
pattern = “abba”, str = “dog cat cat fish” should return false.
pattern = “aaaa”, str = “dog cat cat dog” should return false.
pattern = “abba”, str = “dog dog dog dog” should return false.
Notes:
You may assume pattern contains only lowercase letters, and str contains lowercase letters separated by a single space.
分析:根据给定的模式,判断字符串中的字符是否与给定的模式匹配
public class Solution { public boolean wordPattern(String pattern, String str) { Map<Character,String> map = new HashMap<Character,String>(); char[] charArray = pattern.toCharArray(); int flag=0; //将字符串以空格划分 String[] splits = str.split("\\s"); int splitsLength=splits.length; //获取模式的长度 int charLength=charArray.length; //判断长度是否相等 if(splitsLength!=charLength)return false; String temp; for(int i=0;i<charLength;i++){ //将str中的单词和patternStr中对应的字符以键值对的形式保存 //如果map中的key值(str对应的单词)存在,则判断value是否与对应的patternStr中的字符是否相等 if(map.containsKey(charArray[i])){ //如果不相等,则匹配失败,跳出,返回false if(!map.get(charArray[i]).equals(splits[i])){ flag=1; break; } //如果在map的value中存在patternStr中的字符,则必定匹配失败 }else if(map.containsValue(splits[i])){ flag=1; break; }else{ //将str中的单词和模式中的字符以键值对的形式存储 map.put(charArray[i], splits[i]); } } if(flag==1){ return false; }else{ return true; } }}
303. Range Sum Query - Immutable
Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive.
Example:
Given nums = [-2, 0, 3, -5, 2, -1]
sumRange(0, 2) -> 1
sumRange(2, 5) -> -1
sumRange(0, 5) -> -3
分析:根据给定的索引范围求和(动态规划)
public class NumArray { //dp int sum[]; public NumArray(int[] nums) { //sum中存放nums数组中前i位数之和 sum=nums; for(int i=1;i<nums.length;i++){ sum[i]+=sum[i-1]; System.out.println(sum[i]); } } public int sumRange(int i, int j) { if(i==0){ return sum[j]; } return sum[j]-sum[i-1]; }}// Your NumArray object will be instantiated and called as such:// NumArray numArray = new NumArray(nums);// numArray.sumRange(0, 1);// numArray.sumRange(1, 2);
- 16.11.10
- 苏嵌10 16.11.9
- 苏嵌11 16.11.10
- 16.11.1
- 16.11.2
- 16.11.3
- 16.11.4
- 16.11.5
- 16.11.7
- 16.11.8
- 16.11.9
- 16.11.11
- 16.11.12
- 16.11.24
- 16.11.25
- 16.11.28
- 16.11.29
- 苏嵌3 16.11.1
- 【Android - 框架】之Retrofit+RxJava的使用
- web前端之HTML中a标签锚点
- md5WithRSA
- POJ2051 Argus 优先队列
- 【拥抱开源】十年之做C#屏幕截图工具全面开源
- 16.11.10
- Cisco's router and switch : Encryption and Crack
- Rectangle Area
- 多线程等待唤醒机制:从wait()和sleep()的差别说起
- Deeply-Recursive Convolutional Network for Image Super-Resolution 笔记
- javascript事件委托实现京东首页头部城市选择
- POJ 2784 && UVA1151
- css3初学
- Information theory 熵-不确定性(Entropy - uncertainty)